WPF C# 路径:如何从带有路径数据的字符串获取到代码中的几何图形(不在 XAML 中)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2029680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:50:10  来源:igfitidea点击:

WPF C# Path: How to get from a string with Path Data to Geometry in Code (not in XAML)

c#wpfpath

提问by Peterdk

I want to generate a WPF Path object in Code.

我想在代码中生成一个 WPF 路径对象。

In XAML I can do this:

在 XAML 中,我可以这样做:

 <Path Data="M 100,200 C 100,25 400,350 400,175 H 280">

How can I do the same in Code?

我如何在代码中做同样的事情?

 Path path = new Path();
 Path.Data = "foo"; //This won't accept a string as path data.

Is there a class/Method available that converts the string with PathData to PathGeometry or similar?

是否有可用的类/方法将带有 PathData 的字符串转换为 PathGeometry 或类似的?

Surely somehow the XAML gets parsed and the Data-string converted?

肯定会以某种方式解析 XAML 并转换数据字符串?

采纳答案by Peterdk

var path = new Path();
path.Data = Geometry.Parse("M 100,200 C 100,25 400,350 400,175 H 280");

Path.Data is of type Geometry. Using ReflectorJustDecompile (eff Red Gate), I looked at the definition of Geometry for its TypeConverterAttribute (which the xaml serializer uses to convert values of type stringto Geometry). This pointed me to the GeometryConverter. Checking out the implementation, I saw that it uses Geometry.Parseto convert the string value of the path to a Geometry instance.

Path.Data 是几何类型。使用ReflectorJustDecompile (eff Red Gate),我查看了 Geometry 的 TypeConverterAttribute 定义(xaml 序列化程序使用它来将类型的值转换stringGeometry)。这将我指向了 GeometryConverter。查看实现,我看到它用于Geometry.Parse将路径的字符串值转换为 Geometry 实例。

回答by dbvega

You could use the binding mechanism.

您可以使用绑定机制。

var b = new Binding
{
   Source = "M 100,200 C 100,25 400,350 400,175 H 280"
};
BindingOperations.SetBinding(path, Path.DataProperty, b);

I hope it helps you.

我希望它能帮助你。

回答by Alexander Nikolaev

To make geometry from original text string You could use System.Windows.Media.FormattedText class with BuildGeometry() Method

从原始文本字符串制作几何图形您可以使用 System.Windows.Media.FormattedText 类和 BuildGeometry() 方法

 public  string Text2Path()
    {
        FormattedText formattedText = new System.Windows.Media.FormattedText("Any text you like",
            CultureInfo.GetCultureInfo("en-us"),
              FlowDirection.LeftToRight,
               new Typeface(
                    new FontFamily(),
                    FontStyles.Italic,
                    FontWeights.Bold,
                    FontStretches.Normal),
                    16, Brushes.Black);

        Geometry geometry = formattedText.BuildGeometry(new Point(0, 0));

        System.Windows.Shapes.Path path = new System.Windows.Shapes.Path();
        path.Data = geometry;

        string geometryAsString = geometry.GetFlattenedPathGeometry().ToString().Replace(",",".").Replace(";",",");
        return geometryAsString;
    }