79

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?

Peterdk
  • 15,625
  • 20
  • 101
  • 140

3 Answers3

154
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 JustDecompile (eff Red Gate), 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.

  • 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. –  Jan 08 '10 at 19:27
  • @Peterdk: Is there an alternative in WP7? How does a WP7 Path convert string to Geometry? –  Apr 25 '11 at 15:49
  • 3
    @Will See http://stackoverflow.com/questions/5596506/windows-phone-7-how-to-parse-bezier-path-string-like-in-xaml – Peterdk Apr 25 '11 at 19:36
  • @Nasenbaer: That's unfortunate. If you follow the same steps I did in order to determine how it is done on desktop WPF apps, you can find the answer for store apps. –  May 13 '14 at 15:03
20

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.

denys-vega
  • 3,522
  • 1
  • 19
  • 24
  • 3
    Turns out you have to do this for Windows Store and Phone apps. `Geometry.Parse` is in a namespace not supported in that profile. – Travis Nov 21 '13 at 00:43
4

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

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