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の1つにはありません...また、一方にはPathFigureCollectionがあり、もう一方にはありません....また、設定されていますfreezableプロパティをtrueに設定します。

コードビハインドで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 に答える