Uielement.cliptobounds is in WPF, but not in Silverlight. How to simulate in Silverlight?

I have a Canvas in WPF, and I want to prevent its children from being drawn outside the edge of Canvas ara. In WPF, this is very simple, because you only need to put the Canvas on Set the ClipToBounds property to True and it will execute as expected.

There is a problem with porting the sample XAML to Silverlight, because ClipToBounds does not exist! Is there a way to simulate this function? I am happy to derive from Canvas and override the Measure / Arrange method when needed.

I found it myself Solution. 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();
)

I have a Canvas in WPF, and I want to prevent its children from being drawn outside the edge of Canvas ara. In WPF, this is simple because You only need to set the ClipToBounds property on the Canvas to True, and it will execute as expected.

There is a problem with porting the sample XAML to Silverlight, because ClipToBounds does not exist! Is there a way to simulate this function? I am happy to derive from Canvas and override the Measure / Arrange method when needed.

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();
}

Leave a Comment

Your email address will not be published.