<?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; Advanced Tutorial</title>
	<atom:link href="http://www.designersilverlight.com/category/advanced-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>Thu, 26 Jan 2012 06:45:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>How To Create An Animated ScrollViewer (or ListBox) in WPF</title>
		<link>http://www.designersilverlight.com/2009/05/06/how-to-create-an-animated-scrollviewer-or-listbox-in-wpf/</link>
		<comments>http://www.designersilverlight.com/2009/05/06/how-to-create-an-animated-scrollviewer-or-listbox-in-wpf/#comments</comments>
		<pubDate>Wed, 06 May 2009 22:38:53 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Advanced Tutorial]]></category>
		<category><![CDATA[Binding]]></category>
		<category><![CDATA[Blend]]></category>
		<category><![CDATA[How To...]]></category>
		<category><![CDATA[ScrollViewer]]></category>
		<category><![CDATA[styles]]></category>
		<category><![CDATA[templates]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[XAML]]></category>

		<guid isPermaLink="false">http://www.designersilverlight.com/2009/05/06/how-to-create-an-animated-scrollviewer-or-listbox-in-wpf/</guid>
		<description><![CDATA[UPDATED 05/22/09 In the comments, someone mentioned that the project wasn&#8217;t working properly for keyed scrolling. I&#8217;ve updated the project with: Key scrolling (left, right, up, down, page up, page down) CanKeyboardScroll property on the AnimatedScrollViewer so that keyboard scrolling can be turned off ScrollToSelectedItem property on the AnimatedListBox so that the user can have [...]]]></description>
			<content:encoded><![CDATA[<p><b>UPDATED 05/22/09</b></p>
<p>In the comments, someone mentioned that the project wasn&#8217;t working properly for keyed scrolling. I&#8217;ve updated the project with:</p>
<ul>
<li>Key scrolling (left, right, up, down, page up, page down)</li>
<li>CanKeyboardScroll property on the AnimatedScrollViewer so that keyboard scrolling can be turned off</li>
<li>ScrollToSelectedItem property on the AnimatedListBox so that the user can have it automatically scroll to a ListBoxItem</li>
</ul>
<p>That last one is a little hacky&#8230; I use the ListBox ItemContainerGenerator to get the heights of all the items up to the one you want and then scroll it that. I&#8217;m almost positive there is a better way and if anyone knows what it is, I&#8217;d love to hear it. </p>
<p>First things first, here are the project files. </p>
<p><a href='http://www.designersilverlight.com/wp-content/uploads/2009/05/animatedscrollviewersource.zip'>Animated ScrollViewer and ListBox Project Files (Updated 5/22/09)</a> – Contains the AnimatedScrollViewer control library with AnimatedScrollViewer and AnimatedListBox</p>
<p><a href='http://www.designersilverlight.com/wp-content/uploads/2009/05/animatedscrollviewerdll.zip'>Animated ScrollViewer and ListBox DLL (Updated 5/22/09)</a> – For those of you who don’t care how it works and just want it to work</p>
<p>OK… this is going to be something of a whirlwind since I’ve never written a post this in-depth before… it will strain the limits of my ADD. </p>
<h3>Problem:</h3>
<p>The Listbox/ScrollViewer not only doesn’t animate, but it seems impossible to tweak it so that it animates.</p>
<h3>The Reason:</h3>
<p>The reason has everything to do with the ScrollViewer. Basically, the ScrollContainer and the ScrollBars are very tightly intertwined. There is a lot of code that does all the scrolling calculations and that code needs to apply to the scrolled content as well as the UI for the ScrollBars. If you dig deep enough, you’ll see the reasons. Reasons which I assume for the moment you don’t care about… you’re probably in more of a “make the @#&amp;($ thing work!” mood. I know I was.</p>
<h3>The Solution:</h3>
<p>My solution was basically to completely bypass the built-in ScrollBars and put in new ScrollBars with new logic. They look and act just like normal ScrollBars, so you should be able to style them just you would any normal ScrollViewer.</p>
<p>OK… how I did it. (I’m going to use both Blend 2 and Visual Studio 2008)</p>
<p>First, create a new custom control for WPF. This can be done by going into Visual Studio and creating a new Project. Select “WPF Custom Control Library”</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/05/clip-image0017.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image001[7]" border="0" alt="clip_image001[7]" src="http://www.designersilverlight.com/wp-content/uploads/2009/05/clip-image0017-thumb.png" width="498" height="163" /></a></p>
<p> In Blend: </p>
<p>&#160;<a href="http://www.designersilverlight.com/wp-content/uploads/2009/05/clip-image0019.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image001[9]" border="0" alt="clip_image001[9]" src="http://www.designersilverlight.com/wp-content/uploads/2009/05/clip-image0019-thumb.png" width="492" height="321" /></a></p>
<p> Add a WPF application to the project too so you have something to test. In the WPF application, get Blend to generate the default template for a normal ScrollViewer, accessible (in Blend) by putting a ScrollViewer into the project and right-clicking on it and selecting “Edit Control Parts (Template) –&gt; Edit a Copy…” </p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/05/clip-image001.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image001" border="0" alt="clip_image001" src="http://www.designersilverlight.com/wp-content/uploads/2009/05/clip-image001-thumb.png" width="323" height="96" /></a></p>
<p>Once have the default ScrollViewer template, select the “PART_VerticalScrollBar” and the “PART_HorizontalScrollBar” and copy and paste them. Rename your new ScrollBars something you like… I used “PART_AniVerticalScrollBar” and “PART&quot;_AniHorizontalScrollBar”. Now, set the Visibility of the original ScrollBars to “Collapsed”. (We can’t get rid of them, because the ScrollViewer will be looking for them and will throw a conniption if it can’t find them.) </p>
<p>Also, change the Value of your new ScrollBars to 0. You’ll probably have to click on the orange box next to Value and select “Reset”.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/05/clip-image00111.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image001[11]" border="0" alt="clip_image001[11]" src="http://www.designersilverlight.com/wp-content/uploads/2009/05/clip-image00111-thumb.png" width="219" height="66" /></a></p>
<p>In Visual Studio, right-click on your WPF Custom Control project and go to “Add –&gt; New Item…” . Then select “Custom Control (WPF)” and name it something you like (mine is named AnimatedScrollViewer). This should add a class to your project as well as a basic template to your Generic.xaml file.</p>
<p>Copy the ScrollViewer template that we just made and paste it into the Generic.xaml. The only change we need to make is to change:</p>
<p><span style="color: red">TargetType</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">x</span><span style="color: blue">:</span><span style="color: #a31515">Type </span><span style="color: red">ScrollViewer</span><span style="color: blue">}&quot;</span></p>
<p> <a href="http://11011.net/software/vspaste"></a>
<p>to</p>
<p><span style="color: red">TargetType</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">x</span><span style="color: blue">:</span><span style="color: #a31515">Type </span><span style="color: red">local</span><span style="color: blue">:</span><span style="color: red">AnimatedScrollViewer</span><span style="color: blue">}&quot;</span></p>
<p>in the Style and the ControlTemplate.</p>
<p>OK… that’s pretty much it with the XAML. Now we get to move into the code.</p>
<p>Right now, our class inherits from Control, but we want it to inherit from ScrollViewer like so:</p>
<p><span style="color: blue">public class </span><span style="color: #2b91af">AnimatedScrollViewer </span>: <span style="color: #2b91af">ScrollViewer</span></p>
<p> <a href="http://11011.net/software/vspaste"></a>
<p>Next get some containers for our new spiffy ScrollBars so that we can access them from the custom control code. Type the following before the class:</p>
<p>[<span style="color: #2b91af">TemplatePart</span>(Name = <span style="color: #a31515">&quot;PART_AniVerticalScrollBar&quot;</span>, Type = <span style="color: blue">typeof</span>(<span style="color: #2b91af">ScrollBar</span>))]     <br />[<span style="color: #2b91af">TemplatePart</span>(Name = <span style="color: #a31515">&quot;PART_AniHorizontalScrollBar&quot;</span>, Type = <span style="color: blue">typeof</span>(<span style="color: #2b91af">ScrollBar</span>))]</p>
<p>and the following just inside the class:</p>
<p><span style="color: #2b91af">ScrollBar </span>_aniVerticalScrollBar;     <br /><span style="color: #2b91af">ScrollBar </span>_aniHorizontalScrollBar;</p>
<p> <a href="http://11011.net/software/vspaste"></a>
<p>Now, we’ll override the OnApplyTemplate and make the connection between the template scrollBars and our class ScrollBars:</p>
<p><span style="color: blue">public override void</span>OnApplyTemplate()    <br />{    <br />&#160;&#160;&#160; <span style="color: blue">base</span>.OnApplyTemplate();    </p>
<p>&#160;&#160;&#160; <span style="color: #2b91af">ScrollBar </span>aniVScroll = <span style="color: blue">base</span>.GetTemplateChild(<span style="color: #a31515">&quot;PART_AniVerticalScrollBar&quot;</span>) <span style="color: blue">as</span><span style="color: #2b91af">ScrollBar</span>;    <br />&#160;&#160;&#160; <span style="color: blue">if</span>(aniVScroll != <span style="color: blue">null</span>)    <br />&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; _aniVerticalScrollBar = aniVScroll;    <br />&#160;&#160;&#160; }    <br />&#160;&#160;&#160; _aniVerticalScrollBar.ValueChanged += <span style="color: blue">new</span><span style="color: #2b91af">RoutedPropertyChangedEventHandler</span>&lt;<span style="color: blue">double</span>&gt;(_aniVerticalScrollBar_ValueChanged);    </p>
<p>&#160;&#160;&#160; <span style="color: #2b91af">ScrollBar </span>aniHScroll = <span style="color: blue">base</span>.GetTemplateChild(<span style="color: #a31515">&quot;PART_AniHorizontalScrollBar&quot;</span>) <span style="color: blue">as</span><span style="color: #2b91af">ScrollBar</span>;    <br />&#160;&#160;&#160; <span style="color: blue">if</span>(aniHScroll != <span style="color: blue">null</span>)    <br />&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; _aniHorizontalScrollBar = aniHScroll;    <br />&#160;&#160;&#160; }    <br />&#160;&#160;&#160; _aniHorizontalScrollBar.ValueChanged += <span style="color: blue">new</span><span style="color: #2b91af">RoutedPropertyChangedEventHandler</span>&lt;<span style="color: blue">double</span>&gt;(_aniHorizontalScrollBar_ValueChanged);    </p>
<p>&#160;&#160;&#160; <span style="color: blue">this</span>.PreviewMouseWheel += <span style="color: blue">new</span><span style="color: #2b91af">MouseWheelEventHandler</span>(AnimatedScrollViewer_PreviewMouseWheel);    <br />}<a href="http://11011.net/software/vspaste"></a></p>
<p>Before we address the three event handlers we added, we need to create the Dependency Properties with which they will be futzing.</p>
<p>(We’re going start going a little bit faster. Please download the code for the excruciating detail.) We need to add the following Dependency Properties. I’m using a “PropertyName (type)”. </p>
<h3>Dependency Properties</h3>
<p><strong>ScrollingTime (TimeSpan)</strong> – This will be an easy way to change the speed of the scrolling. I created mine to default at half a second, but if you changed it to 0 seconds, it would act just like any normal ScrollViewer.</p>
<p><strong>ScrollingSpline (KeySpline)</strong> – This property along with the ScrollingTime property is meant to give designers and developers the easiest control possible over the animation. This property describes the spline along which the scrolling will animate. If you don’t know what this means, just leave it alone, you’ll be fine. </p>
<p><strong>TargetVerticalOffset (double)</strong> and <strong>TargetHorizontalOffset (double)</strong> – These are properties that tell the ScrollViewer where it will be animating to. In the PropertyChangedCallback, they kick off a method that starts the animation.</p>
<p><strong>VerticalScrollOffset (double)</strong> and <strong>HorizontalScrollOffset (double)</strong> – For some reason the normal VerticalOffset and HorizontalOffset properties in a ScrollViewer are not capable of animation. So I wrote these properties that can be animated using standard storyboard procedures. If you use them to animate, make sure you also change the TargetVerticalOffset and TargetHorizontalOffset stuff as well… otherwise there will be a disconnect between the two. </p>
<h3>Event Handlers</h3>
<p><strong>CustomPreviewMouseWheel</strong> event handler – This grabs any mouse wheel spinning and uses it to change the TargetVerticalOffset so that the ScrollViewer will still animate the scrolling when the mouse wheel spins.</p>
<p><strong>VScrollBar_ValueChanged</strong> and <strong>HScrollBar_ValueChanged</strong> event handlers – These are called whenever the the ScrollBars are interacted with. There was a really weird problem with some of the interaction (the arrow keys and fast-scrolling buttons weren’t working properly), so these handlers hold logic to try to translate the weirdness into something viable. They then set the Target_Offset properties appropriately.</p>
<h3>Methods</h3>
<p><strong>animateScroller</strong> – This method builds the animation programmatically based off of the appropriate properties and runs it. </p>
<p> And that’s really about it. Once you have the AnimatedScrollViewer working, you can just add use it inside your ListBox templates and it should work. (For those who are averse to doing such a thing, I’ve added extremely simple AnimatedListBox.) </p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http://www.designersilverlight.com/2009/05/06/how-to-create-an-animated-scrollviewer-or-listbox-in-wpf/&amp;layout=standard&amp;show_faces=1&amp;width=450&amp;action=like&amp;colorscheme=light&amp;font=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:25px"></iframe><div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=How+To+Create+An+Animated+ScrollViewer+%28or+ListBox%29+in+WPF+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D653" title="Post to Twitter"><img class="nothumb" src="http://www.designersilverlight.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter.png" alt="Post to Twitter" /></a> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=How+To+Create+An+Animated+ScrollViewer+%28or+ListBox%29+in+WPF+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D653" title="Post to Twitter">Tweet This Post</a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.designersilverlight.com/2009/05/06/how-to-create-an-animated-scrollviewer-or-listbox-in-wpf/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>INotifyPropertyChanged Snippets (And Why You Should Use These Instead of DependencyProperties)</title>
		<link>http://www.designersilverlight.com/2009/04/30/inotifypropertychanged-snippets-and-why-you-should-use-these-instead-of-dependencyproperties/</link>
		<comments>http://www.designersilverlight.com/2009/04/30/inotifypropertychanged-snippets-and-why-you-should-use-these-instead-of-dependencyproperties/#comments</comments>
		<pubDate>Fri, 01 May 2009 01:53:56 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Advanced Tutorial]]></category>
		<category><![CDATA[Binding]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[How To...]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.designersilverlight.com/2009/04/30/inotifypropertychanged-snippets-and-why-you-should-use-these-instead-of-dependencyproperties/</guid>
		<description><![CDATA[First things first, here are my INotifyPropertyChanged snippets. INotifyPropertyChanged snippet (PropertyChangedEventHandler and RaisePropertyChanged method) INotifyPropertyChanged Property snippet Just download them into your &#34;Visual Studio 2008CodeSnippetsVisual C#My CodeSnippets&#34; folder and they should work. Just type &#34;notify&#34; and intellisense should show you &#34;notifyo&#34; (for NotifyObject) and &#34;notifyp&#34; (for NotifyProperty). Hit tab twice and the code should dump [...]]]></description>
			<content:encoded><![CDATA[<p>First things first, here are my INotifyPropertyChanged snippets. </p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/04/notifyo.snippet">INotifyPropertyChanged snippet (PropertyChangedEventHandler and RaisePropertyChanged method)</a></p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2009/04/notify_p.snippet">INotifyPropertyChanged Property snippet</a></p>
<p>Just download them into your &quot;Visual Studio 2008CodeSnippetsVisual C#My CodeSnippets&quot; folder and they should work. Just type &quot;notify&quot; and intellisense should show you &quot;notifyo&quot; (for NotifyObject) and &quot;notifyp&quot; (for NotifyProperty). Hit tab twice and the code should dump into your project.</p>
<p>This is definitely a &quot;use at your own risk&quot; project. </p>
<p>You see, there I was, minding my own business and trying to build some data to use with some XAML comps I was playing with and I was having some of the strangest things happen with my data. I had a DependecyProperty ObservableCollection in my ViewModel and I put a couple different views in my screen. (I was using an MVVM pattern, because that&#8217;s what all the kool kids are doing.) </p>
<p>Then, it suddenly seemed as if all my Views were sharing the same ObservableCollection, even though every other DependencyProperty they were bound to had unique values. So I did what I always do when I have problems like this&#8230; I ask <a href="http://xamlcoder.com/cs/blogs/joe/default.aspx">Joe McBride</a>.</p>
<p>It turns out I had gotten confused. I understood that DependencyProperties were good for the following:</p>
<ul>
<li>Providing callbacks when the property is changed</li>
<li>Binding to stuff</li>
<li>Animations</li>
</ul>
<p>I figured that this is the kind of behavior I wanted from my data. I was wrong. As it turns out that is the kind of behavior I want out of the properties that I use in my <em>WPF and Silverlight controls</em>. It seems that DependencyProperties are meant to be <em>used with controls and not for stand-alone data</em>. </p>
<p>For stand-alone data, I should have used INotifyPropertyChanged, which is an interface for&#8230; well&#8230; notifying things when a property changes. I already had handy snippets for creating DependencyProperties (thanks to <a href="http://labs.nerdplusart.com/">Robby Ingebretsen</a>). So I tweaked his snippets so that they work for INotifyPropertyChanged properties.</p>
<p>Because it seems silly to implement the PropertyChangedEventHandler in every class that needs notify-able properties, I like to create a &quot;NotifyObject&quot; class:</p>
<p><span style="color: blue">class </span><span style="color: #2b91af">NotifyObject </span>: <span style="color: #2b91af">INotifyPropertyChanged     <br /></span>{    <br />&#160;&#160;&#160; <span style="color: blue">public event </span><span style="color: #2b91af">PropertyChangedEventHandler </span>PropertyChanged;    </p>
<p>&#160;&#160;&#160; <span style="color: blue">protected void </span>RaisePropertyChanged(<span style="color: blue">string </span>propertyName)    <br />&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">var </span>handler = PropertyChanged;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">if</span>(handler != <span style="color: blue">null</span>)    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; handler(<span style="color: blue">this</span>, <span style="color: blue">new </span><span style="color: #2b91af">PropertyChangedEventArgs</span>(propertyName));    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }    <br />&#160;&#160;&#160; }&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />}</p>
<p>Then, I can make my new class inherit from NotifyObject and away I go creating my bindable, notify-able data:</p>
<p><span style="color: blue">public class </span><span style="color: #2b91af">MyNotifyableData </span>: <span style="color: #2b91af">NotifyObject     <br /></span>{    <br />&#160;&#160;&#160; <span style="color: blue">public </span>MyNotifyableData()    <br />&#160;&#160;&#160; {    <br />&#160;&#160;&#160; }    </p>
<p>&#160;&#160;&#160; <span style="color: blue">#region </span>MyProperty (INotifyPropertyChanged Property)    <br />&#160;&#160;&#160; <span style="color: blue">private string </span>_myProperty;    </p>
<p>&#160;&#160;&#160; <span style="color: blue">public string </span>MyProperty    <br />&#160;&#160;&#160; {    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">get </span>{ <span style="color: blue">return </span>_myProperty; }    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: blue">set     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>{    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; _myProperty = <span style="color: blue">value</span>;    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; RaisePropertyChanged(<span style="color: #a31515">&quot;MyProperty&quot;</span>);    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }    <br />&#160;&#160;&#160; }    <br />&#160;&#160;&#160; <span style="color: blue">#endregion     <br /></span>}</p>
<p>This property was created using my snippet above. Hope it helps.</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http://www.designersilverlight.com/2009/04/30/inotifypropertychanged-snippets-and-why-you-should-use-these-instead-of-dependencyproperties/&amp;layout=standard&amp;show_faces=1&amp;width=450&amp;action=like&amp;colorscheme=light&amp;font=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:25px"></iframe><div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=INotifyPropertyChanged+Snippets+%28And+Why+You+Should+Use+These+Instead+of+DependencyProperties%29+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D640" title="Post to Twitter"><img class="nothumb" src="http://www.designersilverlight.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter.png" alt="Post to Twitter" /></a> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=INotifyPropertyChanged+Snippets+%28And+Why+You+Should+Use+These+Instead+of+DependencyProperties%29+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D640" title="Post to Twitter">Tweet This Post</a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.designersilverlight.com/2009/04/30/inotifypropertychanged-snippets-and-why-you-should-use-these-instead-of-dependencyproperties/feed/</wfw:commentRss>
		<slash:comments>4</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[c#]]></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>

		<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>
<iframe src="http://www.facebook.com/plugins/like.php?href=http://www.designersilverlight.com/2009/02/25/adventures-with-json-and-silverlight-and-the-new-york-times/&amp;layout=standard&amp;show_faces=1&amp;width=450&amp;action=like&amp;colorscheme=light&amp;font=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:25px"></iframe><div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Adventures+with+JSON+and+Silverlight+%28and+the+New+York+Times%29+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D550" title="Post to Twitter"><img class="nothumb" src="http://www.designersilverlight.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter.png" alt="Post to Twitter" /></a> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Adventures+with+JSON+and+Silverlight+%28and+the+New+York+Times%29+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D550" title="Post to Twitter">Tweet This Post</a></p></div>]]></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>Swapping Content In the Code Behind in Silverlight</title>
		<link>http://www.designersilverlight.com/2008/10/23/swapping-content-in-the-code-behind-in-silverlight/</link>
		<comments>http://www.designersilverlight.com/2008/10/23/swapping-content-in-the-code-behind-in-silverlight/#comments</comments>
		<pubDate>Fri, 24 Oct 2008 03:20:57 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Advanced Tutorial]]></category>
		<category><![CDATA[How To...]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.designersilverlight.com/2008/10/23/swapping-content-in-the-code-behind-in-silverlight/</guid>
		<description><![CDATA[I fought with this for a while today, so I thought I would toss it up as a solution. I was trying to create a transition between two screens for a project I&#8217;m working on. I created the two ContentControls in my XAML like so: &#60;ContentControl x:Name=&#8221;OldContent&#8221;&#62;       &#60;!&#8211; My Old Content –&#62; &#60;/ContentControl&#62; &#60;ContentControl [...]]]></description>
			<content:encoded><![CDATA[<p>I fought with this for a while today, so I thought I would toss it up as a solution.</p>
<p>I was trying to create a transition between two screens for a project I&#8217;m working on. I created the two ContentControls in my XAML like so:</p>
<p><span style="color: #0000ff;">&lt;</span><span style="color: #a31515;">ContentControl </span><span style="color: #ff0000;">x</span><span style="color: #0000ff;">:</span><span style="color: #ff0000;">Name</span><span style="color: #0000ff;">=&#8221;OldContent&#8221;&gt;<br />
      </span><span style="color: #008000;">&lt;!&#8211; My Old Content –&gt;<br />
</span><span style="color: #0000ff;">&lt;/</span><span style="color: #a31515;">ContentControl</span><span style="color: #0000ff;">&gt;<br />
&lt;</span><span style="color: #a31515;">ContentControl </span><span style="color: #ff0000;">x</span><span style="color: #0000ff;">:</span><span style="color: #ff0000;">Name</span><span style="color: #0000ff;">=&#8221;NewContent&#8221; </span><span style="color: #ff0000;">Opacity</span><span style="color: #0000ff;">=&#8221;0&#8243;&gt;<br />
      </span><span style="color: #008000;">&lt;!&#8211; My New Content &#8211;&gt;<br />
</span><span style="color: #0000ff;">&lt;/</span><span style="color: #a31515;">ContentControl</span><span style="color: #0000ff;">&gt;</span></p>
<p>Let’s say for the sake of this post that I was trying to animate the top content as a fade in and then, after the animation is done,  put the top content into the bottom content so that I could fill the NewContent with something else and have it ready for the next transition.</p>
<p>So… I’m using the following animation.</p>
<p><span style="color: #0000ff;">&lt;</span><span style="color: #a31515;">Storyboard </span><span style="color: #ff0000;">x</span><span style="color: #0000ff;">:</span><span style="color: #ff0000;">Name</span><span style="color: #0000ff;">=&#8221;TransitionContent&#8221;&gt;<br />
   &lt;</span><span style="color: #a31515;">DoubleAnimationUsingKeyFrames </span><span style="color: #ff0000;">BeginTime</span><span style="color: #0000ff;">=&#8221;00:00:00&#8243;<br />
</span><span style="color: #ff0000;">         Storyboard.TargetName</span><span style="color: #0000ff;">=&#8221;NewContent&#8221;<br />
</span><span style="color: #ff0000;">         Storyboard.TargetProperty</span><span style="color: #0000ff;">=&#8221;Opacity&#8221;&gt;<br />
                &lt;</span><span style="color: #a31515;">SplineDoubleKeyFrame </span><span style="color: #ff0000;">KeyTime</span><span style="color: #0000ff;">=&#8221;00:00:00.03&#8243; </span><span style="color: #ff0000;">Value</span><span style="color: #0000ff;">=&#8221;1&#8243; /&gt;<br />
</span><span style="color: #0000ff;">   &lt;/</span><span style="color: #a31515;">DoubleAnimationUsingKeyFrames</span><span style="color: #0000ff;">&gt;<br />
&lt;/</span><span style="color: #a31515;">Storyboard</span><span style="color: #0000ff;">&gt;</span><a href="http://11011.net/software/vspaste"></a></p>
<p>(No Blend stuff today because I’m going a mile a minute for the sake of my looming project deadline.)</p>
<p>I add a “Completed” event handler to my Storyboard so that I can swap the content when the animation is over.</p>
<p><span style="color: #0000ff;">&lt;</span><span style="color: #a31515;">Storyboard </span><span style="color: #ff0000;">x</span><span style="color: #0000ff;">:</span><span style="color: #ff0000;">Name</span><span style="color: #0000ff;">=&#8221;TransitionContent&#8221; </span><span style="color: #ff0000;">Completed</span><span style="color: #0000ff;">=&#8221;TransitionContent_Completed&#8221;&gt;</span></p>
<p>In the C# code behind, I first wrote my content swapping like this:</p>
<p><span style="color: #0000ff;">this</span>.OldContent.Content = <span style="color: #0000ff;">this</span>.NewContent.Content;<br />
<span style="color: #0000ff;">this</span>.NewContent.Content = <span style="color: #0000ff;">null</span>;</p>
<p>And I got the following exception:</p>
<p><em>&#8220;System.ArgumentException was unhandled by user code<br />
      Value does not fall within the expected range.&#8221;</em></p>
<p>I’m putting this in because it took me quite some time to figure out what the problem was. Simply put… Silverlight was yelling at me because I was trying to put the same stuff in the visual tree twice. This would have meant two copies of anything with a x:Name attribute… which would have been made it really hard for me to find anything. So it threw an exception.</p>
<p>Ultimately, I had to create an object to store the value of the NewContent  and then trash the NewContent.Content before I could put it into the OldContent.Content. I’ve re-written the code in such a way that if you wanted to swap the two contents, it will do just that.</p>
<p><span style="color: #0000ff;">private void </span>Storyboard2_Completed(<span style="color: #0000ff;">object </span>sender, <span style="color: #2b91af;">EventArgs </span>e)       <br />
{<br />
            <span style="color: #0000ff;">object </span>oldStuff = <span style="color: #0000ff;">this</span>.OldContent.Content <span style="color: #0000ff;">as object</span>;<br />
            <span style="color: #0000ff;">object </span>newStuff = <span style="color: #0000ff;">this</span>.NewContent.Content <span style="color: #0000ff;">as object</span>;</p>
<p>            <span style="color: #0000ff;">this</span>.OldContent.Content = <span style="color: #0000ff;">null</span>;<br />
            <span style="color: #0000ff;">this</span>.NewContent.Content = <span style="color: #0000ff;">null</span>;</p>
<p>            <span style="color: #0000ff;">this</span>.OldContent.Content = newStuff;<br />
            <span style="color: #0000ff;">this</span>.NewContent.Content = oldStuff;</p>
<p>            <span style="color: #0000ff;">this</span>.NewContent.Opacity = 0;<br />
}</p>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>That last little bit is to reset the NewContent so that it is ready for the next transition.</p>
<p>And that’s all there is to it.</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http://www.designersilverlight.com/2008/10/23/swapping-content-in-the-code-behind-in-silverlight/&amp;layout=standard&amp;show_faces=1&amp;width=450&amp;action=like&amp;colorscheme=light&amp;font=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:25px"></iframe><div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Swapping+Content+In+the+Code+Behind+in+Silverlight+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D372" title="Post to Twitter"><img class="nothumb" src="http://www.designersilverlight.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter.png" alt="Post to Twitter" /></a> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Swapping+Content+In+the+Code+Behind+in+Silverlight+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D372" title="Post to Twitter">Tweet This Post</a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.designersilverlight.com/2008/10/23/swapping-content-in-the-code-behind-in-silverlight/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Tip For Finding Resources for a Control in generic.xaml</title>
		<link>http://www.designersilverlight.com/2008/08/26/tip-for-finding-resources-for-a-control-in-genericxaml/</link>
		<comments>http://www.designersilverlight.com/2008/08/26/tip-for-finding-resources-for-a-control-in-genericxaml/#comments</comments>
		<pubDate>Wed, 27 Aug 2008 00:39:42 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Advanced Tutorial]]></category>
		<category><![CDATA[listview]]></category>
		<category><![CDATA[templates]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[XAML]]></category>

		<guid isPermaLink="false">http://www.designersilverlight.com/2008/08/26/tip-for-finding-resources-for-a-control-in-genericxaml/</guid>
		<description><![CDATA[I&#8217;ve recently be working on changing the ControlTemplate of a GridViewColumnHeader in a custom ListView that we&#8217;ve been working on. (The ListView was rewritten for sorting, so that&#8217;s why it had to be custom.) One of the things we had to do was swap out ControlTemplates so that we could display a caret to indicate [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently be working on changing the ControlTemplate of a GridViewColumnHeader in a custom ListView that we&#8217;ve been working on. (The ListView was rewritten for sorting, so that&#8217;s why it had to be custom.)</p>
<p>One of the things we had to do was swap out ControlTemplates so that we could display a caret to indicate ascending or descending lists. We ended up deciding on ControlTemplates over DataTemplates because the DataTemplates would only work for ListViews that had no custom DataTemplates for the headers. We&#8217;re doing all sorts of crazy stuff with our headers and we need to preserve our DataTemplates, so this wasn&#8217;t an option. </p>
<p>In any case, I was having no luck finding the resource when I named it this way:</p>
<blockquote><p><span style="color: blue">&lt;</span><span style="color: #a31515">ControlTemplate </span><span style="color: red">x</span><span style="color: blue">:</span><span style="color: red">Key</span><span style="color: blue">=&quot;MyCustomControlTemplate&quot; <span style="color: red">TargetType</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">x</span><span style="color: blue">:</span><span style="color: #a31515">Type </span><span style="color: red">GridViewColumnHeader</span><span style="color: blue">}&quot;&gt;</span></span></p>
</blockquote>
<p> <a href="http://11011.net/software/vspaste"></a>
<p>I was using the following code to try and find the resource.</p>
<blockquote><p><span style="color: #2b91af">ControlTemplate </span>myNewTemplate = (<span style="color: #2b91af">ControlTemplate</span>)Resources[<span style="color: #a31515">&quot;MyCustomTemplate&quot;</span>];</p>
</blockquote>
<p> <a href="http://11011.net/software/vspaste"></a>
<p>However, we were able to solve the problem by naming the resource this way</p>
<blockquote><p><span style="color: blue">&lt;</span><span style="color: #a31515">ControlTemplate </span><span style="color: red">x</span><span style="color: blue">:</span><span style="color: red">Key</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">ComponentResourceKey </span><span style="color: red">TypeInTargetAssembly</span><span style="color: blue">={</span><span style="color: #a31515">x</span><span style="color: blue">:</span><span style="color: #a31515">Type </span><span style="color: red">local</span><span style="color: blue">:</span><span style="color: red">MyCustomListView</span><span style="color: blue">}, </span><span style="color: red">ResourceId</span><span style="color: blue">=MyCustomControlTemplate}&quot;       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: blue"><span style="color: red">TargetType</span><span style="color: blue">=&quot;{</span><span style="color: #a31515">x</span><span style="color: blue">:</span><span style="color: #a31515">Type </span><span style="color: red">GridViewColumnHeader</span><span style="color: blue">}&quot;&gt;</span></span></p>
<p>   <a href="http://11011.net/software/vspaste"></a></p></blockquote>
<p> <a href="http://11011.net/software/vspaste"></a>
<p>And then using this code to access it.</p>
<blockquote><p><span style="color: #2b91af">ComponentResourceKey </span>myCustomTemplateKey = <span style="color: blue">new </span><span style="color: #2b91af">ComponentResourceKey</span>(<span style="color: blue">typeof</span>(<span style="color: #2b91af">SortableListView</span>), <span style="color: #a31515">&quot;MyCustomControlTemplate&quot;</span>);      <br /><span style="color: #2b91af">ControlTemplate </span>myNewTemplate = (<span style="color: #2b91af">ControlTemplate</span>)<span style="color: blue">this</span>.TryFindResource(myCustomTemplateKey);</p>
</blockquote>
<p> <a href="http://11011.net/software/vspaste"></a>
<p>Just thought I&#8217;d pass it along.</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http://www.designersilverlight.com/2008/08/26/tip-for-finding-resources-for-a-control-in-genericxaml/&amp;layout=standard&amp;show_faces=1&amp;width=450&amp;action=like&amp;colorscheme=light&amp;font=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:25px"></iframe><div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Tip+For+Finding+Resources+for+a+Control+in+generic.xaml+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D267" title="Post to Twitter"><img class="nothumb" src="http://www.designersilverlight.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter.png" alt="Post to Twitter" /></a> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Tip+For+Finding+Resources+for+a+Control+in+generic.xaml+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D267" title="Post to Twitter">Tweet This Post</a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.designersilverlight.com/2008/08/26/tip-for-finding-resources-for-a-control-in-genericxaml/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Triggering Events and Updating Bindings</title>
		<link>http://www.designersilverlight.com/2008/02/27/triggering-events-and-updating-bindings/</link>
		<comments>http://www.designersilverlight.com/2008/02/27/triggering-events-and-updating-bindings/#comments</comments>
		<pubDate>Wed, 27 Feb 2008 08:48:14 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Advanced Tutorial]]></category>
		<category><![CDATA[Data Binding]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[IsVisibleChanged]]></category>
		<category><![CDATA[XAML]]></category>

		<guid isPermaLink="false">http://www.designersilverlight.com/2008/02/27/triggering-events-and-updating-bindings/</guid>
		<description><![CDATA[Sorry for the extended absence&#8230; I&#8217;m been working myself to the bone on the Veracity submission to the Show Off at MIX08. We&#8217;ll have some really kicking stuff to play around with at the Open Space&#8230; you should drop by and say hi. I look something like this: At least I look like that at 1:30 [...]]]></description>
			<content:encoded><![CDATA[<p>Sorry for the extended absence&#8230; I&#8217;m been working myself to the bone on the Veracity submission to the <a href="http://visitmix.com/2008/showoff/">Show Off at MIX08</a>. We&#8217;ll have some really kicking stuff to play around with at the Open Space&#8230; you should drop by and say hi. I look something like this:</p>
<p><img src="http://www.designersilverlight.com/wp-content/uploads/2008/02/meat130.png" alt="MeAt130" /></p>
<p>At least I look like that at 1:30 AM.</p>
<p>OK, so I felt the need to pipe up at this ungodly hour with a public service announcement about events and databinding. I just spent a good hour trying to figure out a workaround for this problem. I had a method being called on my &#8220;IsVisibleChanged&#8221; event. I was planning on take that item and performing some code-behind logic based on the databound Canvas.Left and Canvas.Top properties.</p>
<p>WPF put the smack-down on that action. The data binding wouldn&#8217;t update before the event was called. And I couldn&#8217;t make it. Not without some kind of Invoke&#8230; which, I must admit, scares the ever-loving crap out of me, so I didn&#8217;t try&#8230; also because I didn&#8217;t know how to use it.</p>
<p>My advice if your databinding won&#8217;t update? Find some way of going to the data itself. If you&#8217;re binding to something in the XAML, you should be able to find the object you&#8217;re binding to in the code as well. Just go straight to the source.</p>
<p>Anyone have a better idea or suggestion? I&#8217;m totally open to hearing it.</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http://www.designersilverlight.com/2008/02/27/triggering-events-and-updating-bindings/&amp;layout=standard&amp;show_faces=1&amp;width=450&amp;action=like&amp;colorscheme=light&amp;font=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:25px"></iframe><div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Triggering+Events+and+Updating+Bindings+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D174" title="Post to Twitter"><img class="nothumb" src="http://www.designersilverlight.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter.png" alt="Post to Twitter" /></a> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Triggering+Events+and+Updating+Bindings+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D174" title="Post to Twitter">Tweet This Post</a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.designersilverlight.com/2008/02/27/triggering-events-and-updating-bindings/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>WPF Wii Multi-Point Tutorials, Part 2: Writing a Code-less Wiimote Program</title>
		<link>http://www.designersilverlight.com/2008/01/31/wpf-wii-multi-point-tutorials-part-2-writing-a-code-less-wiimote-program/</link>
		<comments>http://www.designersilverlight.com/2008/01/31/wpf-wii-multi-point-tutorials-part-2-writing-a-code-less-wiimote-program/#comments</comments>
		<pubDate>Thu, 31 Jan 2008 09:12:24 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Advanced Tutorial]]></category>
		<category><![CDATA[How To...]]></category>
		<category><![CDATA[multipoint]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[XAML]]></category>
		<category><![CDATA[]]></category>
		<category><![CDATA[Data Binding]]></category>
		<category><![CDATA[INotifyPropertyChanged]]></category>
		<category><![CDATA[multi-point]]></category>
		<category><![CDATA[Wiimote]]></category>

		<guid isPermaLink="false">http://www.designersilverlight.com/2008/01/31/wpf-wii-multi-point-tutorials-part-2-writing-a-code-less-wiimote-program/</guid>
		<description><![CDATA[ OK, I hope no one is using my last post as an example of what you should be doing when interfacing the Wiimote with WPF. Because it was completely hack-tastic.  Instead, use my new WPF/Wii library. It uses the WPF INotifyPropertyChanged interface to act as an interface so that we can bind the Wiimote data [...]]]></description>
			<content:encoded><![CDATA[<p> OK, I hope no one is using my last post as an example of what you should be doing when interfacing the Wiimote with WPF. Because it was completely hack-tastic.</p>
<p> Instead, use my <a href="http://www.designersilverlight.com/wp-content/uploads/2008/01/wpf_wii_binding_library.zip" title="WPF_Wii_Binding_Library">new WPF/Wii library</a>. It uses the WPF INotifyPropertyChanged interface to act as an interface so that we can bind the Wiimote data directly to the XAML. More on that in a little bit, but first&#8230;</p>
<p>In this post, we&#8217;ll walk through creating a basic multi-point capable app that uses the Wiimote as an input device. What is really unique about this post is the fact that we&#8217;re going to do this in a way that requires absolutely no code whatsoever on your part.</p>
<p>That&#8217;s right. No code. At all. Zero knowledge of C# required.</p>
<p><span id="more-99"></span>First, download the &#8220;<a href="http://www.designersilverlight.com/wp-content/uploads/2008/01/wpf_wii_binding_library.zip" title="WPF_Wii_Binding_Library">WPF_Wii_Binding_Library</a>&#8221; and the &#8220;<a href="http://blogs.msdn.com/coding4fun/archive/2007/03/14/1879033.aspx">WiimoteLib</a>&#8221; dlls, unzip them and put them in the same folder (somewhere you can find them).</p>
<p>Open up Blend or Visual Studio 2008 and start a new application. (Visual Studio 2008 is reccomended for this tutorial.)</p>
<p>Now, head right on over to your <strong>&#8220;Project&#8221;</strong> tab or <strong>&#8220;Solution Explorer&#8221;</strong> and right click on the <strong>&#8220;References&#8221;</strong> folder and click <strong>&#8220;Add Reference&#8221;</strong>.</p>
<p>Blend</p>
<p><img src="http://www.designersilverlight.com/wp-content/uploads/2008/01/references.png" alt="BlendReferences" /></p>
<p>VS 2008</p>
<p><img src="http://www.designersilverlight.com/wp-content/uploads/2008/01/vsreferences.png" alt="VS2008References" /></p>
<p>Browse for the reference &#8220;WPF_Wii_Binding_Library&#8221; (the one you just downloaded) and click &#8220;Open&#8221;.</p>
<p>In your main composition, add a &#8220;Canvas&#8221;.</p>
<p><font color="#0000ff">&lt;</font><font color="#800000">Canvas</font><font color="#0000ff">&gt;</font><br />
<font color="#0000ff">&lt;/</font><font color="#800000">Canvas</font><font color="#0000ff">&gt;</font></p>
<p>Draw three ellipses and give them three different colors.</p>
<p><img src="http://www.designersilverlight.com/wp-content/uploads/2008/01/ellipses.png" alt="Ellipse" /></p>
<p>Define the following XML namespace Window:</p>
<p><font color="#0000ff">&lt;</font><font color="#800000">Window</font> &#8230;<br />
 <font color="#ff0000">xmlns</font><font color="#0000ff">:</font><font color="#ff0000">Wii</font>=&#8221;<font color="#0000ff">clr-namespace:WPF_Wii_Binding_Library;assembly=WPF_Wii_Binding_Library</font>&#8220;<font color="#0000ff">&gt;</font></p>
<p>In your Resources, add the following resources (you can just copy this code if you want):</p>
<p><font color="#0000ff">&lt;</font><font color="#800000">Wii</font><font color="#0000ff">:</font><font color="#800000">WPFWii <font color="#ff0000">x</font></font><font color="#0000ff">:</font><font color="#ff0000">Key</font>=&#8221;<font color="#0000ff">WiiData</font>&#8221; <font color="#ff0000">IsMultiPointMode</font>=&#8221;<font color="#0000ff">True</font>&#8220;  <font color="#0000ff">/&gt;</font><br />
<font color="#0000ff">&lt;</font><font color="#800000">BooleanToVisibilityConverter</font> <font color="#ff0000">x</font><font color="#0000ff">:</font><font color="#ff0000">Key</font>=&#8221;<font color="#0000ff">boolToVis</font>&#8221; <font color="#0000ff">/&gt;</font></p>
<p>Note: If you&#8217;re using anything less than <a href="http://www.microsoft.com/Downloads/details.aspx?familyid=65177E23-C116-475A-9057-5A5071A379F6&amp;displaylang=en">Blend 2 December Preview</a>, I can make no guarantees that this solution won&#8217;t throw a fit. Even with the B2DP, it is still having issues. The best way to do this is really through Visual Studio 2008. But if you don&#8217;t mind just typing all your XAML, Blend will still compile fine.</p>
<p>Find your first Ellipse in the XAML. Add the following Visibility property:</p>
<p><font color="#ff0000">Visibility</font>=&#8221;<font color="#cc9436">{Binding Source={StaticResource WiiData}, Path=IR1Found, Converter={StaticResource boolToVis}}</font>&#8221;</p>
<p>What this is doing is connecting to the Wiimote and checking to see if it can see your first infrared LED. I suggest using the sensor bar that comes with the Wii for the time being.  If it can see the LED, your ellipse will be visible. If it can&#8217;t, it won&#8217;t.</p>
<p>It is now time for us to make the magic. Replace whatever &#8220;Canvas.Left&#8221; and &#8220;Canvas.Top&#8221; properties so that your Ellipse looks like the following:</p>
<p><font color="#0000ff">&lt;</font><font color="#800000">Ellipse </font><font color="#800000"><font color="#ff0000">Width<font color="#0000ff">=&#8221;30&#8243;</font><font color="#ff0000"><br />
      Height</font><font color="#0000ff">=&#8221;30&#8243;</font><font color="#ff0000"><br />
      Fill</font><font color="#0000ff">=&#8221;#FF0045FF&#8221;</font><font color="#ff0000"><br />
      </font></font></font><font color="#800000"><font color="#ff0000"><font color="#ff0000">Stroke</font><font color="#0000ff">=&#8221;#FF000000&#8243;</font></font></font><font color="#800000"><font color="#ff0000"><br />
      </font></font><font color="#ff0000">Visibility</font>=&#8221;<font color="#cc9436">{Binding Source={StaticResource WiiData}, Path=IR1Found, Converter={StaticResource boolToVis}}</font>&#8221;<br />
      <font color="#ff0000">Canvas.Left</font>=&#8221;<font color="#cc9436">{Binding Source={StaticResource WiiData}, Path=IR1Position.X, Mode=OneWay}</font>&#8221;<br />
      <font color="#ff0000">Canvas.Top</font>=&#8221;<font color="#cc9436">{Binding Source={StaticResource WiiData}, Path=IR1Position.Y, Mode=OneWay}</font>&#8221; /&gt;</p>
<p>We&#8217;re binding that ellipse to the X,Y position the Wiimote has tracked the first infrared LED. Let&#8217;s do the same with our second infrared LED and the midpoint between the two LEDs.</p>
<p>LED number 2:</p>
<p><font color="#ff0000">Visibility</font>=&#8221;<font color="#cc9436">{Binding Source={StaticResource WiiData}, Path=IR2Found, Converter={StaticResource boolToVis}}</font>&#8221;<br />
<font color="#ff0000">Canvas.Left</font>=&#8221;<font color="#cc9436">{Binding Source={StaticResource WiiData}, Path=IR2Position.X, Mode=OneWay}</font>&#8221;<br />
<font color="#ff0000">Canvas.Top</font>=&#8221;<font color="#cc9436">{Binding Source={StaticResource WiiData}, Path=IR2Position.Y, Mode=OneWay}</font>&#8221;</p>
<p>Mid point:</p>
<p><font color="#ff0000">Visibility</font>=&#8221;<font color="#cc9436">{Binding Source={StaticResource WiiData}, Path=MidPointFound, Converter={StaticResource boolToVis}}</font>&#8221;<br />
<font color="#ff0000">Canvas.Left</font>=&#8221;<font color="#cc9436">{Binding Source={StaticResource WiiData}, Path=MidPointPosition.X, Mode=OneWay}</font>&#8221;<br />
<font color="#ff0000">Canvas.Top</font>=&#8221;<font color="#cc9436">{Binding Source={StaticResource WiiData}, Path=MidPointPosition.Y, Mode=OneWay}</font>&#8221;</p>
<p>Guess what?</p>
<p>You&#8217;re done. That&#8217;s all you need.  Just make sure your Wiimote is connected to your computer and you&#8217;re ready to rock and roll. (Need help with that part? <a href="http://www.designersilverlight.com/2008/01/28/wpf-multi-point-tutorials-part-1-connecting-the-wiimote-controller/">Check here.</a>)</p>
<p>You&#8217;ve just created a Wiimote application with 0 (zero) lines of custom code and (depending on your formatting) less than 20 lines of XAML markup.</p>
<p>The next tutorial may take a week or more to make since I need to wait for my infrared lights and circuits to come in in order to build an infrared array. (Don&#8217;t worry, I&#8217;ll be posting a tutorial on how to build that infrared array. That tutorial will assume that you have never touched a circuit in your life.)</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http://www.designersilverlight.com/2008/01/31/wpf-wii-multi-point-tutorials-part-2-writing-a-code-less-wiimote-program/&amp;layout=standard&amp;show_faces=1&amp;width=450&amp;action=like&amp;colorscheme=light&amp;font=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:25px"></iframe><div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=WPF+Wii+Multi-Point+Tutorials%2C+Part+2%3A+Writing+a+Code-less+Wiimote+Program+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D99" title="Post to Twitter"><img class="nothumb" src="http://www.designersilverlight.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter.png" alt="Post to Twitter" /></a> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=WPF+Wii+Multi-Point+Tutorials%2C+Part+2%3A+Writing+a+Code-less+Wiimote+Program+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D99" title="Post to Twitter">Tweet This Post</a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.designersilverlight.com/2008/01/31/wpf-wii-multi-point-tutorials-part-2-writing-a-code-less-wiimote-program/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Embedded ListView Columns (Columns Within Columns)</title>
		<link>http://www.designersilverlight.com/2008/01/25/embedded-listview-columns-columns-within-columns/</link>
		<comments>http://www.designersilverlight.com/2008/01/25/embedded-listview-columns-columns-within-columns/#comments</comments>
		<pubDate>Sat, 26 Jan 2008 03:58:08 +0000</pubDate>
		<dc:creator>Matthias Shapiro</dc:creator>
				<category><![CDATA[Advanced Tutorial]]></category>
		<category><![CDATA[Blend]]></category>
		<category><![CDATA[How To...]]></category>
		<category><![CDATA[listview]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[XAML]]></category>
		<category><![CDATA[advanced]]></category>
		<category><![CDATA[columns]]></category>
		<category><![CDATA[Data Template]]></category>
		<category><![CDATA[embedded columns]]></category>
		<category><![CDATA[GridViewColumnHeader]]></category>
		<category><![CDATA[style]]></category>
		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">http://www.designersilverlight.com/2008/01/25/embedded-listview-columns-columns-within-columns/</guid>
		<description><![CDATA[Please Read: Strangely, when you do a Google search for &#8220;wpf&#8221; and &#8220;listview&#8221;, this is one of the top links. This is odd because this particular post is kind of an advanced tutorial. If you&#8217;re looking for more general information on styling the wpf listview, check out this post. It is probably much closer to what you&#8217;re looking [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Please Read:</strong> Strangely, when you do a Google search for &#8220;wpf&#8221; and &#8220;listview&#8221;, this is one of the top links. This is odd because this particular post is kind of an advanced tutorial. If you&#8217;re looking for more general information on styling the wpf listview, check out <a href="http://www.designersilverlight.com/2007/12/06/the-wpf-designers-guide-to-styling-the-your-favorite-adjectival-swear-word-here-listview/">this post</a>. It is probably much closer to what you&#8217;re looking for.</p>
<p>This is a bit of an advanced tutorial. I&#8217;m putting it up because I just figured out how to do it and I want to share. You can also <a href="http://www.designersilverlight.com/wp-content/uploads/2008/01/listviewembeddedcolumns.zip" title="Embedded Columns Project Files">download the project files for this tutorial</a> (in zip format&#8230; requires .Net 3.5).</p>
<p>Recently, I received from my user experience designers a wireframe that looked something like this:</p>
<p><img src="http://www.designersilverlight.com/wp-content/uploads/2008/01/embededwireframe.png" alt="EmbeddedWireframe" /></p>
<p>As you can see, there are embedded categories (categories within categories) here. I considered many solutions (hacks), but I found that a deeper understanding of the ListView and how it works would allow me to resolve this issue very simply (and without even touching the code behind).<span id="more-82"></span></p>
<h2>&lt;Exposition&gt;</h2>
<p>So… within every ListView, there is a Gridview that holds a set of GridViewColumns. What I didn&#8217;t realize until a couple days ago was that when you put the GridViewColumns into the GridView, you&#8217;re implicitly handing the GridView a GridViewColumnCollection.This collection is then referenced by the GridViewHeaderRowPresenter (located deep inside the ListView Template) and the GridViewRowPresenter, which is located inside the ListViewItem template (which I usually get to through the ItemContainerStyle… see here for more details).</p>
<p>Anyway, these two presenters databind back up to the GridView to grab the GridViewColumns that we&#8217;ve defined.</p>
<p>Once I understood that, I realized that I could create a GridViewColumnCollection as a resource and the reference it inside some custom templates.</p>
<p>At this point, I get the feeling that some readers are just hearing &#8220;blah blah blah&#8221;. It&#8217;s actually easier to demonstrate than it is to explain. So let&#8217;s get to it.</p>
<h2>&lt;/Exposition&gt;</h2>
<p>We&#8217;re going to grab the header image from the New York Times RSS feed and display it in its own column while displaying all the accompanying header data under a broad Images header with sub-columns called Title, URL, and Link. It will look like this:</p>
<p><img src="http://www.designersilverlight.com/wp-content/uploads/2008/01/embededfinal.png" alt="EmbeddedFinalScreen" /></p>
<h2>Resources</h2>
<p>First, lets deal with all the resources we need. Take note that this part will be pretty XAML heavy.</p>
<p>Create a resources section. If mine isn&#8217;t in a separate resource dictionary, I usually just create it at the top of the window as seen below.</p>
<p><font color="#0000ff">&lt;</font><font color="#800000">Window.Resources</font><font color="#0000ff">&gt;</font><br />
<font color="#0000ff">&lt;/</font><font color="#800000">Window.Resources</font><font color="#0000ff">&gt;</font></p>
<p>All your resources stuff will go in between those two tags. </p>
<p>Create a GridViewColumnCollection in the resources:</p>
<p><font color="#0000ff">&lt;</font><font color="#800000">GridViewColumnCollection</font> <font color="#ff0000">x:Key</font>=&#8221;<font color="#0000ff">ImageColumnCollection</font>&#8220;<font color="#0000ff">&gt;<br />
      &lt;</font><font color="#800000">GridViewColumn </font><font color="#ff0000">Header</font>=&#8221;<font color="#0000ff">Title</font>&#8221; <font color="#0000ff">/&gt;<br />
      </font><font color="#0000ff">&lt;</font><font color="#800000">GridViewColumn </font><font color="#ff0000">Header</font>=&#8221;<font color="#0000ff">URL</font>&#8221; <font color="#0000ff">/&gt;<br />
</font><font color="#0000ff"><font color="#000000">      </font>&lt;</font><font color="#800000">GridViewColumn</font> <font color="#ff0000">Header</font>=&#8221;<font color="#0000ff">Link</font>&#8221; <font color="#0000ff">/&gt;<br />
</font><font color="#0000ff">&lt;/</font><font color="#800000">GridViewColumnCollection</font><font color="#0000ff">&gt;</font></p>
<p>Now, go through the motions to getting your RSS feed. Follow the steps I lay out in <a href="http://www.designersilverlight.com/2007/12/13/how-do-i-connect-an-rss-feed-to-my-listviewlistboxwhatever/">my post on getting the New York Times RSS feeds</a>, except that, instead of clicking on the <strong>item(Array)</strong>, click on the <strong>image </strong>section. Continue on with the <strong>&#8220;Create Data Template&#8221;</strong> option to get your RSS bindings automated.</p>
<p>Grab the bindings in this template and put them into the DisplayMemberBinding property in the GridViewColumns. Your GridViewColumnCollection should now look like this:</p>
<p><font color="#0000ff">&lt;</font><font color="#800000">GridViewColumnCollection</font> <font color="#ff0000">x:Key</font>=&#8221;<font color="#0000ff">ImageColumnCollection</font>&#8220;<font color="#0000ff">&gt;<br />
      &lt;</font><font color="#800000">GridViewColumn </font><font color="#ff0000">Header</font>=&#8221;<font color="#0000ff">Title</font>&#8221;<br />
            <font color="#ff0000">DisplayMemberBinding</font>=&#8221;<font color="#b8860b">{Binding Mode=OneWay, XPath=title}</font>&#8220;  <font color="#0000ff">/&gt;<br />
      </font><font color="#0000ff">&lt;</font><font color="#800000">GridViewColumn </font><font color="#ff0000">Header</font>=&#8221;<font color="#0000ff">URL</font>&#8221;<br />
            <font color="#ff0000">DisplayMemberBinding</font>=&#8221;<font color="#b8860b">{Binding Mode=OneWay, XPath=url}</font>&#8220;   <font color="#0000ff">/&gt;<br />
</font><font color="#0000ff"><font color="#000000">      </font>&lt;</font><font color="#800000">GridViewColumn</font> <font color="#ff0000">Header</font>=&#8221;<font color="#0000ff">Link</font>&#8221;<br />
            <font color="#ff0000">DisplayMemberBinding</font>=&#8221;<font color="#b8860b">{Binding Mode=OneWay, XPath=link}</font>&#8220;   <font color="#0000ff">/&gt;<br />
</font><font color="#0000ff">&lt;/</font><font color="#800000">GridViewColumnCollection</font><font color="#0000ff">&gt;</font></p>
<p>Now that we have our GridViewColumnCollection in place, let&#8217;s create some stuff that will actually use it.</p>
<p>Create a new empty Data Template. If you&#8217;re still uncomfortable digging into the XAML itself, you can just type the following in:</p>
<p><font color="#0000ff">&lt;</font><font color="#800000">DataTemplate</font> <font color="#ff0000">x:Key</font>=&#8221;<font color="#0000ff">MultiColumnHeaderTemplate</font>&#8220;<font color="#0000ff">&gt;</font><br />
<font color="#0000ff">&lt;/</font><font color="#800000">DataTemplate</font><font color="#0000ff">&gt;</font></p>
<p>And then go to your resources tab and double click on it to make it accessible in the design mode.</p>
<p><img src="http://www.designersilverlight.com/wp-content/uploads/2008/01/embededresources.png" alt="ResourcesTab" /></p>
<p>The way I set this up was very simple. I put in a grid with two rows set to auto, put a TextBlock in the top one and a GridViewHeaderRowPresenter into the other one. The TextBlock at the top is bound to the Header Content with a simple binding:</p>
<p><font color="#ff0000">Text</font>=&#8221;<font color="#b8860b">{Binding}</font>&#8221;</p>
<p>The real trick is that the GridViewHeaderRowPresenter is bound to our Columns resource by simply entering the following property:</p>
<p><font color="#ff0000">Columns</font>=&#8221;<font color="#b8860b">{DynamicResource ImageColumnCollection}</font>&#8221;</p>
<p>Now, each column in that collection will send its header information to be handled by this particular GridViewHeaderRowPresenter.</p>
<p>Our final header data template looks something like this:</p>
<p><font color="#0000ff">&lt;</font><font color="#800000">DataTemplate</font> <font color="#ff0000">x:Key</font>=&#8221;<font color="#0000ff">MultiColumnHeaderTemplate</font>&#8220;<font color="#0000ff">&gt;<br />
      &lt;</font><font color="#800000">Grid</font><font color="#0000ff">&gt;<br />
            &lt;</font><font color="#800000">Grid.RowDefinitions</font><font color="#0000ff">&gt;<br />
                  &lt;</font><font color="#800000">RowDefinition</font> <font color="#ff0000">Height</font>=&#8221;<font color="#0000ff">Auto</font>&#8221; <font color="#0000ff">/&gt;<br />
                  &lt;</font><font color="#800000">RowDefinition</font> <font color="#ff0000">Height</font>=&#8221;<font color="#0000ff">Auto</font>&#8221; <font color="#0000ff">/&gt;<br />
            &lt;/</font><font color="#800000">Grid.RowDefinitions</font><font color="#0000ff">&gt;<br />
            &lt;</font><font color="#800000">TextBlock</font> <font color="#ff0000">Text</font>=&#8221;<font color="#b8860b">{Binding}</font>&#8221;<br />
                  <font color="#ff0000">HorizontalAlignment</font>=&#8221;<font color="#0000ff">Center</font>&#8220;<font color="#0000ff">/&gt;<br />
            &lt;</font><font color="#800000">GridViewHeaderRowPresenter</font> <font color="#ff0000">Grid.Row</font>=&#8221;<font color="#0000ff">1</font>&#8221;<br />
                  <font color="#ff0000">HorizontalAlignment</font>=&#8221;<font color="#0000ff">Stretch</font>&#8221;<br />
                  <font color="#ff0000">Columns</font>=&#8221;<font color="#b8860b">{DynamicResource ImageColumnCollection}</font>&#8221; <font color="#0000ff">/&gt;                                       <br />
      &lt;/</font><font color="#800000">Grid</font><font color="#0000ff">&gt;<br />
&lt;/</font><font color="#800000">DataTemplate</font><font color="#0000ff">&gt;</font></p>
<p>That takes care of our headers… what about our item data? How do I get this data to fit into the proper columns? This was a problem I struggled with for quite some time until a friend of mine pointed out that the GridViewRowPresenter in the ItemTemplate could be extracted and used where ever I wanted to use it. Resultingly, I could place it into the CellTemplate of my Image Information column to compliment the placement of the GridViewHeaderRowPresenter in the Header of that same column.</p>
<p>If you&#8217;re following along and trying hard to learn, create a new DataTemplate and just toss a GridViewRowPresenter into it, binding it to the same column collection. Or you can just copy and paste the XAML below. Your choice.</p>
<p><font color="#0000ff">&lt;</font><font color="#800000">DataTemplate</font> <font color="#ff0000">x:Key</font>=&#8221;<font color="#0000ff">MultiColumnCellTemplate</font>&#8220;<font color="#0000ff">&gt;<br />
      &lt;</font><font color="#800000">Grid</font><font color="#0000ff">&gt;<br />
</font><font color="#0000ff">            &lt;</font><font color="#800000">GridViewRowPresenter</font> <font color="#ff0000">HorizontalAlignment</font>=&#8221;<font color="#0000ff">Stretch</font>&#8221;<br />
                  <font color="#ff0000">Columns</font>=&#8221;<font color="#b8860b">{DynamicResource ImageColumnCollection}</font>&#8221; <font color="#0000ff">/&gt;                                       <br />
      &lt;/</font><font color="#800000">Grid</font><font color="#0000ff">&gt;<br />
&lt;/</font><font color="#800000">DataTemplate</font><font color="#0000ff">&gt;</font> </p>
<p>This will be the basis of our embedded column design. We&#8217;re almost there, let&#8217;s just tie up some resources loose ends.</p>
<p>In order to get the image to show up in the &#8220;Image&#8221; column, we need to create a handy little DataTemplate to display that image. Simply have an image in the Data Template and have the Source property point to the url binding for the image like so:</p>
<p><font color="#0000ff">&lt;</font><font color="#800000">DataTemplate</font> <font color="#ff0000">x:Key</font>=&#8221;<font color="#0000ff">ImageTemplate</font>&#8220;<font color="#0000ff">&gt;<br />
      </font><font color="#0000ff">&lt;</font><font color="#800000">Image</font> <font color="#ff0000">Source</font>=&#8221;<font color="#b8860b">{Binding Mode=OneWay, XPath=url}</font>&#8221; <font color="#0000ff">/&gt;                                       <br />
</font><font color="#0000ff">&lt;/</font><font color="#800000">DataTemplate</font><font color="#0000ff">&gt;</font> </p>
<p>OK… we are now ready to actually do something outside of our resources. Don&#8217;t worry, this will be fast.</p>
<h2>Main XAML Composition</h2>
<p>If you haven&#8217;t already drawn a ListView in your main window, do so now.  Right click on the ListView and  go to <strong>Bind ItemSource to Data…</strong></p>
<p> <img src="http://www.designersilverlight.com/wp-content/uploads/2007/12/itemsourcemenu.png" alt="ItemSourceMenu" /></p>
<p>In the resulting pop-up choose your RSS Feed (mine is called NYTTech) and select the image option from it.  (If you haven&#8217;t added the RSS feed, just copy and paste the line below to the top of your resources.)</p>
<p><font color="#0000ff">&lt;</font><font color="#800000">XmlDataProvider</font> <font color="#ff0000">x:Key</font>=&#8221;<font color="#0000ff">NYTTech</font>&#8221;<br />
      <font color="#ff0000">d:IsDataSource</font>=&#8221;<font color="#0000ff">True</font>&#8221;<br />
      <font color="#ff0000">Source</font>=&#8221;<font color="#0000ff">http://www.nytimes.com/services/xml/rss/nyt/Technology.xml</font>&#8221; <font color="#0000ff">/&gt;</font></p>
<p>Now, in the interest of time, we&#8217;re going to go right into the XAML. If you&#8217;re interested in seeing how to do this using Blend only, <a href="http://www.designersilverlight.com/2007/12/13/styling-a-listview-column-using-blend/">check this post out</a>. Trust me, it&#8217;s not worth it.</p>
<p>Because we&#8217;ve already constructed most of what we need in the resources, we only need to do a couple things here. First, create two GridViewColumns, one with</p>
<p><font color="#ff0000">Header</font>=&#8221;<font color="#0000ff">Image</font>&#8220; </p>
<p>and another with</p>
<p><font color="#ff0000">Header</font>=&#8221;<font color="#0000ff">Image Information</font>&#8221;</p>
<p>In the Image column, set</p>
<p><font color="#ff0000">CellTemplate</font>=&#8221;{StaticResource ImageTemplate}&#8221;</p>
<p>In the Image Information column (which is our column with embedded columns), we just need to point to our already constructed templates like so:</p>
<p><font color="#0000ff">&lt;</font><font color="#800000">GridViewColumnHeader <font color="#ff0000">Header<font color="#000000">=&#8221;</font><font color="#0000ff">Image Information</font><font color="#000000">&#8220;</font> <br />
      HeaderTemplate<font color="#000000">=&#8221;</font><font color="#b8860b">{StaticResource MultiColumnHeaderTemplate}</font><font color="#000000">&#8220; <br />
      </font></font></font><font color="#0000ff"><font color="#800000"><font color="#ff0000">CellTemplate</font><font color="#000000">=&#8221;</font><font color="#b8860b">{StaticResource MultiColumnCellTemplate}</font><font color="#000000">&#8221; </font></font><font color="#0000ff">&gt;</font></font></p>
<p>Go ahead and run the project and you&#8217;ll see that we have one tiny little problem left.</p>
<p>Because the default content alignment of the header style is to center everything, all of our columns are misaligned. To solve this problem, copy the following style into your resources:</p>
<p><font color="#0000ff">&lt;</font><font color="#800000">Style </font><font color="#ff0000">x:Key</font>=&#8221;<font color="#0000ff">StretchHeaderStyle</font>&#8221; <font color="#ff0000">TargetType</font>=&#8221;<font color="#b8860b">{x:Type GridViewColumnHeader}</font>&#8220;<font color="#0000ff">&gt;<br />
      &lt;</font><font color="#800000">Setter</font><font color="#ff0000"> Property</font>=&#8221;<font color="#0000ff">HorizontalContentAlignment</font>&#8221; <font color="#ff0000">Value</font>=&#8221;<font color="#0000ff">Stretch</font>&#8220;<font color="#0000ff">/&gt;<br />
&lt;/</font><font color="#800000">Style</font><font color="#0000ff">&gt;</font></p>
<p>And point to it in your ListView GridView with a :</p>
<p><font color="#ff0000">ColumnHeaderContainerStyle</font>=&#8221;<font color="#b8860b">{StaticResource StretchHeaderStyle}</font>&#8221;</p>
<p>At the end, your ListView should look like this:</p>
<p><font color="#0000ff">&lt;</font><font color="#800000">ListView</font> <font color="#ff0000">IsSynchonizedWithCurrentItem</font>=&#8221;<font color="#0000ff">True</font>&#8220;<font color="#0000ff">&gt;<br />
      &lt;</font><font color="#800000">ListView.View</font><font color="#0000ff">&gt;<br />
            &lt;</font><font color="#800000">GridView <font color="#ff0000">ColumnHeaderContainerStyle</font><font color="#000000">=&#8221;</font><font color="#b8860b">{StaticResource StretchHeaderStyle}</font><font color="#000000">&#8221; </font></font><font color="#0000ff">&gt;<br />
                  &lt;<font color="#800000">GridViewColumn <font color="#ff0000">Header<font color="#000000">=&#8221;</font><font color="#0000ff">Image</font><font color="#000000">&#8220;</font><br />
                        </font></font></font><font color="#0000ff"><font color="#800000"><font color="#ff0000">CellTemplate</font><font color="#000000">=&#8221;</font><font color="#b8860b">{StaticResource ImageTemplate}</font><font color="#000000">&#8221; </font></font><font color="#0000ff">&gt;<br />
                   &lt;<font color="#800000">GridViewColumn <font color="#ff0000">Header<font color="#000000">=&#8221;</font><font color="#0000ff">Image Information</font><font color="#000000">&#8220;</font> <br />
                        HeaderTemplate<font color="#000000">=&#8221;</font><font color="#b8860b">{StaticResource MultiColumnHeaderTemplate}</font><font color="#000000">&#8221;<br />
                        </font></font></font><font color="#0000ff"><font color="#800000"><font color="#ff0000">CellTemplate</font><font color="#000000">=&#8221;</font><font color="#b8860b">{StaticResource MultiColumnCellTemplate}</font><font color="#000000">&#8221; </font></font><font color="#0000ff">&gt;</font></font><font color="#0000ff"> <br />
            </font></font></font><font color="#0000ff">&lt;/</font><font color="#800000">GridView</font><font color="#0000ff">&gt;<br />
      </font><font color="#0000ff">&lt;<font color="#800000">ListView.View</font><font color="#0000ff">&gt;</font><br />
&lt;/</font><font color="#800000">ListView</font><font color="#0000ff">&gt;</font></p>
<p>And if you hate writing XAML, you notice with a grateful heart that I&#8217;ve posted every bit of XAML you need right here. Additionally (and this is a first for this site), because of the complexity of this post, I&#8217;m offering this project as a downloadable zip file so you can tweak it or futz with it at your convenience.</p>
<p><a href="http://www.designersilverlight.com/wp-content/uploads/2008/01/listviewembeddedcolumns.zip" title="Embedded Columns Project Files">Embedded Columns Project Files</a> (in zip format&#8230; requires .Net 3.5)</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http://www.designersilverlight.com/2008/01/25/embedded-listview-columns-columns-within-columns/&amp;layout=standard&amp;show_faces=1&amp;width=450&amp;action=like&amp;colorscheme=light&amp;font=" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:25px"></iframe><div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Embedded+ListView+Columns+%28Columns+Within+Columns%29+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D82" title="Post to Twitter"><img class="nothumb" src="http://www.designersilverlight.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter.png" alt="Post to Twitter" /></a> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Embedded+ListView+Columns+%28Columns+Within+Columns%29+http%3A%2F%2Fdesignersilverlight.com%2F%3Fp%3D82" title="Post to Twitter">Tweet This Post</a></p></div>]]></content:encoded>
			<wfw:commentRss>http://www.designersilverlight.com/2008/01/25/embedded-listview-columns-columns-within-columns/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

