tags:

views:

1316

answers:

1

I want to generate a WPF Path object in Code.

In XAML I can do this:

 <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?

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

+7  A: 
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 Reflector, I looked at the definition of Geometry for its TypeConverterAttribute (which the xaml serializer uses to convert values of type string to Geometry). This pointed me to the GeometryConverter. Checking out the implementation, I saw that it uses Geometry.Parse to convert the string value of the path to a Geometry instance.

Will
Great answer - not only did you answer correctly, but you also explained how you found the solution.
Ray Burns
Well, I took a minute to do a google search for the answer and didn't find anything reasonable. So, in cases like this (simple solutions must exist), I peeked at the code. Knowing how WPF moves from strings to complex types, I followed the lead. Understanding the process is of second importance to knowing the answer.
Will
That's a great answer, also lovely nice simple way to achieve the goal.
Peterdk