【问题标题】:StrokeStartLineCap not set when PathGeometry is bound绑定 PathGeometry 时未设置 StrokeStartLineCap
【发布时间】:2012-09-16 22:13:58
【问题描述】:

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

【问题讨论】:

    标签: wpf pathgeometry


    【解决方案1】:

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

    后面的代码有许多不同之处...它使用“z”关闭路径,而您的 XAML 没有...还有一个有 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;
        }
    }
    

    【讨论】:

    • 谢谢 - 路径闭合和线条描边的组合是罪魁祸首,你的回答帮助我解决了这个问题。
    猜你喜欢
    • 2014-01-25
    • 1970-01-01
    • 2018-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-22
    • 2012-05-24
    相关资源
    最近更新 更多