Question

I have a Canvas, to which I've added several thousand polygons.

I would like to be able to zoom in (which I'm doing via a ScaleTransform.

However I've been trying to use a Canvas.Clip as well to only draw a portion of the Canvas, but as soon as the ScaleTransform values are changed, the clipping stops working...

    <Canvas Grid.Row="1" Margin="10" x:Name="cnvMain" Background="Transparent" >
        <Canvas.Clip>
            <RectangleGeometry x:Name="CanvasClip"  Rect="0, 0, 300, 300"/>
        </Canvas.Clip>
        <Canvas.RenderTransform>
            <ScaleTransform x:Name="CanvasScaleTransform" ScaleX="1" ScaleY="1"></ScaleTransform>
        </Canvas.RenderTransform>
     </Canvas>

And in my codebehind,

    private void slScale_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        CanvasScaleTransform.ScaleX = slScale.Value;
        CanvasScaleTransform.ScaleY = slScale.Value;
    }

Am I doing anything obviously wrong?

Was it helpful?

Solution

Put a border around your canvas and attach the clip region to the border rather than the canvas.

OTHER TIPS

The ScaleTransform (as all other transformations) is applied AFTER every other rendering. That means, first the cliprect is applied, then the scale transform. A solution would be to do the clipping one level higher, by putting another canvas around this one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top