2

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

1 回答 1

1

如果您将GeometryXAML 创建的与在后面的代码中创建的进行比较,那么它们是不同的。

隐藏代码有许多不同之处......它使用“z”来关闭路径,而您的 XAML 没有......还有一个有 PathFigureCollection 另一个没有......它也设置了可冻结属性为真。

您需要尝试在代码隐藏中构建一个与 XAML 生成的相同......似乎构建几何以匹配它需要做很多工作。

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

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;
    }
}
于 2012-09-16T23:44:23.470 回答