<?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>Designer Silverlight &#187; Tutorial</title>
	<atom:link href="http://www.designersilverlight.com/category/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.designersilverlight.com</link>
	<description>Matthias Shapiro's Silverlight &#38; WPF Blog - Because Developers Get All The Good Blogs</description>
	<lastBuildDate>Sun, 05 Sep 2010 21:43:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Using WrapPanel and DockPanel in Windows Phone 7 With Blend</title>
		<link>http://www.designersilverlight.com/2010/06/28/using-wrappanel-and-dockpanel-in-windows-phone-7-with-blend/</link>
		<comments>http://www.designersilverlight.com/2010/06/28/using-wrappanel-and-dockpanel-in-windows-phone-7-with-blend/#comments</comments>
		<pubDate>Tue, 29 Jun 2010 04:30:59 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Blend]]></category>
		<category><![CDATA[Silverlight 3]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Windows Phone 7]]></category>
		<category><![CDATA[XAML]]></category>
		<category><![CDATA[DockPanel]]></category>
		<category><![CDATA[Panels]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[source code]]></category>
		<category><![CDATA[Windows Mobile]]></category>
		<category><![CDATA[WrapPanel]]></category>

		<guid isPermaLink="false">http://www.designersilverlight.com/2010/06/28/using-wrappanel-and-dockpanel-in-windows-phone-7-with-blend/</guid>
		<description><![CDATA[I’m currently working on porting the run-away hit mobile application ShopSavvy to Windows Phone 7 (sign up to be a beta tester) and one of the things I really wished I had was the WrapPanel. (For those who are new to Silverlight/WPF/Windows Phone 7, the WrapPanel stacks UI elements either horizontally or vertically like the [...]]]></description>
			<content:encoded><![CDATA[<p>I’m currently working on porting the run-away hit mobile application ShopSavvy to Windows Phone 7 (<a href="https://spur.wufoo.com/forms/shopsavvy-for-windows-phone-7/">sign up to be a beta tester</a>) and one of the things I really wished I had was the WrapPanel. </p>
<p>(For those who are new to Silverlight/WPF/Windows Phone 7, the WrapPanel stacks UI elements either horizontally or vertically like the StackPanel except that, when it hits the panel limit it… wait for it… wraps and starts stacking in a new column/row. Handy.)</p>
<p>I found a way to <a href="http://www.codebadger.com/blog/post/2010/03/20/Using-the-WrapPanel-from-the-Silverlight-Toolkit-on-Windows-Phone-7.aspx">add it with the Silverlight Toolkit</a>, but doing that added 140 Kb to my application. Maybe I’m just stuck in the world of J2ME, but adding 140 Kb to my mobile app isn’t OK if I can help it. I found <a href="http://www.designersilverlight.com/2010/06/28/using-wrappanel-and-dockpanel-in-windows-phone-7-with-blend/">this post from Jeff Wilcox</a> on adding the WrapPanel and that didn’t seem to work for me. (My comments are the whiny ones at the end of the post.)</p>
<p>But I finally got it working and I’ll tell you how. </p>
<p>Or I could just taunt you. That would be fun too. </p>
<p>Naw, I’m just kidding. Here’s the stuff you need.</p>
<p><a href="http://designersilverlight.com/wp-content/uploads/2010/06/WP7Panels.zip">Windows Phone 7 WrapPanel and DockPanel (Source</a>)</p>
<p><a href="http://designersilverlight.com/wp-content/uploads/2010/06/WP7PanelsDLL.zip">Windows Phone 7 WrapPanel and DockPanel (DLL)</a></p>
<p>All you have to do is download the DLL, unzip and reference in your app like so:</p>
<p>Right click on the “References” section of your project.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2010/06/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.designersilverlight.com/wp-content/uploads/2010/06/image_thumb.png" width="232" height="63" /></a>&#160;</p>
<p>Select the WP7Panels DLL from whatever folder you put it in and see it show up in your References</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.designersilverlight.com/wp-content/uploads/2010/06/image_thumb1.png" width="280" height="186" /></p>
<p>Open up the “Assets” section and type “wrap” or “dock” the wrap-or-dock panel should show up. Double click it or drag it onto the canvas to add it to your app. Blend will take care of the rest of the namespaces and everything. </p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2010/06/image1.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.designersilverlight.com/wp-content/uploads/2010/06/image_thumb2.png" width="338" height="121" /></a>&#160;</p>
<p>And start adding items to your panel. It should work the way it’s supposed to work.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2010/06/image2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.designersilverlight.com/wp-content/uploads/2010/06/image_thumb3.png" width="175" height="217" /></a> <a href="http://www.designersilverlight.com/wp-content/uploads/2010/06/image3.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.designersilverlight.com/wp-content/uploads/2010/06/image_thumb4.png" width="244" height="166" /></a></p>
<p>One note with the DockPanel… items placed in the DockPanel need to have the attached property</p>
<p><font color="#ff0000">[xmlns]:DockPanel.Dock</font>=”<font color="#0000ff">[something]</font>”</p>
<p>if you want them to do something other than align in the center. The [xmlns] name should be the same thing that is before the “DockPanel” in the XAML. It will most likely be “WP7Panels”. Therefore, the following XAML:</p>
<p><font color="#0000ff">&lt;</font><font color="#8b0000">WP7Panels:DockPanel</font><font color="#0000ff">&gt;</font>     <br />&#160;&#160;&#160; <font color="#0000ff">&lt;</font><font color="#800000">Button</font> <font color="#ff0000">Content</font>=&quot;<font color="#0000ff">Button</font>&quot; <font color="#ff0000">WP7Panels:DockPanel.Dock</font>=&quot;<font color="#0000ff">Bottom</font>&quot; <font color="#0000ff">/&gt;      <br /></font>&#160;&#160;&#160; <font color="#0000ff">&lt;</font><font color="#800000">Button</font> <font color="#ff0000">Content</font>=&quot;<font color="#0000ff">Button</font>&quot; <font color="#ff0000">WP7Panels:DockPanel.Dock</font>=&quot;<font color="#0000ff">Top</font>&quot; <font color="#0000ff">/&gt;      <br /></font>&#160;&#160;&#160; <font color="#0000ff">&lt;</font><font color="#800000">Button</font> <font color="#ff0000">Content</font>=&quot;<font color="#0000ff">Button</font>&quot; <font color="#ff0000">WP7Panels:DockPanel.Dock</font>=&quot;<font color="#0000ff">Right</font>&quot; <font color="#0000ff">/&gt;      <br /></font>&#160;&#160;&#160; <font color="#0000ff">&lt;</font><font color="#800000">Button</font> <font color="#ff0000">Content</font>=&quot;<font color="#0000ff">Button</font>&quot; <font color="#ff0000">WP7Panels:DockPanel.Dock</font>=&quot;<font color="#0000ff">Left</font>&quot; <font color="#0000ff">/&gt;      <br /></font>&#160;&#160;&#160; <font color="#0000ff">&lt;</font><font color="#800000">Button</font> <font color="#ff0000">Content</font>=&quot;<font color="#0000ff">Button</font>&quot; <font color="#0000ff">/&gt;      <br /><font color="#0000ff">&lt;/</font><font color="#8b0000">WP7Panels:DockPanel</font><font color="#0000ff">&gt;</font></font></p>
<p>…should look like this:</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2010/06/image4.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.designersilverlight.com/wp-content/uploads/2010/06/image_thumb5.png" width="397" height="321" /></a> </p>
<p align="left">If you want to add the actual files to your Windows Phone 7 app instead of just referencing the DLL (perhaps you are crazy or you have a deep and desperate need to fully understand absolutely everything you touch or you are a C++ developer), you can download the source. I’ve consolidated the DockPanel code into a single file (DockPanel.cs), but you’ll need the following files to get the WrapPanel working (<a href="http://designersilverlight.com/wp-content/uploads/2010/06/WP7Panels.zip">all included in the source</a>).</p>
<ul>
<li>LengthConverter.cs </li>
<li>NumericalExtensions.cs </li>
<li>OrientedSize.cs </li>
<li>TypeConverter.cs </li>
<li>WrapPanel.cs </li>
</ul>
<p>Finally, if you need something else for Windows Phone 7 from the Silverlight Toolkit, you can either add the entire toolkit (<a href="http://www.codebadger.com/blog/post/2010/03/20/Using-the-WrapPanel-from-the-Silverlight-Toolkit-on-Windows-Phone-7.aspx">explained here</a>) or download the <a href="http://silverlight.codeplex.com/SourceControl/changeset/changes/35261">source for the latest Silverlight 3 build</a> and open it in Visual Studio 2010 (for some reason, I couldn’t get it to open in Visual Studio 2008). It should update and you should be all set playing around in there to extract the stuff you need. </p>
<p>Good luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designersilverlight.com/2010/06/28/using-wrappanel-and-dockpanel-in-windows-phone-7-with-blend/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PHP, MySQL, and Silverlight: The Complete Tutorial (Part 3)</title>
		<link>http://www.designersilverlight.com/2010/05/24/php-mysql-and-silverlight-the-complete-tutorial-part-3/</link>
		<comments>http://www.designersilverlight.com/2010/05/24/php-mysql-and-silverlight-the-complete-tutorial-part-3/#comments</comments>
		<pubDate>Mon, 24 May 2010 19:30:58 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Binding]]></category>
		<category><![CDATA[Data Binding]]></category>
		<category><![CDATA[How To...]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[eventtrigger]]></category>

		<guid isPermaLink="false">http://www.designersilverlight.com/?p=970</guid>
		<description><![CDATA[This is meant to be the one-stop-shop blog post for creating a very simple web service in PHP that pulls from a MySQL database and displaying the data in Silverlight. Emphasis on the “simple”. Here is an example of the finished product (I reserve the right to clean up the data on a regular basis): [...]]]></description>
			<content:encoded><![CDATA[<p>This is meant to be the one-stop-shop blog post for creating a very simple web service in PHP that pulls from a MySQL database and displaying the data in Silverlight. Emphasis on the “simple”. Here is an example of the finished product (I reserve the right to clean up the data on a regular basis):</p>
<div id="silverlightControlHost">
		<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="500px" height="300px"><param name="source" value="http://designersilverlight.com/Silverlight/ToDo_List_Tutorial.xap"/><param name="onerror" value="onSilverlightError" /><param name="background" value="white" /><param name="minRuntimeVersion" value="3.0.40818.0" /><param name="autoUpgrade" value="true" /><a href="http://go.microsoft.com/fwlink/?LinkID=149156&#038;v=3.0.40818.0" style="text-decoration: none;"><br />
				<img src="http://go.microsoft.com/fwlink/?LinkID=161376" alt="Get Microsoft Silverlight" style="border-style: none"/><br />
			</a><br />
		</object><iframe id='_sl_historyFrame' style='visibility:hidden;height:0;width:0;border:0px'></iframe></div>
<p><a href="http://www.designersilverlight.com/2010/05/22/php-mysql-and-silverlight-the-complete-tutorial-part-1/">Part 1: MySQL</a></p>
<p><a href="http://www.designersilverlight.com/2010/05/23/php-mysql-and-silverlight-the-complete-tutorial-part-2/">Part 2: PHP</a></p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2010/05/AllFiles_PHP_and_Silverlight.zip">Download all files (PHP &#038; Silverlight)</a><br />
<a href="http://www.designersilverlight.com/wp-content/uploads/2010/05/PHP_Tutorial_Files.zip">Download PHP files only</a><br />
<a href="http://www.designersilverlight.com/wp-content/uploads/2010/05/ToDo_List_Tutorial.zip">Download Silverlight project only</a></p>
<p>In Part 1, we created the MySQL tables necessary to store data for a simple to-do list. In Part 2, we wrote a PHP service to deliver the items in our to-do list in JSON format. In Part 3, we’ll create a Silverlight application that can utilize this web service to retrieve, display, and edit the to-do list.</p>
<h2>Part 3: Connecting Silverlight To Our PHP Web Service</h2>
<p>First, let’s start a new Silverlight project. (You can download the full project here.) In the interest of time, I’m not going to go into the details of the “ideal” implementation for this. I’m just going to show the parts necessary to retrieve, display and interact with the to-do data.</p>
<p>Before we start with the UI, let’s build a model so that we can appropriately bind our data. Add a new folder to the Silverlight project and name it “Models”. Add a new class to it and name the class “ToDoItem.cs”. In this instance, we’re just going to make the model look just like our MySQL table.</p>
<p><strong>ToDoItem.cs</strong></p>
<p><span style="color: #0000ff; font-size: small;"> </span></p>
<p><span style="color: #0000ff; font-size: small;"><span style="font-family: Consolas;">p<span style="font-size: small;">ublic </span></span><span style="font-size: small;"><span style="font-family: Consolas;"><span style="color: #0000ff;">class</span><span style="color: #000000;"> </span><span style="color: #2b91af;">ToDoItem</span></span></span><br />
<span style="font-family: Consolas; font-size: small;">{<br />
</span><span style="font-size: small;"><span style="font-family: Consolas;"><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">bool</span><span style="color: #000000;"> isDone { </span><span style="color: #0000ff;">get</span><span style="color: #000000;">; </span><span style="color: #0000ff;">set</span></span></span><span style="color: #000000;"><span style="font-family: Consolas; font-size: small;">; }</span></span><br />
<span style="font-size: small;"><span style="font-family: Consolas;"><span style="color: #0000ff;"> public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">string</span><span style="color: #000000;"> toDoDescription {</span><span style="color: #0000ff;">get</span><span style="color: #000000;">; </span><span style="color: #0000ff;">set</span></span></span><span style="color: #000000;"><span style="font-family: Consolas; font-size: small;">;}</span></span><br />
<span style="font-size: small;"><span style="font-family: Consolas;"><span style="color: #0000ff;"> public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> toDoID {</span><span style="color: #0000ff;">get</span><span style="color: #000000;">; </span><span style="color: #0000ff;">set</span></span></span><span style="color: #000000;"><span style="font-family: Consolas; font-size: small;">;}</span></span><br />
<span style="font-family: Consolas; color: #0000ff; font-size: small;"><br />
public</span><span style="color: #000000;"><span style="font-family: Consolas; font-size: small;"> ToDoItem()</span></span> <span style="font-family: Consolas; font-size: small;">{ </span><span style="font-family: Consolas; font-size: small;">}<br />
</span><span style="font-family: Consolas; font-size: small;">}</span></span></p>
<p><span style="color: #0000ff; font-size: small;"><span style="font-family: Consolas; font-size: small;"> </span></span></p>
<p><span style="color: #0000ff; font-size: small;"> </span></p>
<p>Now, we’ll add to our MainPage.xaml a ListBox named “ToDoList” that will hold the to-do items and a Grid to house the UI to add a new to-do. We’ll work on gathering and displaying our items first.</p>
<p>Right-click on the ListBox and go to “<strong>Edit Additional Templates –&gt; Edit Generated Items (ItemTemplate) –&gt; Create Empty…</strong>”<br />
<a href="http://www.designersilverlight.com/wp-content/uploads/2010/05/clip_image0012.png"><img style="display: inline; border-width: 0px;" title="clip_image001" src="http://www.designersilverlight.com/wp-content/uploads/2010/05/clip_image001_thumb2.png" border="0" alt="clip_image001" width="634" height="94" /></a><br />
You’ll love this part… Just add a checkbox to it. That’s all we’ll need, so that’s all we’ll add. Then, go into the XAML and make the checkbox look like this:</p>
<p><span style="color: #0000ff; font-size: small;"> </span></p>
<p><span style="color: #0000ff; font-size: small;"><span style="font-family: Consolas; font-size: small;">&lt;</span><span style="font-size: small;"><span style="font-family: Consolas;"><span style="color: #a31515;">CheckBox</span><span style="color: #ff0000;"> Content</span><span style="color: #0000ff;">=&#8221;{</span><span style="color: #a31515;">Binding</span><span style="color: #ff0000;"> toDoDescription</span><span style="color: #0000ff;">}&#8221;<br />
</span></span></span><span style="font-size: small;"><span style="font-family: Consolas;"><span style="color: #ff0000;"> IsChecked</span><span style="color: #0000ff;">=&#8221;{</span><span style="color: #a31515;">Binding</span><span style="color: #ff0000;"> isDone</span><span style="color: #0000ff;">}&#8221;</span></span></span><span style="color: #000000;"> </span><br />
<span style="font-size: small;"><span style="font-family: Consolas;"><span style="color: #ff0000;"> Tag</span><span style="color: #0000ff;">=&#8221;{</span><span style="color: #a31515;">Binding</span><span style="color: #ff0000;"> toDoID</span><span style="color: #0000ff;">}&#8221; /&gt;</span></span></span></span></p>
<p><span style="color: #0000ff; font-size: small;"> </span></p>
<p><span style="font-family: Consolas; color: #0000ff; font-size: small;"> </span></p>
<p>This binding is all we’ll need once we gather our to-dos from the web service and attach it to the ListBox. Let’s go ahead and get that data into this ListBox.</p>
<p>Open MainPage.xaml.cs and, at the top of the class, add:</p>
<p><span style="font-size: small;"><span style="font-family: Consolas;"><span style="color: #2b91af;">WebClient</span><span style="color: #000000;"> wc = </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> </span><span style="color: #2b91af;">WebClient</span></span></span><span style="font-size: small;"><span style="font-family: Consolas;"><span style="color: #000000;">();<br />
</span><span style="color: #2b91af;">ObservableCollection</span><span style="color: #000000;">&lt;</span><span style="color: #2b91af;">ToDoItem</span><span style="color: #000000;">&gt; myToDoList = </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> </span><span style="color: #2b91af;">ObservableCollection</span><span style="color: #000000;">&lt;</span><span style="color: #2b91af;">ToDoItem</span></span></span><span style="font-size: small;"><span style="font-family: Consolas;"><span style="color: #000000;">&gt;();<br />
</span><span style="color: #0000ff;">string</span><span style="color: #000000;"> baseURI = </span><span style="color: #a31515;">&#8220;http://&lt;Web_address_holding_your_php_files&gt;&#8221;</span><span style="color: #000000;">;</span></span></span></p>
<p>You may have to add “using System.Net;” and “using System.Collections.ObjectModel;” to the top of the file. While we’re adding references, right-click on the “References” folder and find the “System.Json” component and add it to the project. Then add “using System.Json;” to the references section in the top.  It’ll come in handy in a minute.</p>
<p>In the MainPage() method under InitializeComponent(), type “wc.DownloadStringCompleted += “ and hit TAB twice. This will add an event handler for when the WebClient has finished connecting to the web service we wrote.</p>
<p>The resulting event handler (which should be named “wc_DownloadStringCompleted”), is the code that our application will go to whenever it makes a call to our web service and gets a result. In it, we will do the following things:</p>
<ol>
<li>Check to make sure we got a result without error</li>
<li>Check to see what kind of result we got (did we get all to-do items? or add a new to-do item?)</li>
<li>Walk through the result, extracting the data we need</li>
<li>Apply that data to the UI</li>
</ol>
<p>Let’s add the code to do that for getting all the items. Make your event handler look like this:</p>
<div id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:7934bf62-c08c-435e-980b-d18700e35c2b" class="wlWriterEditableSmartContent" style="margin: 0px; display: inline; float: none; padding: 0px;">
<div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt;">
<div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px;">wc_DownloadStringCompleted</div>
<div style="background: #ddd; max-height: 400px; overflow: auto;">
<ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;">
<li><span style="color: #0000ff;">void</span> wc_DownloadStringCompleted(<span style="color: #0000ff;">object</span> sender, <span style="color: #2b91af;">DownloadStringCompletedEventArgs</span> e)</li>
<li style="background: #f3f3f3;"> {</li>
<li> <span style="color: #0000ff;">if</span> (e.Error == <span style="color: #0000ff;">null</span> &amp;&amp; e.Result!= <span style="color: #a31515;">&#8220;&#8221;</span>)</li>
<li style="background: #f3f3f3;"> {</li>
<li> <span style="color: #2b91af;">JsonValue</span> completeResult = <span style="color: #2b91af;">JsonPrimitive</span>.Parse(e.Result);</li>
<li style="background: #f3f3f3;"> <span style="color: #0000ff;">string</span> resultType = completeResult[<span style="color: #a31515;">"returnType"</span>].ToString().Replace(<span style="color: #a31515;">&#8216;&#8221;&#8216;</span>, <span style="color: #a31515;">&#8216; &#8216;</span>).Trim();</li>
<li> <span style="color: #0000ff;">if</span> (resultType == <span style="color: #a31515;">&#8220;todoItems&#8221;</span>)</li>
<li style="background: #f3f3f3;"> {</li>
<li> myToDoList.Clear();</li>
<li style="background: #f3f3f3;"></li>
<li> <span style="color: #2b91af;">JsonArray</span> toDoItemsJson = (<span style="color: #2b91af;">JsonArray</span>)completeResult[<span style="color: #a31515;">"results"</span>];</li>
<li style="background: #f3f3f3;"> <span style="color: #0000ff;">foreach</span> (<span style="color: #2b91af;">JsonValue</span> todoItemJson <span style="color: #0000ff;">in</span> toDoItemsJson)</li>
<li> {</li>
<li style="background: #f3f3f3;"> <span style="color: #2b91af;">ToDoItem</span> thisTodo = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">ToDoItem</span>();</li>
<li></li>
<li style="background: #f3f3f3;"> <span style="color: #0000ff;">if</span> (todoItemJson[<span style="color: #a31515;">"ToDoIndex"</span>] != <span style="color: #0000ff;">null</span>)</li>
<li> thisTodo.toDoID = <span style="color: #2b91af;">Convert</span>.ToInt32(todoItemJson[<span style="color: #a31515;">"ToDoIndex"</span>].ToString().Replace(<span style="color: #a31515;">&#8216;&#8221;&#8216;</span>, <span style="color: #a31515;">&#8216; &#8216;</span>).Trim());</li>
<li style="background: #f3f3f3;"> <span style="color: #0000ff;">if</span> (todoItemJson[<span style="color: #a31515;">"TodoText"</span>] != <span style="color: #0000ff;">null</span>)</li>
<li> thisTodo.toDoDescription = todoItemJson[<span style="color: #a31515;">"TodoText"</span>].ToString().Replace(<span style="color: #a31515;">&#8216;&#8221;&#8216;</span>, <span style="color: #a31515;">&#8216; &#8216;</span>).Trim();</li>
<li style="background: #f3f3f3;"> <span style="color: #0000ff;">if</span> (todoItemJson[<span style="color: #a31515;">"IsDone"</span>] != <span style="color: #0000ff;">null</span>)</li>
<li> {</li>
<li style="background: #f3f3f3;"> <span style="color: #0000ff;">int</span> isDoneInt =  <span style="color: #2b91af;">Convert</span>.ToInt32(todoItemJson[<span style="color: #a31515;">"IsDone"</span>].ToString().Replace(<span style="color: #a31515;">&#8216;&#8221;&#8216;</span>, <span style="color: #a31515;">&#8216; &#8216;</span>).Trim());</li>
<li> <span style="color: #0000ff;">if</span>(isDoneInt == 0){</li>
<li style="background: #f3f3f3;"> thisTodo.isDone = <span style="color: #0000ff;">false</span>;</li>
<li> } <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (isDoneInt == 1){</li>
<li style="background: #f3f3f3;"> thisTodo.isDone = <span style="color: #0000ff;">true</span>;</li>
<li> }</li>
<li style="background: #f3f3f3;"> }</li>
<li> myToDoList.Add(thisTodo);</li>
<li style="background: #f3f3f3;"> }</li>
<li> ToDoList.ItemsSource = myToDoList;</li>
<li style="background: #f3f3f3;"> }</li>
<li> }</li>
<li style="background: #f3f3f3;"> }</li>
</ol>
</div>
</div>
</div>
<p>With this in place, all we need to do is make a call to the get_todo_items.php, which we can do by adding this code just below the wc.DownloadStringCompleted line in the MainPage() method.</p>
<p><span style="font-size: small;"><span style="font-family: Consolas;"><span style="color: #000000;">wc.DownloadStringAsync(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> </span><span style="color: #2b91af;">Uri</span><span style="color: #000000;">(baseURI + </span><span style="color: #a31515;">&#8220;get_todo_items.php&#8221;</span><span style="color: #000000;">));</span></span></span></p>
<p>When we run the project, we should get all objects from our database and the results should show up just the way we want in our Silverlight application. We will follow the exact same model to add new to-do items and update the to-do items we already have.</p>
<p>I’m in the process of separating out the final part of this tutorial (adding and updating the items) into a supplemental post so that I can wrap this post up. I encourage anyone walking through this to try to complete those parts by yourself and refer to the last bit only if you get stuck.</p>
<p>Finally, a little bit of warning. I structured this project to get it working in as straightforward a manner as possible. It is not in any way the ideal architectural example for a data-driven Silverlight application. It’s just a good way to get started using Silverlight in an environment that isn’t 100% Microsoft technologies.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designersilverlight.com/2010/05/24/php-mysql-and-silverlight-the-complete-tutorial-part-3/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>PHP, MySQL, and Silverlight: The Complete Tutorial (Part 2)</title>
		<link>http://www.designersilverlight.com/2010/05/23/php-mysql-and-silverlight-the-complete-tutorial-part-2/</link>
		<comments>http://www.designersilverlight.com/2010/05/23/php-mysql-and-silverlight-the-complete-tutorial-part-2/#comments</comments>
		<pubDate>Mon, 24 May 2010 00:07:35 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[JSON]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[web service]]></category>

		<guid isPermaLink="false">http://www.designersilverlight.com/?p=964</guid>
		<description><![CDATA[This is meant to be the one-stop-shop blog post for creating a very simple web service in PHP that pulls from a MySQL database and displaying the data in Silverlight. Emphasis on the “simple”. Here is an example of the finished product (I reserve the right to clean up the data on a regular basis): [...]]]></description>
			<content:encoded><![CDATA[<p>This is meant to be the one-stop-shop blog post for creating a very simple web service in PHP that pulls from a MySQL database and displaying the data in Silverlight. Emphasis on the “simple”.</p>
<p>Here is an example of the finished product (I reserve the right to clean up the data on a regular basis):</p>
<div id="silverlightControlHost">
		<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="500px" height="300px"><param name="source" value="http://designersilverlight.com/Silverlight/ToDo_List_Tutorial.xap"/><param name="onerror" value="onSilverlightError" /><param name="background" value="white" /><param name="minRuntimeVersion" value="3.0.40818.0" /><param name="autoUpgrade" value="true" /><a href="http://go.microsoft.com/fwlink/?LinkID=149156&#038;v=3.0.40818.0" style="text-decoration: none;"><br />
				<img src="http://go.microsoft.com/fwlink/?LinkID=161376" alt="Get Microsoft Silverlight" style="border-style: none"/><br />
			</a><br />
		</object><iframe id='_sl_historyFrame' style='visibility:hidden;height:0;width:0;border:0px'></iframe></div>
<p><a href="http://www.designersilverlight.com/2010/05/22/php-mysql-and-silverlight-the-complete-tutorial-part-1/">Part 1: MySQL</a></p>
<p><a href="http://www.designersilverlight.com/2010/05/24/php-mysql-and-silverlight-the-complete-tutorial-part-3/">Part 3: Silverlight</a></p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2010/05/AllFiles_PHP_and_Silverlight.zip">Download all files (PHP &#038; Silverlight)</a><br />
<a href="http://www.designersilverlight.com/wp-content/uploads/2010/05/PHP_Tutorial_Files.zip">Download PHP files only</a><br />
<a href="http://www.designersilverlight.com/wp-content/uploads/2010/05/ToDo_List_Tutorial.zip">Download Silverlight project only</a></p>
<p>In Part 1, we created the MySQL tables necessary to store data for a simple to-do list. In Part 2, we’ll write some PHP code that will give us the ability to grab the data out of the database and send it, in JSON format, to our Silverlight application.</p>
<h2>Part 2: Writing the PHP Web Service</h2>
<p>We’re going to create 4 PHP files (<a href="../wp-content/uploads/2010/05/PHP_Tutorial_Files.zip">download  php files</a>):</p>
<ul>
<li>mysql_vars.php – holds all the information for connecting to the MySQL database</li>
<li>get_todo_items.php – for getting all the to do items</li>
<li>add_todo_item.php – adds a to do item</li>
<li>change_status.php – changes a to-do item from “not done” to “done” or vice versa</li>
</ul>
<p>Since all these projects will be using the information in mysql_vars.php, we’ll write that first.</p>
<p><strong>mysql_vars.php</strong></p>
<p><span style="color: #ff0000;">&lt;?php</span><br />
$dbUsername = &#8220;<span style="color: #cc0000;">[my_database_username]</span>&#8220;;<br />
$dbPassword = &#8220;<span style="color: #cc0000;">[my_database_password]</span>&#8220;;<br />
$db = &#8220;<span style="color: #cc0000;">[my_database_name]</span>&#8220;;<br />
$server = &#8220;<span style="color: #cc0000;">[my_database_server]</span>&#8220;;</p>
<p><span style="color: #ff9900;"> //To Do Table and Column Names</span><br />
$mysql_todoTable  = &#8220;<span style="color: #cc0000;">to_do_data</span>&#8220;;<br />
$mysql_todoIndexCol = &#8220;<span style="color: #cc0000;">index_key</span>&#8220;;<br />
$mysql_isDoneCol = &#8220;<span style="color: #cc0000;">is_done</span>&#8220;;<br />
$mysql_todoTextCol = &#8220;<span style="color: #cc0000;">to_do_text</span>&#8220;;</p>
<p>$connection = <span style="color: #0000ff;">mysql_connect</span>($server, $dbUsername, $dbPassword);</p>
<p><span style="color: #0000ff;"> function</span> formatInput($rawURLData)<br />
{<br />
$returnString = <span style="color: #0000ff;">urldecode</span>($rawURLData);<br />
$returnString = <span style="color: #0000ff;">mysql_real_escape_string</span>($returnString);<br />
<span style="color: #006600;">return</span> $returnString;<br />
}<br />
<span style="color: #ff0000;">?&gt;</span></p>
<p>We’ve added the to-do table and column names so that, if we decide to change anything later, we can just go to this file and update the table or column once.</p>
<p>Just for good measure, we’ve added a function we’ll want to use across our php files. The function “formatInput” will be used to make sure all our data is decoded from the URL that calls our web service (the urldecode method) and then try to block any SQL injections (the mysql_real_escape_string method).</p>
<p>Now, let’s write the basic “Get the data” file. What we’re going to do is write it so that the we can choose to get:</p>
<ul>
<li>all the to-do items</li>
<li>all the to-do items that are “done”</li>
<li>all the to-do items that are “not done”</li>
</ul>
<p>This range of functionality isn’t even close to ideal. In a perfect world, we would want a wider range of options in gathering items (for example, items that contain a certain word or one item in particular or limit the number of items we call by a date range). However, for our very simple purposes, this will do.</p>
<p>The way our web service will work is that we have a URL that we’ll call from Silverlight when we want to get some data. When we calls this web service, we may want only the &#8220;not done&#8221; items or only the &#8220;done&#8221; items. We&#8217;ll handle that option by adding &#8220;?itemStatus=done&#8221; or &#8220;?itemStatus=notDone&#8221; to the end of the URL.</p>
<p>Example: if our base URL is</p>
<p><a href="http://www.mywebsite.com/">http://www.mywebsite.com/</a></p>
<p>the call to</p>
<p><a href="http://www.mywebsite.com/get_todo_items.php">http://www.mywebsite.com/get_todo_items.php</a></p>
<p>will get all items, regardless of their completed status while</p>
<p><a href="http://www.mywebsite.com/get_todo_items.php?itemStatus=done">http://www.mywebsite.com/get_todo_items.php?itemStatus=done</a></p>
<p>will get all the to do items that are complete. So we need to make sure that our web service responds appropriately to both calls.</p>
<p>There are comments in the code, but I&#8217;ll just explain the basic concept in picture form:</p>
<p>We take in a URL, extract the variables from it, create the MySQL query based on the variables, execute the MySQL query, extract the results, and then send back the php object encoded as a Json object. Each one of our files will follow this same pattern.</p>
<p><strong>get_todo_items.php</strong></p>
<p><span style="color: #ff0000;">&lt;?</span><br />
<span style="color: #008000;"> include</span> <span style="color: #cc0000;">&#8216;mysql_vars.php&#8217;</span>;</p>
<p><span style="color: #ff9900;"> //    set up the &#8220;itemStatus&#8221; URL option and build a query addition<br />
//        to account for the itemStatus variable<br />
</span>$itemStatus = <span style="color: #0066ff;">$_GET</span>[<span style="color: #cc0000;">'itemStatus'</span>];<br />
$itemQueryAddition = &#8220;&#8221;;</p>
<p><span style="color: #008000;"> if</span>($itemStatus <span style="color: #0000ff;">!=</span> <span style="color: #008000;">NULL</span>)<br />
{<br />
<span style="color: #008000;"> if</span>($itemStatus <span style="color: #0000ff;">==</span> <span style="color: #cc0000;">&#8220;done&#8221;</span>)<br />
{<br />
$itemQueryAddition = <span style="color: #cc0000;">&#8220;WHERE `$mysql_isDoneCol` = 1&#8243;</span>;<br />
} <span style="color: #008000;">else if</span> ($itemStatus == <span style="color: #cc0000;">&#8220;notDone&#8221;</span>)<br />
{<br />
$itemQueryAddition = <span style="color: #cc0000;">&#8220;WHERE `$mysql_isDoneCol` = 0&#8243;</span>;<br />
}</p>
<p><span style="color: #ff9900;"> // Construct our MySQL query</span><br />
$todoQuery = <span style="color: #cc0000;">&#8220;SELECT * FROM `$mysql_todoTable` $itemQueryAddition ;&#8221;</span>;</p>
<p><span style="color: #ff9900;"> // execute the query and gather the results&#8230;</span><br />
<span style="color: #0000ff;">mysql_select_db</span>($db, $connection);<br />
$todoResult =<span style="color: #0000ff;"> mysql_query</span>($todoQuery);<br />
$todoArray = <span style="color: #0000ff;">array()</span>;</p>
<p><span style="color: #008000;"> while</span>($itemRow <span style="color: #0000ff;">= mysql_fetch_array</span>($todoResult))<br />
{<br />
$todoArray<span style="color: #0000ff;">[] = array(</span> <span style="color: #cc0000;">&#8220;ToDoIndex&#8221;</span> <span style="color: #0000ff;">=&gt;</span> $itemRow[<span style="color: #cc0000;">'index_key'</span>],<br />
<span style="color: #cc0000;">&#8220;IsDone&#8221;</span> <span style="color: #0000ff;">=&gt;</span> $itemRow[<span style="color: #cc0000;">'is_done'</span>],<br />
<span style="color: #cc0000;">&#8220;TodoText&#8221;</span> <span style="color: #0000ff;">=&gt;</span> $itemRow[<span style="color: #cc0000;">'to_do_text'</span>] );<br />
}<br />
<span style="color: #0000ff;">mysql_close</span>($connection);</p>
<p><span style="color: #ff9900;"> // &#8230; then encode the results as JSON Text&#8230;<br />
//   we&#8217;re using a &#8220;returnType&#8221; field so that our Silverlight application can differentiate between<br />
//   the kind of return values it recieves and parse the Json object appropriately</span></p>
<p>$returnItems = <span style="color: #0000ff;">array(</span> <span style="color: #cc0000;">&#8220;returnType&#8221;</span> =&gt; &#8220;todoItems&#8221;,<br />
<span style="color: #cc0000;">&#8220;results&#8221;</span> =&gt; $returnItems);<br />
$JSONResult = json_encode($todoArray);</p>
<p><span style="color: #ff9900;"> // &#8230; and print the results so that our app can read them<br />
</span> <span style="color: #0000ff;">echo</span> $JSONResult;<br />
<span style="color: #ff0000;">?&gt;</span></p>
<p>The other two files,<strong> add_todo_item.php</strong> and <strong>change_status.php</strong> use exactly the same structure to add a new item and change the status of an existing item (respectively). I won’t put all the code here in this post that is already too long, but you can download all the files here.</p>
<p>Update the mysql_vars.php file to fit your needs and you should be able to just upload these files and have your running to-do web service all ready for Silverlight to call it for data, which is something we’ll deal with in Part 3.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designersilverlight.com/2010/05/23/php-mysql-and-silverlight-the-complete-tutorial-part-2/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>PHP, MySQL and Silverlight: The Complete Tutorial (Part 1)</title>
		<link>http://www.designersilverlight.com/2010/05/23/php-mysql-and-silverlight-the-complete-tutorial-part-1/</link>
		<comments>http://www.designersilverlight.com/2010/05/23/php-mysql-and-silverlight-the-complete-tutorial-part-1/#comments</comments>
		<pubDate>Sun, 23 May 2010 18:56:25 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[database]]></category>

		<guid isPermaLink="false">http://www.designersilverlight.com/?p=960</guid>
		<description><![CDATA[This is meant to be the one-stop-shop blog post for creating a very simple web service in PHP that pulls from a MySQL database and displaying the data in Silverlight. Emphasis on the “simple”. This tutorial is geared toward someone who has never done databases or web services. Here is an example of the finished [...]]]></description>
			<content:encoded><![CDATA[<p>This is meant to be the one-stop-shop blog post for creating a very simple web service in PHP that pulls from a MySQL database and displaying the data in Silverlight. Emphasis on the “simple”. This tutorial is geared toward someone who has never done databases or web services.</p>
<p>Here is an example of the finished product (I reserve the right to clean up the data on a regular basis):</p>
<div id="silverlightControlHost">
		<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="500px" height="300px"><param name="source" value="http://designersilverlight.com/Silverlight/ToDo_List_Tutorial.xap"/><param name="onerror" value="onSilverlightError" /><param name="background" value="white" /><param name="minRuntimeVersion" value="3.0.40818.0" /><param name="autoUpgrade" value="true" /><a href="http://go.microsoft.com/fwlink/?LinkID=149156&#038;v=3.0.40818.0" style="text-decoration: none;"><br />
				<img src="http://go.microsoft.com/fwlink/?LinkID=161376" alt="Get Microsoft Silverlight" style="border-style: none"/><br />
			</a><br />
		</object><iframe id='_sl_historyFrame' style='visibility:hidden;height:0;width:0;border:0px'></iframe></div>
<p>This tutorial will walk through the steps to create a simple to-do list. Our to-do list will hold text of what it is we need to do and a value indicating if the task has been done. In this tutorial, we will create a MySQL table to hold our data, a PHP service to call the data and a Silverlight application to display and interact with the data.</p>
<p><a href="http://www.designersilverlight.com/2010/05/23/php-mysql-and-silverlight-the-complete-tutorial-part-2/">Part 2: PHP</a></p>
<p><a href="http://www.designersilverlight.com/2010/05/24/php-mysql-and-silverlight-the-complete-tutorial-part-3/">Part 3: Silverlight</a></p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2010/05/AllFiles_PHP_and_Silverlight.zip">Download all files (PHP &#038; Silverlight)</a><br />
<a href="http://www.designersilverlight.com/wp-content/uploads/2010/05/PHP_Tutorial_Files.zip">Download PHP files only</a><br />
<a href="http://www.designersilverlight.com/wp-content/uploads/2010/05/ToDo_List_Tutorial.zip">Download Silverlight project only</a></p>
<h2>Part 1: The MySQL Database</h2>
<p>First, let’s create the table we need for our data.</p>
<p>If you don’t have or don’t like phpMyAdmin, the MySQL query to create the table described below is:</p>
<p>CREATE TABLE `[your_database_name]`.`to_do_data` (<br />
`index_key` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,<br />
`is_done` TINYINT( 4 ) NOT NULL DEFAULT &#8217;0&#8242;,<br />
`to_do_text` TEXT NOT NULL ,<br />
FULLTEXT ( `to_do_text` )<br />
) ENGINE = MYISAM</p>
<p>If you have phpMyAdmin, I highly suggest using it if you’re a MySQL novice. In phpMyAdmin, go to your database and enter the name of the table you want to create in the “Create new table on database [your_db_name]” section.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2010/05/clip_image0015.png"><img style="display: inline; border: 0px;" title="clip_image001[5]" src="http://www.designersilverlight.com/wp-content/uploads/2010/05/clip_image0015_thumb.png" border="0" alt="clip_image001[5]" width="445" height="105" /></a></p>
<p>We’ll call our table “to_do_data” and give it 3 fields.</p>
<ul>
<li>index_key – identifies the to-do item uniquely</li>
<li>is_done  &#8211; true/false value indicates the status of the to-do item</li>
<li>to_do_text &#8211; a short text to describe what needs to be done</li>
</ul>
<p>In phpMyAdmin, it will look like this:</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2010/05/clip_image0011.png"><img style="display: inline; border: 0px;" title="clip_image001" src="http://www.designersilverlight.com/wp-content/uploads/2010/05/clip_image001_thumb1.png" border="0" alt="clip_image001" width="735" height="343" /></a></p>
<p>A couple of notes about the fields:</p>
<p><strong>index_key</strong></p>
<p>This is the primary key of the table and is used to uniquely identify the given row. As such, it cannot be null and it auto-increments as rows are added to the table.</p>
<p><strong>is_done</strong></p>
<p>We use “TINYINT” type for this value because <a href="http://stackoverflow.com/questions/289727/which-mysql-datatype-to-use-for-storing-boolean-values-from-to-php">using BOOLEAN is basically the same thing</a>. It is not null because every item must either be “done” or “not done”. “Done” = true = 1 and “not done” = false = 0. We are going to default to “0” (false) because we’re assuming that users aren’t going to make a to do list of stuff that is already complete.</p>
<p><strong>to_do_text</strong></p>
<p>We have artificially limited the text size to 1024 because we assume that this will be a set of short to-dos to, not a set of journal entries. We’ve also turned on “Fulltext” which lets the MySQL database index our entries for quick searching.</p>
<p>Now we just click the “Save” button at the bottom and we have our table.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2010/05/clip_image0017.png"><img style="display: inline; border: 0px;" title="clip_image001[7]" src="http://www.designersilverlight.com/wp-content/uploads/2010/05/clip_image0017_thumb.png" border="0" alt="clip_image001[7]" width="794" height="105" /></a></p>
<p>Now our database system is in place and we’re ready to write a PHP webservice to implement all the CRUD (create, read, update, delete) capabilities our system will need.</p>
<p>If you want to learn more about MySQL, I highly recommend &#8220;<a href="http://www.amazon.com/gp/product/0471412767?ie=UTF8&#038;tag=irrationalopt-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0471412767">A Visual Introduction to SQL</a><img src="http://www.assoc-amazon.com/e/ir?t=irrationalopt-20&#038;l=as2&#038;o=1&#038;a=0471412767" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />&#8220;. This is the book I have, a left-over of my grad school years and it is an exceptional book for picking your way through various database scenarios. </p>
<p>If you&#8217;re looking for something a bit less costly, &#8220;<a href="http://www.amazon.com/gp/product/0672327120?ie=UTF8&#038;tag=irrationalopt-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0672327120">MySQL Crash Course</a><img src="http://www.assoc-amazon.com/e/ir?t=irrationalopt-20&#038;l=as2&#038;o=1&#038;a=0672327120" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />&#8221; is a fine book on the subject as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designersilverlight.com/2010/05/23/php-mysql-and-silverlight-the-complete-tutorial-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New Silverlight Posts at Veracity Blogs</title>
		<link>http://www.designersilverlight.com/2009/08/19/new-silverlight-posts-at-veracity-blogs/</link>
		<comments>http://www.designersilverlight.com/2009/08/19/new-silverlight-posts-at-veracity-blogs/#comments</comments>
		<pubDate>Wed, 19 Aug 2009 16:09:27 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[How To...]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[styles]]></category>
		<category><![CDATA[templates]]></category>

		<guid isPermaLink="false">http://www.designersilverlight.com/?p=732</guid>
		<description><![CDATA[I&#8217;ve started a series of posts on Silverlight and other topics over at the Veracity Solutions Blog. Veracity Solutions is the software consulting firm where I work and we&#8217;ve decided that it would be a good idea to do some sponsored posting there. Blog posts up so far are: Styling a ComboBox in Silverlight 3 [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve started a series of posts on Silverlight and other topics over at the Veracity Solutions Blog.</p>
<p>Veracity Solutions is the software consulting firm where I work and we&#8217;ve decided that it would be a good idea to do some sponsored posting there.</p>
<p>Blog posts up so far are:</p>
<ul>
<li><a href="http://blogs.veracitysolutions.com/styling-a-combobox-in-silverlight-3/">Styling a ComboBox in Silverlight 3</a></li>
<li><a href="http://blogs.veracitysolutions.com/how-to-wrap-text-in-a-silverlight-3-combobox/">How To Wrap Text In a Silverlight 3 ComboBox</a></li>
<li><a href="http://blogs.veracitysolutions.com/how-to-get-a-silverlight-3-autocompletebox-to-show-sample-data-in-blend-3/">How To Get a Silverlight 3 AutoCompleteBox to Show Sample Data In Blend 3</a></li>
<li><a href="http://blogs.veracitysolutions.com/how-to-create-a-listbox-with-checkboxes-or-radiobuttons-in-silverlight-3/">How To Create A ListBox with CheckBoxes (or RadioButtons) In Silverlight 3</a></li>
</ul>
<p>More are coming, including tutorials on behaviors and paths.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designersilverlight.com/2009/08/19/new-silverlight-posts-at-veracity-blogs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adventures with JSON and Silverlight (and the New York Times)</title>
		<link>http://www.designersilverlight.com/2009/02/25/adventures-with-json-and-silverlight-and-the-new-york-times/</link>
		<comments>http://www.designersilverlight.com/2009/02/25/adventures-with-json-and-silverlight-and-the-new-york-times/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 00:17:56 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Advanced Tutorial]]></category>
		<category><![CDATA[Blend]]></category>
		<category><![CDATA[How To...]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[New York Times API]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.designersilverlight.com/2009/02/25/adventures-with-json-and-silverlight-and-the-new-york-times/</guid>
		<description><![CDATA[Summary: In this post, I walk through the basics of using Silverlight to query the New York Times Article API and display the results of the query. You can see the final result below. You can also download source code for this project here. JSON/Silverlight/New York Times project files Huge thanks to Josh Holmes, whose [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Summary:</strong> In this post, I walk through the basics of using Silverlight to query the New York Times Article API and display the results of the query. You can see the final result below.</p>
<p><iframe src="http://silverlight.services.live.com/invoke/77530/JSON_NYT_Tutorial_Part_1/iframe.html" scrolling="no" frameborder="0" style="width:400px; height:300px"></iframe></p>
<p>You can also download source code for this project here.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/02/jsonnewyorktimestutorial_part_1.zip">JSON/Silverlight/New York Times project files </a></p>
<p>Huge thanks to Josh Holmes, whose <a href="http://www.joshholmes.com/blog/2009/01/20/PlayingWithJSON.aspx">JSON/Silverlight tutorial</a> was the base of much of this project.</p>
<p>This project is somewhat code-intensive (and kind of long, expect 30-60 minutes), so if you just want the utility provided here without any of the work, you can skip over to <a href="http://www.designersilverlight.com/2009/02/25/using-silverlight-to-display-json-data-collected-from-the-new-york-times-api/">my post on displaying the results</a>, which is strictly a Blend tutorial.</p>
<p>However, I recommend walking through this one since it will help get you to a point of pulling real data from an API, which I’ve found to be a wonderful help as I practice putting together data-based designs. One of the things I&#8217;ve been wanting to be able to do as a designer is to explore a data set easily and quickly so that I can have data to play with in my interfaces. I found exactly what I wanted in the New York Times API, but then I found out I had to learn JSON.</p>
<p>&#8220;Oh great,&#8221; I said to myself, &#8220;another technology for me to learn.&#8221; But it turned out that I didn&#8217;t actually have to learn that much, because Visual Studio does nearly all the work for me.</p>
<h3>Super Quick Introduction to JSON</h3>
<p>If you&#8217;re not interested and you want to get to the business of grabbing New York Times data, you can skip it . It is helpful, but not strictly necessary.</p>
<p>JSON stands for JavaScript Object Notation and is basically just a really handy way to pass data along in a web service. It is very simple&#8230; within a set of curly brackets, you will have name/value pairs separated by a colon with each piece of data.<br />
Example:<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;FirstName : &#8220;Matthias&#8221;,<br />
&nbsp;&nbsp;&nbsp;&nbsp;LastName : &#8220;Shapiro&#8221;,<br />
&nbsp;&nbsp;&nbsp;&nbsp;Blog : &#8220;Designer WPF&#8221;<br />
}</p>
<p>This is a JSON object. Arrays are created by using square brackets and JSON obejcts can be placed into a Javascript var. These things are not really related in anyway, but putting them in the same sentence allows me to only write one more example instead of two</p>
<p>Example:<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;FirstName: “Matthias”,<br />
&nbsp;&nbsp;&nbsp;&nbsp;LastName: “Shapiro”,<br />
&nbsp;&nbsp;&nbsp;&nbsp;Siblings : [<br />
&nbsp;&nbsp;&nbsp;&nbsp;{Name : “Abby”},<br />
&nbsp;&nbsp;&nbsp;&nbsp;{Name : “Joel”},<br />
&nbsp;&nbsp;&nbsp;&nbsp;{Name : “Anna”},<br />
&nbsp;&nbsp;&nbsp;&nbsp;{Name : “John”},<br />
&nbsp;&nbsp;&nbsp;&nbsp;{Name : “Nate”}<br />
&nbsp;&nbsp;&nbsp;&nbsp;]<br />
}</p>
<p>And there we have the basics of JSON.</p>
<h3>End of Super Quick Introduction to JSON</h3>
<p>What is so awesome about JSON and Silverlight is that Visual Studio 2008 has a set of JSON-friendly classes that make working with JSON a breeze. Which is really handy because the New York Times, which is a dream come true for the new data-gatherer, delivers JSON results. So let’s walk through making a call to the New York Times Article API, handling the data we get back, and putting it into a Listbox for viewing.</p>
<p>First, <a href="http://developer.nytimes.com/">go to the New York Times Developer site</a>, log in (or register) and get your API key.</p>
<p>Next, start a new Silverlight project in Visual Studio 2008. I named mine “JSONNewYorkTimesTutorial”.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image0019.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001[9]" src="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image0019-thumb.png" border="0" alt="clip_image001[9]" width="659" height="424" /></a></p>
<p>Open your new project in Blend, pull up Page.xaml and add a TextBlock, ListBox, a TextBox and a Button. Name the ListBox “ResultsDisplay” and the TextBox “SearchText”.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image00113.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001[13]" src="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image00113-thumb.png" border="0" alt="clip_image001[13]" width="299" height="105" /></a></p>
<p>Now, my Page.xaml looks like this.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image00111.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001[11]" src="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image00111-thumb.png" border="0" alt="clip_image001[11]" width="457" height="346" /></a></p>
<p>OK… now let’s go to the code-behind for our project go to the event section of the button in Blend and type “PerformQuery” into the “Click” event.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image00115.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001[15]" src="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image00115-thumb.png" border="0" alt="clip_image001[15]" width="264" height="104" /></a></p>
<p>This will automatically insert the necessary code into the code-behind, so pull up Visual Studio. Before we implement a call to the NYT API, lets add some useful stuff. If you have not yet gone to <a href="http://developer.nytimes.com/apps/register?authChecked=1">get your Developer key, do so now</a>.</p>
<p><span style="color: blue">private string </span>myApiKey;<br />
<span style="color: blue">private </span><span style="color: #2b91af">WebClient </span>callNYT;</p>
<p><span style="color: blue">public </span>Page()<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;InitializeComponent();<br />
&nbsp;&nbsp;&nbsp;&nbsp;myApiKey = <span style="color: #a31515">&#8220;&amp;api-key=(put your api key here. No, you may not have mine)&#8221;</span>;<br />
&nbsp;&nbsp;&nbsp;&nbsp;callNYT = <span style="color: blue">new </span><span style="color: #2b91af">WebClient</span>();<br />
&nbsp;&nbsp;&nbsp;&nbsp;callNYT.OpenReadCompleted += <span style="color: blue">new </span><span style="color: #2b91af">OpenReadCompletedEventHandler</span>(callNYT_OpenReadCompleted);<br />
}</p>
<p>In the code above, we’ve created a string that we can use to apply our unique NYT API key and we’ve created an instance of WebClient to call and receive information from the NYT API. When an object from the NYT API has been received , it will call the OpenReadCompleted event, which will be handled by our callNYT_OpenReadCompleted method.</p>
<p>(By the way, if you’re not getting the proper intellisense for the “Web Client” part, add “<span style="color: blue">using </span>System.Net;&#8221; to the top of your file.)</p>
<p>Now on to our button method. There are tons of things we can add to our query to find the exact information we want. But in the interest of simplicity, this post will deal only with a simple text search. To that end, let’s go to our PerformQuery method and turn it into this:</p>
<p><span style="color: blue">private void </span>PerformQuery(<span style="color: blue">object </span>sender, <span style="color: #2b91af">RoutedEventArgs </span>e)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">string </span>NYTQueryBase = <span style="color: #a31515">&#8220;http://api.nytimes.com/svc/search/v1/article&#8221;</span>;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">string </span>SearchTerm = <span style="color: #a31515">&#8220;?query=&#8221;</span>+ SearchText.Text;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af">Uri </span>queryUri = <span style="color: blue">new </span><span style="color: #2b91af">Uri</span>(NYTQueryBase + SearchTerm + myApiKey);</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;callNYT.OpenReadAsync(queryUri);<br />
}</p>
<p>We’ve done two things here. The first is that we built our search query using the query base, the query string and our API key. That was simple enough.</p>
<p>Next, we’ve going to use the WebClient we created to call our new query. When the query has been completed, our program will run the callNYT_OpenReadCompleted method with its result, which will be a JSON object constructed by the NYT servers. We will get back a JSON object with the following:</p>
<ul>
<li>offset – We will get 10 results per page. The offset tells us which page of the results we’re on. The default is 0, which gives us results 0 – 9.</li>
<li>tokens – this is a array of our search terms.</li>
<li>total – this is an integer indicating of how many results there were for our search.</li>
<li>results – this is an array of results with the following format
<ul>
<li>body – a portion of the beginning of the article</li>
<li>byline – the article byline, usually including the author name</li>
<li>date – the date the article appeared, in a “yyyymmdd” format. For example, today would be “20090225”.</li>
<li>title – the article title</li>
<li>url – a url link to the article</li>
</ul>
</li>
</ul>
<p>A quick note: The NYT API is extremely flexible and we can actually define how we want our results to come back. This is just the default result format for the purposes of demonstration.</p>
<p>Before we handle this object, we want to create a class for the results. Right click on your project and go to “Add –&gt; Class…”. Name your new class “NYTResult.cs” and make sure it looks like this:</p>
<p><span style="color: blue">public class </span><span style="color: #2b91af">NYTResult </span>{<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">public string </span>Body { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">public string </span>Byline { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">public </span><span style="color: #2b91af">DateTime </span>Date { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">public string </span>Title { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">public </span><span style="color: #2b91af">Uri </span>Url { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }<br />
}</p>
<p>I added the following method to the class to handle the date conversion from the NYT format to a .NET DateTime object.</p>
<p><span style="color: blue">public </span><span style="color: #2b91af">DateTime </span>formattedDateTime(<span style="color: blue">string </span>NYT_Time)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue"><span style="color: #444444;"> </span>int </span>year = <span style="color: #2b91af">Convert</span>.ToInt32(NYT_Time.Substring(0, 4));<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">int </span>month = <span style="color: #2b91af">Convert</span>.ToInt32(NYT_Time.Substring(4, 2));<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">int </span>day = <span style="color: #2b91af">Convert</span>.ToInt32(NYT_Time.Substring(6, 2));<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af">DateTime </span>finalDateTime = <span style="color: blue">new </span><span style="color: #2b91af">DateTime</span>(year, month, day);<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">return </span>finalDateTime;<br />
}</p>
<p>OK… now we’re really ready to handle the JSON object. Right click on the references in your project and select “Add Reference…”</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image001.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001" src="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image001-thumb.png" border="0" alt="clip_image001" width="287" height="81" /></a></p>
<p>In  your “Add References” box, select “System.Json” and click “OK”.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image0015.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001[5]" src="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image0015-thumb.png" border="0" alt="clip_image001[5]" width="481" height="408" /></a></p>
<p>Add “<span style="color: blue">using </span>System.Json;” to the references in your Page.xaml.cs file. And, just for good measure, add “<span style="color: blue">using </span>System.Collections.ObjectModel;” as well.</p>
<p>Go to the callNYT_OpenReadCompleted method and enter the following. I’ve tried to comment the code so that I don’t need to further explain it. Side note: I’m not always the best at understanding what is self-explanatory and what I need to elaborate on. If there are any additional questions, post them in the comments and I’ll answer as quickly as I can.</p>
<p><span style="color: blue">void </span>callNYT_OpenReadCompleted(<span style="color: blue">object </span>sender, <span style="color: #2b91af">OpenReadCompletedEventArgs </span>e)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green">//grab our result and make a JSON Object out of it<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span> <span style="color: green">//    then extract the results array from that object<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span> <span style="color: #2b91af">JsonObject </span>completeResult = (<span style="color: #2b91af">JsonObject</span>)<span style="color: #2b91af">JsonObject</span>.Load(e.Result);<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af">JsonArray </span>resultsArray = (<span style="color: #2b91af">JsonArray</span>)completeResult[<span style="color: #a31515">"results"</span>];</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green">//an observable collection to hold the data and attach it to our ListBox<br />
</span><br />
&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #2b91af">ObservableCollection</span>&lt;<span style="color: #2b91af">NYTResult</span>&gt; resultCollection = <span style="color: blue">new </span><span style="color: #2b91af">ObservableCollection</span>&lt;<span style="color: #2b91af">NYTResult</span>&gt;();</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green">//iterate through the results and transfer the data from a<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span> <span style="color: green">//   JSON object into our nice pretty .NET object<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span> <span style="color: blue">foreach </span>(<span style="color: #2b91af">JsonObject </span>NYTRawResult <span style="color: blue">in </span>resultsArray)<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #2b91af">NYTResult </span>singleResult = <span style="color: blue">new </span><span style="color: #2b91af">NYTResult</span>();</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green">//don&#8217;t forget to check your results&#8230; an article might not have a<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span> <span style="color: green">//  byline or a link<br />
</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">if </span>(NYTRawResult.Keys.Contains(<span style="color: #a31515">&#8220;body&#8221;</span>))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleResult.Body = NYTRawResult[<span style="color: #a31515">"body"</span>];<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">if</span>(NYTRawResult.Keys.Contains(<span style="color: #a31515">&#8220;byline&#8221;</span>))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleResult.Byline = NYTRawResult[<span style="color: #a31515">"byline"</span>];<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">if </span>(NYTRawResult.Keys.Contains(<span style="color: #a31515">&#8220;date&#8221;</span>))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleResult.Date = singleResult.formattedDateTime(NYTRawResult[<span style="color: #a31515">"date"</span>]);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">if </span>(NYTRawResult.Keys.Contains(<span style="color: #a31515">&#8220;title&#8221;</span>))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleResult.Title = NYTRawResult[<span style="color: #a31515">"title"</span>];<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue">if </span>(NYTRawResult.Keys.Contains(<span style="color: #a31515">&#8220;url&#8221;</span>))<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleResult.Url = <span style="color: blue">new </span><span style="color: #2b91af">Uri</span>(NYTRawResult[<span style="color: #a31515">"url"</span>]);</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green">//add our new result to the collection<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span> resultCollection.Add(singleResult);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green">//assign the result as the source for our ListBox<br />
&nbsp;&nbsp;&nbsp;&nbsp;</span> ResultsDisplay.ItemsSource = resultCollection;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: green">//take the overall article count and display it<br />
</span>&nbsp;&nbsp;&nbsp;&nbsp; resultCount.Text = <span style="color: #a31515">&#8220;Number of articles: &#8221; </span>+ completeResult[<span style="color: #a31515">"total"</span>].ToString();<br />
}</p>
<p>Now, we can run our project. Type something into the TextBox and hit the button and we get this:<br />
<a href="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image0017.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001[7]" src="http://www.designersilverlight.com/wp-content/uploads/2009/02/clip-image0017-thumb.png" border="0" alt="clip_image001[7]" width="404" height="303" /></a></p>
<p>Not exactly the most readable thing ever. So let’s add the following to the ListBox XAML:</p>
<p><span style="color: red">DisplayMemberPath</span><span style="color: blue">=&#8221;Title&#8221;</span></p>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Now we get something a little more like this (go ahead and give it a whirl):</p>
<p><iframe src="http://silverlight.services.live.com/invoke/77530/JSON_NYT_Tutorial_Part_1/iframe.html" scrolling="no" frameborder="0" style="width:400px; height:300px"></iframe></p>
<p>Much better. Remember, this is a simple query, so it’s only looking for items that have that word in the article… it might not be in the title.</p>
<p><a href="http://www.designersilverlight.com/2009/02/25/using-silverlight-to-display-json-data-collected-from-the-new-york-times-api/">My next post builds on this one</a> and I walk through building a more useful display for our results. It will be far less code intensive and far more designer centric.</p>
<p>I’ve made the source available for this project. I’ve taken out my NYT API key, so it will not run unless you get your own and put it in.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/02/jsonnewyorktimestutorial_part_1.zip">JSON &#8211; Silverlight &#8211; New York Times Tutorial Part 1 project files</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.designersilverlight.com/2009/02/25/adventures-with-json-and-silverlight-and-the-new-york-times/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Mask Animations (Clipping Path Animations) In Silverlight Without a Line of Code</title>
		<link>http://www.designersilverlight.com/2009/01/15/mask-animations-clipping-path-animations-in-silverlight-without-a-line-of-code/</link>
		<comments>http://www.designersilverlight.com/2009/01/15/mask-animations-clipping-path-animations-in-silverlight-without-a-line-of-code/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 17:14:46 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Blend]]></category>
		<category><![CDATA[How To...]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[without a line of code]]></category>

		<guid isPermaLink="false">http://www.designersilverlight.com/?p=472</guid>
		<description><![CDATA[Last night I submitted my MIX 10K Challenge piece, so I’m just killing time while I wait for it to get accepted. Please keep me in mind if you plan on voting. OK… I’ve been excited about this for some time now. I found out (almost completely by accident) that you can animate a mask [...]]]></description>
			<content:encoded><![CDATA[<p>Last night I submitted my MIX 10K Challenge piece, so I’m just killing time while I wait for it to get accepted. Please keep me in mind if you plan on voting.</p>
<p>OK… I’ve been excited about this for some time now. I found out (almost completely by accident) that you can animate a mask (also known as a clipping path) in Silverlight without writing code or even typing (much). This is exciting because I think it’s something that might interest our Flash friends who, late at night and in the privacy of their own homes, take a peek to see if Silverlight is worth looking into. When I worked with Flash, mask animations were my bread and butter and they seemed so hard to do in Silverlight.</p>
<p>But they’re not.</p>
<p>First open up your project in Blend. (I always create my project in Visual Studio and then open it in Blend because visual Studio has so much better project debugging and makes testing a breeze.)</p>
<p>Find the item you want to clip (in my case this is an image) and select the pen tool from the toolbar.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/01/clip-image001.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001" src="http://www.designersilverlight.com/wp-content/uploads/2009/01/clip-image001-thumb.png" border="0" alt="clip_image001" width="44" height="46" /></a></p>
<p>And start drawing.I’m a poor artist, so the star I drew looks pretty bad. If you need to change any of the points, use the direct selection tool (the light arrow, second from the top).</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/01/clip-image0014.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001[4]" src="http://www.designersilverlight.com/wp-content/uploads/2009/01/clip-image0014-thumb.png" border="0" alt="clip_image001[4]" width="42" height="114" /></a></p>
<p>OK… now the fun part. Add a storyboard to your project</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/01/clip-image0016.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001[6]" src="http://www.designersilverlight.com/wp-content/uploads/2009/01/clip-image0016-thumb.png" border="0" alt="clip_image001[6]" width="332" height="141" /></a></p>
<p>and you can animate the clipping mask using all the normal animation tools. Use the direct selection tool above to animate the vertices. You’ll get something like this:</p>
<p><iframe src="http://silverlight.services.live.com/invoke/77530/pathAnimation_Part1/iframe.html" scrolling="no" frameborder="0" style="width:400px; height:300px"></iframe></p>
<p>Now… if you’re not interested in the details, just skip ahead a little bit. Something important happened the moment we started animating the path. Normally, path data is kept in a single string format that looks something like this:</p>
<p><span style="color: red;">Data</span><span style="color: blue;">=&#8221;M159.67175,104.26108 L177.91812,28.328932 L195.51648,104.43327 L255.0802,102.6104 L206.86984,151.82758 L225.8029,226.56477 L179.0616,179.17046 L129.73396,229.29906 L147.97842,150.63879 L98.650803,101.53297 z&#8221;</span></p>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>This is actually a “<a href="http://msdn.microsoft.com/en-us/library/cc189041(VS.95).aspx">powerful and complex mini-language</a> that that you can use to describe geometric paths in XAML.” Microsoft uses it by default to handle path data. But it doesn’t work very well for animating paths, so the second you animate a single vertex in your path, it changes the format you see above to the format you see below:</p>
<p><span style="color: blue;">&lt;</span><span style="color: #a31515;">Path.Data</span><span style="color: blue;">&gt;<br />
</span><span style="color: blue;">&lt;</span><span style="color: #a31515;">PathGeometry</span><span style="color: blue;">&gt;<br />
&lt;</span><span style="color: #a31515;">PathFigure </span><span style="color: red;">IsClosed</span><span style="color: blue;">=&#8221;True&#8221; </span><span style="color: red;">StartPoint</span><span style="color: blue;">=&#8221;91.0527648925781,84.0121078491211&#8243;&gt;<br />
&lt;</span><span style="color: #a31515;">LineSegment </span><span style="color: red;">Point</span><span style="color: blue;">=&#8221;118.057907104492,0.549586236476898&#8243;/&gt;<br />
&lt;</span><span style="color: #a31515;">LineSegment </span><span style="color: red;">Point</span><span style="color: blue;">=&#8221;144.103973388672,84.2013778686523&#8243;/&gt;<br />
&lt;</span><span style="color: #a31515;">LineSegment </span><span style="color: red;">Point</span><span style="color: blue;">=&#8221;232.259979248047,82.1977386474609&#8243;/&gt;<br />
&lt;</span><span style="color: #a31515;">LineSegment </span><span style="color: red;">Point</span><span style="color: blue;">=&#8221;160.907287597656,136.2958984375&#8243;/&gt;<br />
&lt;</span><span style="color: #a31515;">LineSegment </span><span style="color: red;">Point</span><span style="color: blue;">=&#8221;188.928756713867,218.444961547852&#8243;/&gt;<br />
&lt;</span><span style="color: #a31515;">LineSegment </span><span style="color: red;">Point</span><span style="color: blue;">=&#8221;119.750289916992,166.350433349609&#8243;/&gt;<br />
&lt;</span><span style="color: #a31515;">LineSegment </span><span style="color: red;">Point</span><span style="color: blue;">=&#8221;46.7439804077148,221.450408935547&#8243;/&gt;<br />
&lt;</span><span style="color: #a31515;">LineSegment </span><span style="color: red;">Point</span><span style="color: blue;">=&#8221;73.7462997436523,134.989212036133&#8243;/&gt;<br />
&lt;</span><span style="color: #a31515;">LineSegment </span><span style="color: red;">Point</span><span style="color: blue;">=&#8221;0.740016639232636,81.0134506225586&#8243;/&gt;<br />
&lt;/</span><span style="color: #a31515;">PathFigure</span><span style="color: blue;">&gt;<br />
&lt;/</span><span style="color: #a31515;">PathGeometry</span><span style="color: blue;">&gt;<br />
&lt;/</span><span style="color: #a31515;">Path.Data</span><span style="color: blue;">&gt;</span></p>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Same data, but only this latter format is appropriate for animation. Remember this, it will come in handy in a second.</p>
<p>OK… now right click on your path and go to “Path –&gt; Make Clipping Path”</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/01/clip-image00110.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001[10]" src="http://www.designersilverlight.com/wp-content/uploads/2009/01/clip-image00110-thumb.png" border="0" alt="clip_image001[10]" width="367" height="52" /></a></p>
<p>You’ll get a dialog pop-up that will ask you which item in the visual tree you would like to clip. I chose my image, but it will work with anything.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/01/clip-image00112.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001[12]" src="http://www.designersilverlight.com/wp-content/uploads/2009/01/clip-image00112-thumb.png" border="0" alt="clip_image001[12]" width="297" height="158" /></a></p>
<p>And… poof! You have clipping animation.</p>
<p><iframe src="http://silverlight.services.live.com/invoke/77530/PathAnimation_Part2/iframe.html" scrolling="no" frameborder="0" style="width:400px; height:300px"></iframe></p>
<p>LIMITATIONS: Here are the limitations to this method:</p>
<ol>
<li><strong>You need to animate before you make your path into a clipping path. – </strong>Remember that little bit above about the data string format vs. path geometry? If you don’t animate before hand, Blend will convert all your beautiful line segments into the un-animate-able data string. Animating the path tells Blend to leave your formatting alone.</li>
<li><strong>Once you make your path a clipping  path, animation gets <em>a lot</em> harder</strong> – Basically, you can change the time by dragging the key frames around and change the easing. Any other alterations require entering values by hand. So do all your animation first before setting the path. The easiest work around to this is to copy and paste your path out of the Control.Clip section and back into your main XAML and tweak animation from there. But your best option is to just take the time to make your clipping animation perfect before you make it a clipping path.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.designersilverlight.com/2009/01/15/mask-animations-clipping-path-animations-in-silverlight-without-a-line-of-code/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How To Give a Silverlight 2 Gadget a Transparent Background</title>
		<link>http://www.designersilverlight.com/2008/12/03/how-to-give-a-silverlight-2-gadget-a-transparent-background/</link>
		<comments>http://www.designersilverlight.com/2008/12/03/how-to-give-a-silverlight-2-gadget-a-transparent-background/#comments</comments>
		<pubDate>Thu, 04 Dec 2008 06:06:22 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[gadget]]></category>
		<category><![CDATA[Silverlight 2]]></category>
		<category><![CDATA[transparent background]]></category>
		<category><![CDATA[vista]]></category>

		<guid isPermaLink="false">http://www.designersilverlight.com/2008/12/03/how-to-give-a-silverlight-2-gadget-a-transparent-background/</guid>
		<description><![CDATA[Warning: This is a poor solution if your gadget has any level of dragging interaction in it. The &#8220;windowless=true&#8221; step will make it so that any dragging interaction will be interrupted by the gadget, which will interpret such a move as an attempt to drag the gadget off the side bar. Giving your Silverlight gadget [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Warning:</strong> This is a poor solution if your gadget has any level of dragging interaction in it. The &#8220;windowless=true&#8221; step will make it so that any dragging interaction will be interrupted by the gadget, which will interpret such a move as an attempt to drag the gadget off the side bar.</p>
<p>Giving your Silverlight gadget a transparent background is actually quite simple. First, follow the directions from <a href="http://blogs.msdn.com/szurgot/archive/2008/11/03/silverlight-gadget-version-of-snowflake-app.aspx">Chris Szurgot&#8217;s post on Silverlight gadgets</a>. No point in duplicating his work.</p>
<p>When you&#8217;re done, create a transparent png file. <a href="http://www.designersilverlight.com/wp-content/uploads/2008/12/transparent.png">Or download this one.</a></p>
<p>Now, head over to your html file. Get rid of all CSS background information. Add the following just below the body tag:</p>
<p><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;"> </span></span></p>
<p><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">&lt;</span><span style="font-size: x-small; color: #a31515;"><span style="font-size: x-small; color: #a31515;">g</span></span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">:</span></span><span style="font-size: x-small; color: #a31515;"><span style="font-size: x-small; color: #a31515;">background</span></span><span style="font-size: x-small;"> </span><span style="font-size: x-small; color: #ff0000;"><span style="font-size: x-small; color: #ff0000;">id</span></span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">=&#8221;background&#8221;</span></span><span style="font-size: x-small;"><br />
</span><span style="font-size: x-small; color: #ff0000;"><span style="font-size: x-small; color: #ff0000;">src</span></span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">=&#8221;transparent.png&#8221;<br />
</span></span><span style="font-size: x-small; color: #ff0000;"><span style="font-size: x-small; color: #ff0000;">style</span></span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">=&#8221;</span></span><span style="font-size: x-small; color: #ff0000;"><span style="font-size: x-small; color: #ff0000;">position</span></span><span style="font-size: x-small;">:</span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">absolute</span></span><span style="font-size: x-small;">;</span><span style="font-size: x-small; color: #ff0000;"><span style="font-size: x-small; color: #ff0000;">top</span></span><span style="font-size: x-small;">:</span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">0</span></span><span style="font-size: x-small;">;</span><span style="font-size: x-small; color: #ff0000;"><span style="font-size: x-small; color: #ff0000;">left</span></span><span style="font-size: x-small;">:</span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">0</span></span><span style="font-size: x-small;">;</span><span style="font-size: x-small; color: #ff0000;"><span style="font-size: x-small; color: #ff0000;">z-index</span></span><span style="font-size: x-small;">:</span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">-999</span></span><span style="font-size: x-small;">;</span><span style="font-size: x-small; color: #ff0000;"><span style="font-size: x-small; color: #ff0000;">no</span></span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">=repeat</span></span><span style="font-size: x-small;">;</span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">&#8220;</span></span><span style="font-size: x-small;"> </span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">/&gt;</span></span></span></p>
<p>Finally, change the following params in the Silverlight object host.</p>
<p><span style="color: blue;">&lt;</span><span style="color: #a31515;">param </span><span style="color: red;">name</span><span style="color: blue;">=&#8221;background&#8221; </span><span style="color: red;">value</span><span style="color: blue;">=&#8221;transparent&#8221; /&gt;<br />
&lt;</span><span style="color: #a31515;">param </span><span style="color: red;">name</span><span style="color: blue;">=&#8221;windowless&#8221; </span><span style="color: red;">value</span><span style="color: blue;">=&#8221;true&#8221; /&gt;</span></p>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>And that’s it.</p>
<p><strong>Additional Warning:</strong> It looks like there is no solution yet for the pink halo around any anti-aliased pixels in a Silverlight gadget.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2008/12/clip-image001.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001" src="http://www.designersilverlight.com/wp-content/uploads/2008/12/clip-image001-thumb.png" border="0" alt="clip_image001" width="138" height="213" /></a></p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2008/12/clip-image0014.png"><img style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" title="clip_image001[4]" src="http://www.designersilverlight.com/wp-content/uploads/2008/12/clip-image0014-thumb.png" border="0" alt="clip_image001[4]" width="81" height="83" /></a></p>
<p>This is a problem that transparent Silverlight gadgets have had for some time now. If that doesn’t bother you then you’re all set.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designersilverlight.com/2008/12/03/how-to-give-a-silverlight-2-gadget-a-transparent-background/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Getting a MouseLeftButtonDown or MouseLeftButtonUp Event From Your TextBox</title>
		<link>http://www.designersilverlight.com/2008/12/03/getting-a-mouseleftbuttondown-or-mouseleftbuttonup-event-from-your-textbox/</link>
		<comments>http://www.designersilverlight.com/2008/12/03/getting-a-mouseleftbuttondown-or-mouseleftbuttonup-event-from-your-textbox/#comments</comments>
		<pubDate>Thu, 04 Dec 2008 00:28:46 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[XAML]]></category>
		<category><![CDATA[colorpicker]]></category>
		<category><![CDATA[MouseLeftButtonDown]]></category>
		<category><![CDATA[MouseLeftButtonUp]]></category>
		<category><![CDATA[SelectAll]]></category>
		<category><![CDATA[TextBox]]></category>

		<guid isPermaLink="false">http://www.designersilverlight.com/?p=396</guid>
		<description><![CDATA[One of the nifty little tricks I got working in my attempt to update my Silverlight Color Picker was that I wanted to have the entire content of a TextBox to select when I clicked on it. Imagine my surprise when clicking on the TextBox didn&#8217;t fire either a MouseLeftButtonUp or a MouseLeftButtonDown event! This [...]]]></description>
			<content:encoded><![CDATA[<p>One of the nifty little tricks I got working in my attempt to update my Silverlight Color Picker was that I wanted to have the entire content of a TextBox to select when I clicked on it. Imagine my surprise when clicking on the TextBox <em>didn&#8217;t fire either a MouseLeftButtonUp or a MouseLeftButtonDown event!</em></p>
<p>This stunned me a little bit. Turns out there is a big discussion about this over at the <a href="http://silverlight.net/forums/p/31070/112500.aspx">Silverlight Forum</a>. Long story short, I needed to add this code to my project (you&#8217;ll see it in my Color Picker project if you download it).</p>
<p>Add this code to your MyPage.xaml.cs file:</p>
<p><span style="color: #0000ff;">public class</span> <span style="color: #008080;">TextBoxClickable</span> : <span style="color: #008080;">TextBox</span><br />
{<br />
&nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">protected override void</span> OnMouseLeftButtonDown(<span style="color: #008080;">MouseButtonEventArgs</span> e)<br />
&nbsp; &nbsp; &nbsp;{<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">base</span>.OnMouseLeftButtonDown(e);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;e.Handled = <span style="color: #0000ff;">false</span>;<br />
&nbsp; &nbsp; &nbsp;}</p>
<p>&nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">protected override void</span> OnMouseLeftButtonUp(<span style="color: #008080;">MouseButtonEventArgs</span> e)<br />
&nbsp; &nbsp; &nbsp;{<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">base</span>.OnMouseLeftButtonUp(e);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;e.Handled = <span style="color: #0000ff;">false</span>;<br />
&nbsp; &nbsp; &nbsp;}<br />
}</p>
<p>Your XAML should define your namespace:</p>
<p><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">UserControl</span> <span style="color: #ff0000;">x<span style="color: #0000ff;">:</span>Class</span>=&#8221;<span style="color: #0000ff;">MyProjectName.MyPage</span>&#8221;<br />
&nbsp; &nbsp; &nbsp;<span style="color: #ff0000;"> xmlns<span style="color: #0000ff;">:</span>clickable</span>=&#8221;<span style="color: #0000ff;">clr-namespace:MyPage</span>&#8220;&gt;</p>
<p>And you can use your new clickable TextBox like this:</p>
<p><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">clickable</span><span style="color: #0000ff;">:</span><span style="color: #800000;">TextBoxClickable</span> <span style="color: #ff0000;">MouseLeftButtonDown</span>=&#8221;<span style="color: #0000ff;">TextBox_SelectAll</span>&#8221; <span style="color: #0000ff;">/&gt;</span></p>
<p>If you&#8217;re like me and want to select everything in the TextBox with a single click, your method will look like this:</p>
<p><span style="color: #0000ff;">private void</span> TextBox_SelectAll(<span style="color: #0000ff;">object</span> sender, <span style="color: #008080;">MouseButtonEventArgs</span> e)<br />
{<br />
&nbsp; &nbsp; &nbsp;<span style="color: #008080;">TextBox</span> selectionBox = (<span style="color: #008080;">TextBox</span>)sender;<br />
&nbsp; &nbsp; &nbsp;selectionBox.SelectAll();<br />
}</p>
<p>That&#8217;s it. I&#8217;ll post my updated color pickers in a couple hours.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designersilverlight.com/2008/12/03/getting-a-mouseleftbuttondown-or-mouseleftbuttonup-event-from-your-textbox/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>How To Use a ListView or DataGrid In a ComboBox Drop Down (Without A Line Of Code)</title>
		<link>http://www.designersilverlight.com/2008/10/01/how-to-use-a-listview-or-datagrid-in-a-combobox-drop-down-without-a-line-of-code/</link>
		<comments>http://www.designersilverlight.com/2008/10/01/how-to-use-a-listview-or-datagrid-in-a-combobox-drop-down-without-a-line-of-code/#comments</comments>
		<pubDate>Thu, 02 Oct 2008 00:30:00 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[combobox]]></category>
		<category><![CDATA[listview]]></category>
		<category><![CDATA[styles]]></category>
		<category><![CDATA[templates]]></category>

		<guid isPermaLink="false">http://www.designersilverlight.com/2008/10/01/how-to-use-a-listview-or-datagrid-in-a-combobox-drop-down-without-a-line-of-code/</guid>
		<description><![CDATA[Super cool new thing I learned today. In WPF, we can make it so that the drop down (popup) on a ComboBox displays the data in a ListView. Because of the fact that I believe the DataGrid will eventually overtake the ListView as the gold standard for data display in WPF, we’ll do it both [...]]]></description>
			<content:encoded><![CDATA[<p>Super cool new thing I learned today. In WPF, we can make it so that the drop down (popup) on a ComboBox displays the data in a ListView. Because of the fact that I believe the DataGrid will eventually overtake the ListView as the gold standard for data display in WPF, we’ll do it both ways.</p>
<p>This tutorial is done entirely in Blend and without a line of code.</p>
<p><strong>Step 0)</strong> (for the DataGrid only)</p>
<p>Go to Code Plex and <a href="http://www.codeplex.com/wpf/Release/ProjectReleases.aspx?ReleaseId=14963">download the WPF Toolkit</a>. Extract to a convenient location.</p>
<p>Right-Click on the References folder in your project tab and click “Add Reference…”</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2008/10/clip-image001.png"><img src="http://www.designersilverlight.com/wp-content/uploads/2008/10/clip-image001-thumb.png" title="clip_image001" style="border-width: 0px; display: inline" alt="clip_image001" border="0" height="85" width="238" /></a></p>
<p>Navigate to the location you extracted the WPFToolkit.dll file, select it and hit OK.<br />
<a href="http://www.designersilverlight.com/wp-content/uploads/2008/10/clip-image0016.png"><img src="http://www.designersilverlight.com/wp-content/uploads/2008/10/clip-image0016-thumb.png" title="clip_image001[6]" style="border-width: 0px; display: inline" alt="clip_image001[6]" border="0" height="205" width="177" /></a></p>
<p>Step 1) Select the ComboBox you wish to change and edit the ControlTemplate by right-clicking and selecting “Edit Control Parts (Template) –&gt; Edit a Copy…”</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2008/10/clip-image0011.png"><img src="http://www.designersilverlight.com/wp-content/uploads/2008/10/clip-image0011-thumb.png" title="clip_image001[1]" style="border: 0px none ; display: inline" alt="clip_image001[1]" border="0" height="55" width="328" /></a></p>
<p>Step 2) Find the ItemsPresenter. This is what would normally display our ItemsSource.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2008/10/clip-image0013.png"><img src="http://www.designersilverlight.com/wp-content/uploads/2008/10/clip-image0013-thumb.png" title="clip_image001[3]" style="border: 0px none ; display: inline" alt="clip_image001[3]" border="0" height="187" width="252" /></a></p>
<p>We’re going to get rid of it. And the ScrollViewer for good measure.</p>
<p>Step 3) Where the ScrollViewer is, put in a ListView or a DataGrid, whichever one you’re using.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2008/10/clip-image0015.png"><img src="http://www.designersilverlight.com/wp-content/uploads/2008/10/clip-image0015-thumb.png" title="clip_image001[5]" style="border: 0px none ; display: inline" alt="clip_image001[5]" border="0" height="132" width="255" /></a></p>
<p>Now, go the properties of that ListView or DataGrid and click on the box to the right of “ItemsSource”</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2008/10/clip-image00111.png"><img src="http://www.designersilverlight.com/wp-content/uploads/2008/10/clip-image00111-thumb.png" title="clip_image001[11]" style="border: 0px none ; display: inline" alt="clip_image001[11]" border="0" height="60" width="168" /></a></p>
<p>and, in the resulting menu, select “Template Binding –&gt; ItemsSource”.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2008/10/clip-image0017.png"><img src="http://www.designersilverlight.com/wp-content/uploads/2008/10/clip-image0017-thumb.png" title="clip_image001[7]" style="border: 0px none ; display: inline" alt="clip_image001[7]" border="0" height="294" width="438" /></a></p>
<p>Set the DataContext of the ListView or DataGrid to the DataContext of the parent ComboBox using the same process.</p>
<p>And… you’re done! Open the ComboBox and you will see that you can select items in the ListView or DataGrid in the ComboBox dropdown and see those items change the selection of the ComboBox.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2008/10/clip-image00113.png"><img src="http://www.designersilverlight.com/wp-content/uploads/2008/10/clip-image00113-thumb.png" title="clip_image001[13]" style="border: 0px none ; display: inline" alt="clip_image001[13]" border="0" height="193" width="545" /></a></p>
<p>You’ll notice that this is probably not yet an ideal solution. For example, when we select an item, the dropdown doesn’t automatically close. Your best bet is to use the SelectionChanged event to trigger some logic to close the ComboBox dropdown.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designersilverlight.com/2008/10/01/how-to-use-a-listview-or-datagrid-in-a-combobox-drop-down-without-a-line-of-code/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
