Zoom problems of Canvas and ScrollViewer


  • Hi all,

    I'm developing a chart app using WPF, I met some problems during implementing zoom functionality, here is the details:

    I put all elements I want to zoom in a canvas called ZoomCanvas, which itself is in another Canvas called RootCanvas, the chart Axis, titles are all in RootCanvas, too. Then I put RootCanvas in a ScrollViewer, something like this:

     <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
       <Canvas  Name="RootCanvas" Height="600" Width="800">

         <Canvas  Name="ZoomCanvas" Height="480" Width="680" >
             <ScaleTransform x:Name="m_canvasTransform"/>

         <TextBlock/>...    <Line/>



    What I want to achieve is to only zoom ZoomCanvas, without affect any other elements. From user's point of view, the render size of ZoomCanvas is fixed. I use RenderTransform to scale ZoomCanvas, it works, but the content will be rendered outside of the canvas original size. Actually the height and width of ZoomCanvas are not changed. How can I scale the canvas while keep it rendered in original size?

    The second problem is related. Since RootCanvas's size are not changed, the parent ScrollViewer doesn't get notified to enable the scrollbar automatically. Should I manualy in the code compute the offset and increase the size of RootCanvas? Or is there any better or standard way to achieve this?

    Any comment or suggestion will be highly appreciate, thanks in advance.

    Wednesday, December 06, 2006 8:35 AM

All replies

  • Do you want dimensions of ZoomCanvas to update because of its Transform? If so, set ScaleTransform on Canvas.LayoutTransform instead of RenderTransform - 

    If you don't want dimensions of ZoomCanvas to change and want render within bounds then use RenderTransform and set Canvas.ClipToBounds="true".

    Wednesday, December 06, 2006 11:38 PM
  • In case anyone runs into this problem in the future...


    You can prevent a control from drawing outside it's bounds by setting the ClipToBounds property to true.

    Saturday, September 22, 2007 4:54 PM
  • But the contrapositive is not necessarily true.  Specifically, you cannot set ClipToBounds to false and expect the element to automatically draw outside of its bounds.


    ClipToBounds is buggy in this respect.  The name implies that this is the only criteria for determining if clipping occurs, but the framework actually uses additional criteria to decide whether a control will apply clipping to its children.


    For example, try the following in XamlPad and notice that the clip is still enforced even though ClipToBounds is false:


    <Page xmlns=""
      <Grid Width="100" Height="100" Background="Green" ClipToBounds="False">
        <Ellipse Fill="Blue" Width="300" Height="200" />


    File this under Tips and Tricks:  A solution for this problem is to wrap the Grid's content in a Canvas. (Yes, you may need some other layout element inside the canvas to support your desired layout.)  For example, the above example would now look like this:


    <Page xmlns=""
      <Grid Width="100" Height="100" Background="Green" ClipToBounds="False">
          <Ellipse Fill="Blue" Width="300" Height="200" />


    The reason this works is because a Canvas always returns a size of 0,0.  So the parent element assumes there is nothing to clip and doesn't even try to clip its content.  Voila!  The content flows outside of the Grid, as desired.


    Sunday, September 23, 2007 8:07 PM
  • I have a grid that has a RenderTransform setting that scales its children down to a much smaller size.  The problem is that the children get clipped to the grid size BEFORE the grid's RenderTransform is applied to the children, so the children get clipped, then scaled, and so most of the children are clipped away.  This happens even if both grid and children have ClipToBounds set false.


    Sticking a canvas between the grid and its children worked, thank you!  But, I'm wondering if I've misunderstood my problem and fixed it the wrong way?


    I'm pretty sure I don't want to be using LayoutTransform.  I actually compute the RenderTransform scale factor in an overridden Grid.MeasureOverride(), and by then it is too late to be diddling around with LayoutTransform (I tried).  I want to scale the grid's children according to the available space for the grid.  If the user resizes the window, I want the RenderTransform scale factor to change accordingly, so that fixed children get scaled properly to the new grid.  Perhaps I could apply the scale factor to all of the children, but there are a lot and it seemed easier to set the grid RenderTransform instead.  Is this wrong?




    Monday, April 28, 2008 4:04 AM
  • Sounds like a perfectly reasonable approach to me, Ted!  I've used a similar tact myself. Smile


    Monday, April 28, 2008 6:15 AM
  • I ran out of the very same scroll viewer broblem. Just changed RenderTransform to LayoutTransform everything is fine :)


    Wednesday, December 02, 2009 11:57 AM