如何测量 WPF/Silverlight 中 Path 对象的大小?

发布于 2024-12-14 22:03:40 字数 2106 浏览 1 评论 0原文

我正在 WPF 中开发映射系统,在处理 Path 元素时遇到了一些奇怪的行为。我们有一个画布元素,其中包含显示地图上位置的路径对象。当我尝试通过代码测量它们的大小或位置时,它们的大小始终包括它们距原点 (0,0) 的距离,并且它们的位置始终位于原点。我不知道如何测量路径本身的实际可见区域。这是一个示例:

Path Sample

现在,您可以看到的路径大小只有大约一百个像素,但是在阅读时AcutalWidth/ActualHeight 属性也包括距上/左角的距离。我还在路径上尝试了 .Measure() 方法并得到了相同的结果。是否有任何特殊工具可以实际测量路径的可见区域?

下面是这个例子的代码供参考:

<Window x:Class="WPF_Testing_Area.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Canvas x:Name="Wrapper" Width="2185.922" Height="3091.486">
            <Path Fill="#FF9EC99F" MouseLeftButtonDown="Path_MouseLeftButtonDown" Stroke="Black" StrokeThickness="1.5" StrokeStartLineCap="Round" StrokeEndLineCap="Round" StrokeDashCap="Round" StrokeLineJoin="Round" 
                    Data="F1M1501.916,677.412C1480.471,683.851 1457.263,676.647 1443.234,659.196 1441.36,656.865 1439.083,654.889 1436.51,653.362L1436.805,800.23 1533.736,819.855C1537.515,820.62,1541.335,821.166,1545.177,821.49L1501.916,677.412z"/>
        </Canvas>
    </Grid>
</Window>

以及背后的代码:

using System.Windows;
using System.Windows.Input;
using System.Windows.Shapes;

namespace WPF_Testing_Area
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Path_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var path = sender as Path;
            var size = new Size(path.ActualWidth, ActualHeight);
            MessageBox.Show(size.ToString());

            // try to measure the shape
            path.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
            MessageBox.Show(path.DesiredSize.ToString());
        }
    }
}

I am working on a mapping system in WPF and have come across some weird behavior when dealing with Path elements. We have a canvas element with path objects within it that show locations on a map. When I try to measure their size or location through code, their size is always includes their distance from the origin (0,0) and their location is always at the origin. I cannot figure out how to measure the actual visible area of the path itself. Here is an example:

Path Sample

Now the path as you can see is only roughly a hundred pixels in size, but when reading the AcutalWidth/ActualHeight properties it includes its distance from the top/left corner too. I also tried the .Measure() method on the path and got the same result. Are there any special tools to use that can actually measure the visible area of paths?

Here is the code of this example for reference:

<Window x:Class="WPF_Testing_Area.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Canvas x:Name="Wrapper" Width="2185.922" Height="3091.486">
            <Path Fill="#FF9EC99F" MouseLeftButtonDown="Path_MouseLeftButtonDown" Stroke="Black" StrokeThickness="1.5" StrokeStartLineCap="Round" StrokeEndLineCap="Round" StrokeDashCap="Round" StrokeLineJoin="Round" 
                    Data="F1M1501.916,677.412C1480.471,683.851 1457.263,676.647 1443.234,659.196 1441.36,656.865 1439.083,654.889 1436.51,653.362L1436.805,800.23 1533.736,819.855C1537.515,820.62,1541.335,821.166,1545.177,821.49L1501.916,677.412z"/>
        </Canvas>
    </Grid>
</Window>

And the code behind:

using System.Windows;
using System.Windows.Input;
using System.Windows.Shapes;

namespace WPF_Testing_Area
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Path_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var path = sender as Path;
            var size = new Size(path.ActualWidth, ActualHeight);
            MessageBox.Show(size.ToString());

            // try to measure the shape
            path.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
            MessageBox.Show(path.DesiredSize.ToString());
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

万水千山粽是情ミ 2024-12-21 22:03:41

我会尝试从 path.ActualWidth 的值中减去路径 Canvas.Left 的值(Canvas.Top 和 path.ActualHeight 相同)。我想这会给你正确的尺寸。

I'd try subtracting the value of the path's Canvas.Left from the value of path.ActualWidth (same for Canvas.Top and path.ActualHeight). I'd think that would give you the correct size.

忆梦 2024-12-21 22:03:41

这将被证明是相当棘手的,因为如果路径包含圆弧并且该圆弧延伸超出用于定义路径的坐标/点,则将很难计算边界框。

请参阅:WPF:如何获取真实大小形状(边界框)获取 WPF 元素的真实视觉边界框?

This will prove to be quite tricky because if the path contains an arc and the arc extends beyond the coordinates/point that were used to define the path, it will be hard to calculate a bounding box.

See: WPF: How to get the true size (Bounding Box) of shapes and Getting the true visual bounding box of a WPF element?

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