views:

873

answers:

1

I have a Canvas in WPF and I want to prevent its children from being drawn outside the edges of the Canvas ara. In WPF this is simple as you just set the ClipToBounds property on the Canvas to True and it does as expected.

Porting the sample XAML to Silverlight there is an issue because ClipToBounds does not exist! Is there a way to simulate this functionality? I am happy to derive from Canvas and override the Measure/Arrange methods if needed.

+2  A: 

I found the solution myself. Override the ArrangeOverride method like this...

protected override Size ArrangeOverride(Size finalSize)
{
    RectangleGeometry clipRectGeometry = new RectangleGeometry();
    clipRectGeometry.Rect = new Rect(new Point(0,0), finalSize);
    Clip = clipRectGeometry;

    return base.ArrangeOverride();
}
Phil Wright