How to create stretching clipping rectangle in Silverlight
Since Silverlight doesn't have the comfy feature of 'ClipToBounds' properties on controls, I have to define clipping shapes by myself. I was wondering if I could create a clipping rectangle that's f开发者_运维技巧ollowing the size of my control. Any suggestions?
If there is an existing control in you layout that you want to dynamically clip then use its SizeChanged
event. For example lets say you want to clip this Grid:-
<Grid SizeChanged="Grid_SizeChanged" Width="50" Height="20">
<Grid.Clip>
<RectangleGeometry />
</Grid.Clip>
<TextBlock Margin="0 -9 0 0" Text="This text should not be legible" />
</Grid>
With the code-behind:-
private void Grid_SizeChanged(object sender, SizeChangedEventArgs e)
{
((RectangleGeometry)((Grid)sender).Clip).Rect = new Rect(0.0, 0.0, e.NewSize.Width, e.NewSize.Height);
}
For a your own custom control you might consider handling the clip rectangle in the ArrangeOverride
instead of relying on the SizeChanged event. In this case you probably want to assign RectangleGeometry to the Clip
property in code rather than relying on it being assigned in the Xaml of the default template.
Silverlight supports that: try using HorisontalAlignment and vertical alignment propertys. Set them to stretch. If this doesn't work then you will have to post xaml example.
精彩评论