How to create stretching clipping rectangle in Silverlight

帅比萌擦擦* 提交于 2019-12-10 21:44:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!