在 Silverlight 2 中动态确定路径
我在画布内有一个带有圆角的边框,并且想要向画布添加一个剪切区域,以便我添加的任何内容都会被剪切到边框内的区域。 我知道我可以设置画布的 Clip 属性,但由于画布和对象是动态调整大小的,而不是在 XAML 中分配大小,所以我无法弄清楚如何计算要使用的路径。 有没有某种方法可以从 UIElement(本例中为边框)派生 PathGeometry? 如果不是,解决这个问题的最佳方法是什么? 这是我正在使用的测试页的 XAML。
<UserControl x:Class="TimelinePrototype.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Margin="10">
<Button x:Name="cmdDraw" FontSize="18" Click="cmdDraw_Click" Content="Draw" Margin="0,0,5,0" VerticalAlignment="Bottom" />
<TextBlock x:Name="txtDateRange" FontSize="18" Margin="10,0,10,10" VerticalAlignment="Bottom" />
</StackPanel>
<Canvas x:Name="TimelineCanvas" Grid.Row="1" HorizontalAlignment="Stretch"
SizeChanged="TimelineCanvas_SizeChanged">
<Border x:Name="TimelineBorder"
Background="LightGray"
BorderBrush="Black"
BorderThickness="2"
CornerRadius="15"
Margin="10"
Grid.Row="1"
VerticalAlignment="Top">
</Border>
</Canvas>
</Grid>
I have a border with rounded corners within a canvas and want to add a clipping region to the canvas so that anything I add is clipped to the region within the border. I know that I can set the Clip property of the canvas but as the canvas and object are sized dynamically rather than having sizes assigned in the XAML, I can't figure out how to calculate the path to use. Is there some way to derive a PathGeometry from a UIElement (the border in this case)? If not what is the best way to approach this? Here is the XAML for the test page I'm working with.
<UserControl x:Class="TimelinePrototype.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Margin="10">
<Button x:Name="cmdDraw" FontSize="18" Click="cmdDraw_Click" Content="Draw" Margin="0,0,5,0" VerticalAlignment="Bottom" />
<TextBlock x:Name="txtDateRange" FontSize="18" Margin="10,0,10,10" VerticalAlignment="Bottom" />
</StackPanel>
<Canvas x:Name="TimelineCanvas" Grid.Row="1" HorizontalAlignment="Stretch"
SizeChanged="TimelineCanvas_SizeChanged">
<Border x:Name="TimelineBorder"
Background="LightGray"
BorderBrush="Black"
BorderThickness="2"
CornerRadius="15"
Margin="10"
Grid.Row="1"
VerticalAlignment="Top">
</Border>
</Canvas>
</Grid>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用 ActualHeight 和 ActualWidth 属性
Try using the ActualHeight and ActualWidth properties
我最终使用了这段代码,但仍然对任何替代方法感兴趣。
I ended up using this code, but would still be interested in any alternate methods.
尝试 blacklight
blacklight 工具包有一个圆角剪切工具,并且是免费的。
Try blacklight
The blacklight toolpack has a rounded corner clipping tool and is free.