XAML会产生预期的结果:一条两端是圆角的直线。
但是,将数据绑定到相同的PathGeometry会产生扁平的末端。我不知道为什么会这样,有人能解释一下吗?
下面是一个简化的示例:
XAML:
<Grid>
<Path Fill="Green" Stroke="Black" StrokeThickness="8"
Stretch="None" IsHitTestVisible="False"
Data="{Binding IndicatorGeometry}"
StrokeStartLineCap="Round" StrokeEndLineCap="Round"/>
<!--<Path Fill="Green" Stroke="Black" StrokeThickness="8"
Stretch="None" IsHitTestVisible="False"
StrokeStartLineCap="Round" StrokeEndLineCap="Round">
<Path.Data>
<PathGeometry>
<PathFigure StartPoint="64,64">
<LineSegment Point="128,8"/>
</PathFigure>
</PathGeometry>
</Path.Data>
</Path>-->
</Grid>C#:
private static PathFigure[] ms_figure = new []
{
new PathFigure(
new Point(64, 64),
new[]
{
new LineSegment(new Point(128, 8), false)
},
true)
};
public PathGeometry IndicatorGeometry
{
get { return (PathGeometry)GetValue(IndicatorGeometryProperty); }
set { SetValue(IndicatorGeometryProperty, value); }
}
public static readonly DependencyProperty IndicatorGeometryProperty =
DependencyProperty.Register("IndicatorGeometry", typeof(PathGeometry), typeof(MainWindow),
new FrameworkPropertyMetadata(new PathGeometry(ms_figure)));
public MainWindow()
{
InitializeComponent();
DataContext = this;
}发布于 2012-09-17 07:44:23
如果将由XAML创建的Geometry与在后台代码中创建的XAML进行比较,则它们是不同的。
代码隐藏的那个有许多freezable使用"z“来关闭路径,而你的XAML没有...还有一个有PathFigureCollection,另一个没有...而且它将freezable属性设置为true。
您需要尝试在代码隐藏中构建一个与XAML生成的几何图形相同的one...it似乎要做很多工作才能构建匹配的几何图形。
我已经想出了另一种构建几何图形的方法,works...hopefully在你的情况下会有所帮助。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication4
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private static Geometry m_DefaultIndicatorGeometry = Geometry.Parse("M 64,64 L 128,8");
public Geometry IndicatorGeometry
{
get { return (Geometry)GetValue(IndicatorGeometryProperty); }
set { SetValue(IndicatorGeometryProperty, value); }
}
public static readonly DependencyProperty IndicatorGeometryProperty =
DependencyProperty.Register("IndicatorGeometry", typeof(Geometry), typeof(MainWindow),
new FrameworkPropertyMetadata(m_DefaultIndicatorGeometry));
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
}
}您也可以只使用字符串作为属性,因为Data属性有一个TypeConverter,用于将使用路径标记语法描述路径的字符串转换为几何图形。
public partial class MainWindow : Window
{
private static string m_DefaultIndicatorGeometry = "M 64,64 L 128,8";
public string IndicatorGeometry
{
get { return (string)GetValue(IndicatorGeometryProperty); }
set { SetValue(IndicatorGeometryProperty, value); }
}
public static readonly DependencyProperty IndicatorGeometryProperty =
DependencyProperty.Register("IndicatorGeometry", typeof(string), typeof(MainWindow),
new FrameworkPropertyMetadata(m_DefaultIndicatorGeometry));
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
}https://stackoverflow.com/questions/12451191
复制相似问题