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

Pet*_*rdk 68 c# wpf path

我想在Code中生成一个WPF Path对象.

在XAML中,我可以这样做:

 <Path Data="M 100,200 C 100,25 400,350 400,175 H 280">
Run Code Online (Sandbox Code Playgroud)

我怎样才能在Code中做同样的事情?

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

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

当然不知何故XAML被解析并且数据字符串被转换了?

小智 132

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

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

  • 请注意,此解决方案不适用于WP7. (5认同)
  • @Will见http://stackoverflow.com/questions/5596506/windows-phone-7-how-to-parse-bezier-path-string-like-in-xaml (3认同)

dbv*_*ega 20

您可以使用绑定机制.

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

我希望它对你有所帮助.

  • 事实证明,您必须为Windows应用商店和手机应用执行此操作.`Geometry.Parse`位于该配置文件中不支持的命名空间中. (3认同)