如何测量 WPF/Silverlight 中 Path 对象的大小?
我正在 WPF 中开发映射系统,在处理 Path 元素时遇到了一些奇怪的行为。我们有一个画布元素,其中包含显示地图上位置的路径对象。当我尝试通过代码测量它们的大小或位置时,它们的大小始终包括它们距原点 (0,0) 的距离,并且它们的位置始终位于原点。我不知道如何测量路径本身的实际可见区域。这是一个示例:
现在,您可以看到的路径大小只有大约一百个像素,但是在阅读时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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会尝试从 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.
这将被证明是相当棘手的,因为如果路径包含圆弧并且该圆弧延伸超出用于定义路径的坐标/点,则将很难计算边界框。
请参阅: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?
我认为这将是更多技巧。你可以参考这些链接吗?
http://msdn.microsoft.com/en -us/library/865tf5y6%28v=vs.80%29.aspx
http://forums.silverlight.net/t/22300.aspx/1
This will be more trick I think.Can you refer these links.
http://msdn.microsoft.com/en-us/library/865tf5y6%28v=vs.80%29.aspx
http://forums.silverlight.net/t/22300.aspx/1