在 Silverlight 2 中动态确定路径

发布于 2024-07-15 18:33:25 字数 1442 浏览 6 评论 0原文

我在画布内有一个带有圆角的边框,并且想要向画布添加一个剪切区域,以便我添加的任何内容都会被剪切到边框内的区域。 我知道我可以设置画布的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

败给现实 2024-07-22 18:33:25

尝试使用 ActualHeightActualWidth 属性

var height = TimelineCanvas.ActualHeight;
var width = TimelineCanvas.ActualWidth;

Try using the ActualHeight and ActualWidth properties

var height = TimelineCanvas.ActualHeight;
var width = TimelineCanvas.ActualWidth;
说好的呢 2024-07-22 18:33:25

我最终使用了这段代码,但仍然对任何替代方法感兴趣。

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;

I ended up using this code, but would still be interested in any alternate methods.

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;
開玄 2024-07-22 18:33:25

尝试 blacklight

blacklight 工具包有一个圆角剪切工具,并且是免费的。

Try blacklight

The blacklight toolpack has a rounded corner clipping tool and is free.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文