【发布时间】:2011-11-11 21:18:48
【问题描述】:
我正在开发 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());
}
}
}
【问题讨论】:
标签: wpf silverlight size measurement