绑定PathGeometry时未设置StrokeStartLineCap

时间:2012-09-16 22:13:58

标签: wpf pathgeometry

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;
    }

1 个答案:

答案 0 :(得分:1)

如果将XAML创建的Geometry与后面代码中创建的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; } } } 进行比较,那么它们就不同了。

代码隐藏的一个有许多不同之处......它使用“z”来关闭Path,而你的XAML没有......还有一个有一个PathFigureCollection而另一个没有....它还将freezable属性设置为true。

你需要尝试在代码隐藏中构建一个与XAML生成的代码相同的东西......构建几何图形似乎需要做很多工作才能匹配。

我想出了另一种构建几何图形的方法......希望这对你的情况有所帮助。

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;
    }
}

您也可以使用字符串作为属性,因为Data属性具有TypeConverter,可以使用路径标记语法将描述Path的字符串转换为几何。

{{1}}