问题
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 following the size of my control. Any suggestions?
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/5679264/how-to-create-stretching-clipping-rectangle-in-silverlight