<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Veracity Solutions Blog &#187; Programmatic Path</title>
	<atom:link href="http://blogs.veracitysolutions.com/tag/programmatic-path/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.veracitysolutions.com</link>
	<description>Software Consulting That Rocks</description>
	<lastBuildDate>Thu, 22 Apr 2010 17:21:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Creating a Programmatic Path in Silverlight (or WPF)</title>
		<link>http://blogs.veracitysolutions.com/creating-a-programmatic-path-in-silverlight-or-wpf/</link>
		<comments>http://blogs.veracitysolutions.com/creating-a-programmatic-path-in-silverlight-or-wpf/#comments</comments>
		<pubDate>Thu, 20 Aug 2009 01:59:19 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Advanced Tutorial]]></category>
		<category><![CDATA[How To...]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[Path]]></category>
		<category><![CDATA[Programmatic Path]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://blogs.veracitysolutions.com/?p=620</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p><a href='http://www.designersilverlight.com/wp-content/uploads/2009/08/ProgrammaticPathTutorial.zip'>Download Project Files for Creating a Programmatic Path in Silverlight or WPF</a> </p>
<p>(Note: The project is in Silverlight, but if you copy and paste the code into a WPF project, it should work without any changes)</p>
<p>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? </p>
<p>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.</p>
<p>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. </p>
<p>One way to do that is to tackle and fully grok the Path data code system. (Good guides to it are <a href="http://msdn.microsoft.com/en-us/library/cc189041(VS.95).aspx">here</a> and <a href="http://www.c-sharpcorner.com/UploadFile/mahesh/PathInSL03252009005946AM/PathInSL.aspx">here</a>.) This system is really cool, but (unless I’ve missed something) they cannot be animated. </p>
<p>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.</p>
<p>First, because I’m a XAML guy, I’m going to provide a simple XAML example of a Path that can be animated:</p>
<blockquote><p><span style="color: blue">&lt;</span><span style="color: #a31515">Path </span><span style="color: red">x</span><span style="color: blue">:</span><span style="color: red">Name</span><span style="color: blue">=&quot;MyPath&quot; </span><span style="color: red">Fill</span><span style="color: blue">=&quot;#FFA30000&quot; </span><span style="color: red">Stroke</span><span style="color: blue">=&quot;Black&quot; </span><span style="color: red">Width</span><span style="color: blue">=&quot;100&quot; </span><span style="color: red">Height</span><span style="color: blue">=&quot;100&quot;&gt;     <br />&#160;&#160;&#160; &lt;</span><span style="color: #a31515">Path.Data</span><span style="color: blue">&gt;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">PathGeometry </span><span style="color: red">FillRule</span><span style="color: blue">=&quot;EvenOdd&quot;&gt;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">PathFigure </span><span style="color: red">IsClosed</span><span style="color: blue">=&quot;True&quot; </span><span style="color: red">StartPoint</span><span style="color: blue">=&quot;0,0&quot;&gt;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">LineSegment </span><span style="color: red">Point</span><span style="color: blue">=&quot;0,100&quot;/&gt;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">LineSegment </span><span style="color: red">Point</span><span style="color: blue">=&quot;100,100&quot;/&gt;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;</span><span style="color: #a31515">LineSegment </span><span style="color: red">Point</span><span style="color: blue">=&quot;100,0&quot;/&gt;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">PathFigure</span><span style="color: blue">&gt;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">PathGeometry</span><span style="color: blue">&gt;     <br />&#160;&#160;&#160; &lt;/</span><span style="color: #a31515">Path.Data</span><span style="color: blue">&gt;     <br />&lt;/</span><span style="color: #a31515">Path</span><span style="color: blue">&gt;</span></p>
<p><span style="color: blue"></span> </p></blockquote>
<p>This path is a simple square and looks like this:</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip_image0012.png"><img style="border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px" border="0" alt="clip_image001" src="http://www.designersilverlight.com/wp-content/uploads/2009/08/clip_image001_thumb1.png" width="141" height="133" /></a></p>
<p>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.</p>
<blockquote><p><span style="color: #2b91af">Path </span>createPath(<span style="color: #2b91af">ObservableCollection</span>&lt;<span style="color: #2b91af">Point</span>&gt; rawData)    <br />{    </p>
<p>&#160;&#160;&#160; <span style="color: #2b91af">Path </span>FinalPath = <span style="color: blue">new </span><span style="color: #2b91af">Path</span>();    <br />&#160;&#160;&#160; <span style="color: #2b91af">PathGeometry </span>FinalPathGeometry = <span style="color: blue">new </span><span style="color: #2b91af">PathGeometry</span>();    <br />&#160;&#160;&#160; <span style="color: #2b91af">PathFigure </span>PrimaryFigure = <span style="color: blue">new </span><span style="color: #2b91af">PathFigure</span>();    </p>
<p>&#160;&#160;&#160; <span style="color: green">//if you want the path to be a shape, you want to close the PathFigure     <br />&#160;&#160;&#160; //&#160;&#160; that makes up the Path. If you want it to simply by a line, set      <br />&#160;&#160;&#160; //&#160;&#160; PrimaryFigure.IsClosed = false;      <br />&#160;&#160;&#160; </span>PrimaryFigure.IsClosed = <span style="color: blue">true</span>;    <br />&#160;&#160;&#160; <br />&#160;&#160;&#160; PrimaryFigure.StartPoint = rawData[0];    </p>
<p>&#160;&#160;&#160; <span style="color: #2b91af">PathSegmentCollection </span>LineSegmentCollection = <span style="color: blue">new </span><span style="color: #2b91af">PathSegmentCollection</span>();    </p>
<p>&#160;&#160;&#160; <span style="color: blue">for </span>(<span style="color: blue">int </span>i = 1; i &lt; rawData.Count; i++)    <br />&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #2b91af">LineSegment </span>newSegment = <span style="color: blue">new </span><span style="color: #2b91af">LineSegment</span>();    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; newSegment.Point = rawData[i];    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; LineSegmentCollection.Add(newSegment);    <br />&#160;&#160;&#160; }    </p>
<p>&#160;&#160;&#160; PrimaryFigure.Segments = LineSegmentCollection;    <br />&#160;&#160;&#160; FinalPathGeometry.Figures.Add(PrimaryFigure);    <br />&#160;&#160;&#160; FinalPath.Data = FinalPathGeometry;    </p>
<p>&#160;&#160;&#160; <span style="color: blue">return </span>FinalPath;    <br />}</p>
</blockquote>
<p>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. </p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.veracitysolutions.com/creating-a-programmatic-path-in-silverlight-or-wpf/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
