Creating a Programmatic Path in Silverlight (or WPF)

Download Project Files for Creating a Programmatic Path in Silverlight or WPF

(Note: The project is in Silverlight, but if you copy and paste the code into a WPF project, it should work without any changes)

Have you ever wanted to create a Path programmatically or create a Path in code behind or dynamically build a Path or hand code a Path in Silverlight or WPF?

Yes, I just said the same thing 4 different ways, but I’m trying to cast a wide net for Google to catch. One of the biggest troubles I have when I’m trying to do something new is that I don’t always know the proper terminology, so I try to think of all the ways people might want to ask the question.

I’ve been playing around with a good number of information visualization concepts recently and one thing that I’ve found that I needed was the ability to create a path programmatically.

One way to do that is to tackle and fully grok the Path data code system. (Good guides to it are here and here.) This system is really cool, but (unless I’ve missed something) they cannot be animated.

I wanted to build a path based off a data set, but that means that I have to build it programmatically. I didn’t find a lot of useful stuff on how to do this, so I thought I’d throw some out there.

First, because I’m a XAML guy, I’m going to provide a simple XAML example of a Path that can be animated:

<Path x:Name="MyPath" Fill="#FFA30000" Stroke="Black" Width="100" Height="100">
    <
Path.Data>
        <
PathGeometry FillRule="EvenOdd">
            <
PathFigure IsClosed="True" StartPoint="0,0">
                <
LineSegment Point="0,100"/>
                <
LineSegment Point="100,100"/>
                <
LineSegment Point="100,0"/>
            </
PathFigure>
        </
PathGeometry>
    </
Path.Data>
</
Path>

This path is a simple square and looks like this:

clip_image001

Now, let’s assume you want to recreate this programmatically using a collection of points that represent X,Y coordinates. If you’re using an ObservableCollection of Points (like I am) you can use the following method to return an the appropriate path.

Path createPath(ObservableCollection<Point> rawData)
{

    Path FinalPath = new Path();
    PathGeometry FinalPathGeometry = new PathGeometry();
    PathFigure PrimaryFigure = new PathFigure();

    //if you want the path to be a shape, you want to close the PathFigure
    //   that makes up the Path. If you want it to simply by a line, set
    //   PrimaryFigure.IsClosed = false;
   
PrimaryFigure.IsClosed = true;
   
    PrimaryFigure.StartPoint = rawData[0];

    PathSegmentCollection LineSegmentCollection = new PathSegmentCollection();

    for (int i = 1; i < rawData.Count; i++)
    {
        LineSegment newSegment = new LineSegment();
        newSegment.Point = rawData[i];
        LineSegmentCollection.Add(newSegment);
    }

    PrimaryFigure.Segments = LineSegmentCollection;
    FinalPathGeometry.Figures.Add(PrimaryFigure);
    FinalPath.Data = FinalPathGeometry;

    return FinalPath;
}

Once you have the path in place, you can just add it to your layout and it should show up. If you’re still confused, you can download the project that I created this in and walk through the rest of it yourself.

8 Comments

  1. [...] Creating a Programmatic Path in Silverlight (or WPF) – Mainly because I’m working on some good solid data visualization in Silverlight and one of the things that I need to create custom graphs is the ability to create dynamic vector paths. [...]

  2. Brian1 says:

    Very nice! I have need of a twist on your methods here, as I have need of something I call a “hole punch”. I have an image into which I would like to put a few holes. I have it working in XAML, but it cannot resize / scale. I believe that a programming solution would be in order. The XAML method will be to create a rectangle that is the size of the image; convert it into a path. Then create an ellipse somewhere inside; turn it into a path as well. Then combine the paths. (We can see the ellipse is a hole in the rectangle. The rectangle is white filled in my example and the ellipse shows the image peering through. NOT the desired effect.) Finally, make the combined path a Clipping path of the particular image and … presto, chango … a hole is punched into the image and we see the background through the hole the ellipse made. Very cool! My problem is that this hole is not stable when resizing or scaling the image. Bummer! So I hope that your coding method could provide my long awaited solution. Could I encourage you to take up this challenge, if it interests you? I find it fascinating. (Could you please email me if you are interested?)

  3. [...] Creating a Programmatic Path in Silverlight (or WPF) – Mainly because I’m working on some good solid data visualization in Silverlight and one of the things that I need to create custom graphs is the ability to create dynamic vector paths. [...]

  4. Dave says:

    Your Google net worked- you’re first in the list for “silverlight path programmatically”! ;)

  5. [...] Matthias Shapiro demonstrated how you create a geometry object using C# and XAML. [...]

  6. art says:

    Thanks for the good explanation. i was wondering how it can be done the other way arount, getting the points programaticaly from the path.

    if possible prease send me somme example how it can be donne.

  7. Stan Padillo says:

    Hey, I came across this blog article while looking for help with Microsoft Silverlight. I have recently changed internet browser from Google Chrome to Microsoft Internet Explorer 5. Just recently I seem to have a problem with loading sites that use Microsoft Silverlight. Everytime I go on a page that requires Microsoft Silverlight, the page does not load and I get a “npctrl.dll” error. I cannot seem to find out how to fix the problem. Any aid getting Microsoft Silverlight to function is greatly appreciated! Thanks

Leave a Reply