我在画布中有一个带圆角的边框,并希望在画布上添加剪裁区域,以便我添加的任何内容都会剪切到边框内的区域。我知道我可以设置画布的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>
答案 0 :(得分:1)
尝试使用ActualHeight和ActualWidth属性
var height = TimelineCanvas.ActualHeight;
var width = TimelineCanvas.ActualWidth;
答案 1 :(得分:0)
我最终使用了这段代码,但仍然对任何替代方法感兴趣。
RectangleGeometry clipRect = new RectangleGeometry();
clipRect.Rect = new Rect(TimelineBorder.Margin.Left, TimelineBorder.Margin.Top, TimelineCanvas.ActualWidth - (TimelineBorder.Margin.Left + TimelineBorder.Margin.Right), TimelineCanvas.ActualHeight - (TimelineBorder.Margin.Top + TimelineBorder.Margin.Bottom));
clipRect.RadiusX = TimelineBorder.CornerRadius.TopLeft;
clipRect.RadiusY = TimelineBorder.CornerRadius.TopLeft;
TimelineCanvas.Clip = clipRect;
答案 2 :(得分:0)
blacklight工具包有一个圆角修剪工具,是免费的。