首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >绑定PathGeometry时未设置StrokeStartLineCap

绑定PathGeometry时未设置StrokeStartLineCap
EN

Stack Overflow用户
提问于 2012-09-17 06:13:58
回答 1查看 885关注 0票数 2

XAML会产生预期的结果:一条两端是圆角的直线。

但是,将数据绑定到相同的PathGeometry会产生扁平的末端。我不知道为什么会这样,有人能解释一下吗?

下面是一个简化的示例:

XAML:

代码语言:javascript
复制
<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#:

代码语言:javascript
复制
    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;
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-17 07:44:23

如果将由XAML创建的Geometry与在后台代码中创建的XAML进行比较,则它们是不同的。

代码隐藏的那个有许多freezable使用"z“来关闭路径,而你的XAML没有...还有一个有PathFigureCollection,另一个没有...而且它将freezable属性设置为true。

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

我已经想出了另一种构建几何图形的方法,works...hopefully在你的情况下会有所帮助。

代码语言:javascript
复制
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,用于将使用路径标记语法描述路径的字符串转换为几何图形。

代码语言:javascript
复制
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;
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12451191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档