<?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>JAPF &#187; patterns</title>
	<atom:link href="http://www.japf.fr/tag/patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.japf.fr</link>
	<description>Jeremy Alles Presentation Foundation: WPF, .Net and modern software development</description>
	<lastBuildDate>Thu, 29 Jul 2010 07:29:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How to close a View from a ViewModel ?</title>
		<link>http://www.japf.fr/2009/09/how-to-close-a-view-from-a-viewmodel/</link>
		<comments>http://www.japf.fr/2009/09/how-to-close-a-view-from-a-viewmodel/#comments</comments>
		<pubDate>Tue, 29 Sep 2009 11:14:55 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[model-view-viewmodel]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[patterns]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=252</guid>
		<description><![CDATA[Note: I wrote this article to clarify relationships between ViewModel and View classes. If you want a complete solution you should take a look at existing MVVM framework like Cinch (by Sacha Barber). Yesterday, there was an insteresting question about MVVM on StackOverflow: &#8220;How to close a View from a ViewModel ?&#8221; Like always with [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Note</strong>: I wrote this article to clarify relationships between ViewModel and View classes. If you want a complete solution you should take a look at existing MVVM framework like <a href="http://sachabarber.net/?page_id=523">Cinch</a> (by <a href="http://sachabarber.net/">Sacha Barber</a>).</p>
<p>Yesterday, there was an insteresting question about MVVM on <a href="http://stackoverflow.com/questions/1484233/wpf-mvvm-closing-a-view-from-viewmodel/1486481#1486481">StackOverflow</a>: &#8220;<em>How to close a View from a ViewModel</em> ?&#8221;</p>
<p>Like always with WPF, there are many approaches to solve this problem.<br />
<strong><br />
Solution 1: give a reference to the View in the ViewModel</strong></p>
<p>You need to control the View from the ViewModel ? Just gives a reference to the View in the ViewModel constructor.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p252code5'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2525"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p252code5"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> Window1<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DataContext</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Window1ViewModel<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Unfortunatelly, this approach has several drawbacks:</p>
<ul>
<li> it breaks the foundamental principle of the MVVM methodology: the ViewModel should be an abstraction of the View</li>
<li>it complicates the work needed to unit test your ViewModel</li>
<li>it introduces high coupling between the ViewModel and the View</li>
</ul>
<p><strong>Solution 2: the ViewModel raises an event when it wants to close its associated View</strong></p>
<p>If having a reference to the View in the ViewModel is not the right thing, why not using an event. We can add a RequestClose event in the ViewModel class and raise this event when the ViewModel wants to close its associated View.</p>
<p>The View, when it creates the ViewModel subscribe to the RequestClose event. In the event handler, the View is closed.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p252code6'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2526"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p252code6"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// class is omitted, only constructor is shown</span>
<span style="color: #0600FF; font-weight: bold;">public</span> Window1<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  var viewmodel <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Window1ViewModel<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  viewmodel<span style="color: #008000;">.</span><span style="color: #0000FF;">RequestClose</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">&#40;</span>s, e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DataContext</span> <span style="color: #008000;">=</span> viewmodel<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><strong>Solution 2, first refinement</strong></p>
<p>Of course I prefer the event based solution, but we can improve it. The first refinement we can do is to make sure the event will be coherent over all our classes. To achieve this I setup an interface:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p252code7'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2527"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p252code7"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">interface</span> IRequestCloseViewModel
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">event</span> EventHandler RequestClose
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>This interface is implemented by my ViewModel classes which wants to support the ability to close their associated Views.</p>
<p><strong>Solution 3, second refinement</strong></p>
<p>Another possible refinement is to automate the subscription of the RequestClose event in the View. To do that, I created an abstract ApplicationWindowBase class that inherits from Window. When the DataContext changes, I check if the IRequestCloseViewModel is supported by the DataContext:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p252code8'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2528"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p252code8"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ApplicationWindowBase <span style="color: #008000;">:</span> Window
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> ApplicationWindowBase<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DataContextChanged</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> DependencyPropertyChangedEventHandler<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnDataContextChanged</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnDataContextChanged<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, DependencyPropertyChangedEventArgs e<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">NewValue</span> <span style="color: #008000;">is</span> IRequestCloseViewModel<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #008080; font-style: italic;">// if the new datacontext supports the IRequestCloseViewModel we can use</span>
      <span style="color: #008080; font-style: italic;">// the event to be notified when the associated viewmodel wants to close</span>
      <span style="color: #008080; font-style: italic;">// the window</span>
      <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>IRequestCloseViewModel<span style="color: #008000;">&#41;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">NewValue</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">RequestClose</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">&#40;</span>s, e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Like I said in the intro, this a very basic implementation of this concept. Many other approach exists. A good source of information is in the source code of existing MVVM frameworks.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2009/09/how-to-close-a-view-from-a-viewmodel/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>French article about MVVM posted !</title>
		<link>http://www.japf.fr/2009/03/french-article-about-mvvm-posted/</link>
		<comments>http://www.japf.fr/2009/03/french-article-about-mvvm-posted/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 06:45:16 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[WPF]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[model-view-viewmodel]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[patterns]]></category>
		<category><![CDATA[practises]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=149</guid>
		<description><![CDATA[It&#8217;s finally online after a long work.I hope my french readers will enjoy it:  http://japf.developpez.com/tutoriels/dotnet/mvvm-pour-des-applications-wpf-bien-architecturees-et-testables/ Here is the abstract translated to english: &#8220;Inch by inch, the WPF technology is being adopted by .Net developers as a development platform for next generation user interfaces. This changeover is taking time and complicated because WPF changes principles that [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s finally online after a long work.I hope my french readers will enjoy it:  <a href="http://japf.developpez.com/tutoriels/dotnet/mvvm-pour-des-applications-wpf-bien-architecturees-et-testables/">http://japf.developpez.com/tutoriels/dotnet/mvvm-pour-des-applications-wpf-bien-architecturees-et-testables/</a></p>
<p>Here is the abstract translated to english:</p>
<p><em>&#8220;Inch by inch, the WPF technology is being adopted by .Net developers as a development platform for next generation user interfaces. This changeover is taking time and complicated because WPF changes principles that are well known until now in the process of designing a user interface. The Model-View-ViewModel methodology helps formalize WPF developement by giving guidance that leads to apps cleary architectured, testable, and by optimizing the workflow between developer and designer.&#8221;</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2009/03/french-article-about-mvvm-posted/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Very simple MVVM demo application</title>
		<link>http://www.japf.fr/2009/02/very-simple-mvvm-demo-application/</link>
		<comments>http://www.japf.fr/2009/02/very-simple-mvvm-demo-application/#comments</comments>
		<pubDate>Sun, 01 Feb 2009 12:09:06 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[WPF]]></category>
		<category><![CDATA[icollectionview]]></category>
		<category><![CDATA[icommand]]></category>
		<category><![CDATA[model-view-viewmodel]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[patterns]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=137</guid>
		<description><![CDATA[The last couple of days, I&#8217;ve been busy writing an article about MVVM in French. I don&#8217;t know yet if I&#8217;ll translate it to English, but I&#8217;m sure I can share the demo application, and this is the goal of this post ! I think the best way to understand how things fit together is [...]]]></description>
			<content:encoded><![CDATA[<p>The last couple of days, I&#8217;ve been busy writing an article about MVVM in French. I don&#8217;t know yet if I&#8217;ll translate it to English, but I&#8217;m sure I can share the demo application, and this is the goal of this post !</p>
<p>I think the best way to understand how things fit together is to explore the source code of the application.</p>
<p>It&#8217;s a very small demo because I wanted to demonstrate only a subset of MVVM facilities, so you&#8217;ll find:</p>
<ul>
<li>an easy way to work with commands in MVVM using Josh Smith RelayCommand class</li>
<li>the power of the ICollectionView interface to implement navigation and search feature</li>
<li>empty code-behind file for my views</li>
<li>a base class for all my ViewModel objects (again, based on the work of Josh)</li>
</ul>
<p style="text-align: center;"><img class="size-medium wp-image-139 aligncenter" title="mvvm-demo-1" src="http://www.japf.fr/wp-content/uploads/2009/02/mvvm-demo-1-300x116.png" alt="mvvm-demo-1" width="300" height="116" /></p>
<p style="text-align: center;"><img class="alignnone size-medium wp-image-140" title="mvvm-demo-2" src="http://www.japf.fr/wp-content/uploads/2009/02/mvvm-demo-2-300x116.png" alt="mvvm-demo-2" width="300" height="116" /></p>
<p style="text-align: left;">You can download the source code <a href="http://www.japf.fr/wp-content/uploads/2009/02/mvvmdemoapplication.zip">here</a>. Enjoy <img src='http://www.japf.fr/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2009/02/very-simple-mvvm-demo-application/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Why should I use Model-View-ViewModel pattern</title>
		<link>http://www.japf.fr/2008/12/why-should-i-use-model-view-viewmodel-pattern/</link>
		<comments>http://www.japf.fr/2008/12/why-should-i-use-model-view-viewmodel-pattern/#comments</comments>
		<pubDate>Mon, 22 Dec 2008 13:47:23 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[WPF]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[model-view-viewmodel]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[patterns]]></category>
		<category><![CDATA[testability]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=90</guid>
		<description><![CDATA[Recently, while discussing a WPF issue on a forum, I discovered that some people didn&#8217;t know anything about the Model-View-ViewModel pattern. I&#8217;m not an expert of MVVM as I discovered it a couple of months ago. I started to use it on my first real project at work, which involve a pretty big application using [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, while discussing a WPF issue on a forum, I discovered that some people didn&#8217;t know anything about the Model-View-ViewModel pattern. I&#8217;m not an expert of MVVM as I discovered it a couple of months ago. I started to use it on my first real project at work, which involve a pretty big application using advanced TreeView, Ribbon, on-demand graphic creation, a C++ kernel, datagrid, etc. The MVVM pattern helps me to keep my design as clean as possible. In this post, I&#8217;d like to give you some of the aspects of the MVVM pattern, and useful links across the web.</p>
<p>If you find yourself:</p>
<ul>
<li>having huge .xaml.cs code behind file</li>
<li>using event handler (such as Click&#8230;) everywhere</li>
<li>tweaking TreeViewItem (using ItemContainerGenerator for example) to manipulate a TreeView</li>
<li>having tons of IValueConverter</li>
<li>… then MVVM might come to the rescue :p</li>
</ul>
<p><strong>What MVVM can offer?</strong></p>
<ul>
<li>a clean and well defined separation between the Model and the View</li>
<li>the possibility to easily leverage the power of WPF: databinding, commands, validation, and more</li>
<li>a code that is testable</li>
<li>an organization that facilitate the workflow between designers and developers</li>
<li>minimalist code-behind file (which leads to testable code)</li>
<li>facilitate the deployment of an application in multiple environment (WPF / Silverlight)</li>
</ul>
<p><strong>How is that possible?</strong></p>
<p>Here is a part of the introduction of John Gossman:</p>
<p><em>“In simple examples, the View is data bound directly to the Model. For example, a boolean in the Model can be data bound to a CheckBox, or a string field to a TextBox. In practice however, only a small subset of application UI can be data bound directly to the Model, especially if the Model is a pre-existing class or data schema over which the application developer has no control.<br />
The Model is very likely to have a data types that cannot be mapped directly to controls.  Finally we need a place to put view state such as selection or modes.<br />
The ViewModel is responsible for these tasks.  In this latter role the ViewModel contains data-transformers that convert Model types into View types, and it contains Commands the View can use to interact with the Model.”</em></p>
<p><img class="alignnone size-full wp-image-92" title="viewmodel" src="http://www.japf.fr/wp-content/uploads/2008/12/viewmodel.png" alt="viewmodel" width="468" height="164" /></p>
<ul>
<li>The View has the associated ViewModel set as its DataContext so that the View can easily data bind to ViewModel properties</li>
<li>The ViewModel reflects its changes to the Model by calling appropriate methods on the Model</li>
<li>The ViewModel exposes ICommand that the View data bind to, in order to execute actions</li>
<li>The Model signals its changes to the ViewModel by raising events</li>
</ul>
<p><strong>What are good resources to start working with MVVM?</strong></p>
<ul>
<li>Karl Shifflett started a series about MVVM, you can find his work <a href="http://karlshifflett.wordpress.com/mvvm/">here</a>.</li>
<li>Karl and Josh Smith wrote a very nice application that uses MVVM pattern. Moreover the application contains also information about internalization support. Check out the CodeProject article <a href="http://karlshifflett.wordpress.com/mvvm/internationalized-wizard-in-wpf-using-m-v-vm/">here</a>.</li>
<li>Karl wrote <a href="http://karlshifflett.wordpress.com/mvvm/input-validation-ui-exceptions-model-validation-errors/">an article</a> about a very important concept: user input validation. This is the first article of his new MVVM series.</li>
<li>WPF architect, John Gossman wrote a series of blog post about MVVM on his blog. An introduction to MVVM is <a href="http://blogs.msdn.com/johngossman/archive/2005/10/08/478683.aspx">available</a> and a good point to start.</li>
<li>Josh Smith wrote an excellent tool, Crack.Net which is built using MVVM. Crack.Net is hosted on <a href="http://www.codeplex.com/cracknetproject">CodePlex</a>, and studying the source code is a very good manner to understand how stuff fits together in the MVVM world.</li>
<li>Josh Smith wrote <a href="http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx">an article</a> about applying MVVM concepts to the WPF treeview. If you must work with a complex treeview, then you MUST read this article.</li>
<li>Marlon Grech wrote <a href="http://marlongrech.wordpress.com/2008/11/22/icollectionview-explained/">an article</a> about ICollectionView. ICollectionView is a useful interface that can be used in ViewModel classes to keep track user selection</li>
</ul>
<p>If you never heard about MVVM, or never take the time to look at it, I suggest you to take a look at some of those links. Trust me, the investisment worth it !</p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2008/12/why-should-i-use-model-view-viewmodel-pattern/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Learn Model-View-ViewModel, LOB WPF/Silverlight application</title>
		<link>http://www.japf.fr/2008/12/learn-model-view-viewmodel-lob-wpfsilverlight-application/</link>
		<comments>http://www.japf.fr/2008/12/learn-model-view-viewmodel-lob-wpfsilverlight-application/#comments</comments>
		<pubDate>Thu, 11 Dec 2008 10:16:49 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[WPF]]></category>
		<category><![CDATA[model-view-viewmodel]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[patterns]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=86</guid>
		<description><![CDATA[I&#8217;ve been waiting for a long time for this, and Karl Shifflet finally did it ! Karl is a WPF guru, and he just started a new series of articles on Model-View-ViewModel. Here is his own introduction: &#8220;My goal is to provide an introduction, several simple examples and progress to a series of WPF LOB [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been waiting for a long time for this, and <a href="http://karlshifflett.wordpress.com/">Karl Shifflet</a> finally did it !</p>
<p>Karl is a WPF guru, and he just started <a href="http://karlshifflett.wordpress.com/mvvm/">a new series of articles</a> on Model-View-ViewModel. Here is his own introduction: <em>&#8220;My goal is to provide an introduction, several simple examples and progress to a series of WPF LOB scenarios where M-V-VM is used.  Scenarios like, dialog box, simple form, master &#8211; detail, complex master detail with several embedded ViewModels, etc.&#8221;</em></p>
<p>I&#8217;m sure this series will very soon become <strong>the</strong> reference to learn MVVM. <a href="http://karlshifflett.wordpress.com/mvvm/input-validation-ui-exceptions-model-validation-errors/">His first article</a> is about validating the user input in a concise and agile way. Right now, I can&#8217;t give more details because I just start to read the article, but I&#8217;ll try to give my feedback as soon as possible.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2008/12/learn-model-view-viewmodel-lob-wpfsilverlight-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Thinking in WPF: attached properties</title>
		<link>http://www.japf.fr/2008/08/thinking-in-wpf-attached-properties/</link>
		<comments>http://www.japf.fr/2008/08/thinking-in-wpf-attached-properties/#comments</comments>
		<pubDate>Wed, 13 Aug 2008 13:45:50 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[WPF]]></category>
		<category><![CDATA[attached properties]]></category>
		<category><![CDATA[drag and drop]]></category>
		<category><![CDATA[patterns]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=19</guid>
		<description><![CDATA[Today, I decided to start a series that I&#8217;m calling &#8220;Thinking in WPF&#8221;. As you already know, I do NOT consider myself as a WPF specialist, so those blog posts don&#8217;t aim at giving a perfect knowledge of the &#8220;best&#8221; (if there are any) ways of thinking in WPF. I&#8217;d just like to share what [...]]]></description>
			<content:encoded><![CDATA[<p>Today, I decided to start a series that I&#8217;m calling &#8220;Thinking in WPF&#8221;. As you already know, I do <strong>NOT </strong>consider myself as a WPF specialist, so those blog posts don&#8217;t aim at giving a perfect knowledge of the &#8220;best&#8221; (if there are any) ways of thinking in WPF. I&#8217;d just like to share what I&#8217;m experiencing <img src='http://www.japf.fr/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>In my previous post, I started to write about things that I think makes WPF different. In the current project I&#8217;m working on at work, I had a good example of a new way of thinking with WPF. Todays article deals with a new WPF concept called <em>Attached properties</em>.</p>
<p><span id="more-19"></span></p>
<p><strong>Example</strong></p>
<p>Imagine you need to display a list of person (that&#8217;s very original isn&#8217;t it ?). Each person has a name, a first name, and let&#8217;s say an address. Because you want to display a list of data, you&#8217;re probably going to use a ListView.You also would like to let the user sort this list of data in the order he wants, simply by dragging and dropping items in the control.</p>
<p>This is a feature that is possible to implement for a ListView (by handling common Drag&#8217;n'Drop events such as <em>DragEnter</em>, <em>DragOver</em>, <em>PreviewDrop</em>&#8230; Basically, you can think of two ways to implement this behavior:</p>
<ul>
<li>1. handle all Drag&#8217;n'Drop events in the code behind (C# file)</li>
<li>2. inherit ListView and add new features</li>
</ul>
<p>Well, in my opinion, none of those two options are very elegant:</p>
<ul>
<li>The first one pollutes the code behind file with &#8220;functional&#8221; code. I prefer trying to keep the code behind file as clean as possible. Moreover, this solution doesn&#8217;t allow ANY reuse.</li>
<li>The second one is maybe the first one we can think about, but&#8230; It forces to change all the ListView control to our NewListView control (which inherit ListView). I don&#8217;t like that solution that much.</li>
</ul>
<p><strong>What is an Attached property ?</strong></p>
<p>The good news is that WPF allows you to implement this by using an alternative, an <em>Attached property</em>. Let&#8217;s first have a look at the definition from MSDN website:</p>
<blockquote><p><em>&#8220;An attached property is a concept defined by Extensible Application Markup Language (XAML). An attached property is intended to be used as a type of global property that is settable on any object.&#8221;</em></p></blockquote>
<p>Hmmm&#8230; What does that mean ? Actually, if you already played with WPF, you already encountered a lot of attached properties. For example if you add an Image in a DockPanel, you might write something like</p>
<p>&lt;Image DockPanel.Dock=&#8221;Top&#8221; &#8230;/&gt;</p>
<p>Have you ever thought about the DockPanel.Dock part ? It is funny because Image objects don&#8217;t have a DockPanel property. Actually all UI element that you can dock (Image, TextBlock, anything) doesn&#8217;t have a DockPanel property, so how it is possible to write that ? It is possible because the DockPanel control defines an attached property.</p>
<p><strong>Attached properties as an extensibility mechanism: the Attached Behavior pattern</strong></p>
<p>As the name suggests, an attached property can be attached to arbitrary object. In the previous example (with DockPanel.Dock) we use an attached property to add an attribute to an object (we add the information &#8220;<em>I want to dock this image on the top</em>&#8220;). What is important, is that we can also use attached properties to provide extensibility without the need for traditional inheritance.</p>
<p>A very nice definition of the Attached Behavior can be found <a href="http://blogs.msdn.com/johngossman/archive/2008/05/07/the-attached-behavior-pattern.aspx">on the blog of John Gossman</a>:</p>
<blockquote><p>&#8220;The Attached Behavior pattern encapsulates &#8220;behavior&#8221; (usually user interactivity) into a class outside the visual heirarchy and allows it to be applied to a visual element by setting an attached property and hooking various events on the visual element.&#8221;</p></blockquote>
<p>How does that work ? Basically what we can do is the following:</p>
<ul>
<li>Declare a boolean attached property in a new class, for example DragDropHelper.cs</li>
<li>When the value of this attached property, it is possible to hook the interesting events (OnDrag, OnDrop&#8230;) and implement Drag&#8217;n'Drop operation in them</li>
</ul>
<p>I suggest you to have a look at <a href="http://www.beacosta.com/blog/?p=53">this excellent post</a> from Beatriz Costa which implement Drag&#8217;n'Drop using attached properties.</p>
<p>The benefits of this technique are the following:</p>
<ul>
<li>No need to change all your ListView control to a subclass that you would create by using standard inheritance principle</li>
<li>It becomes possible (as in <a href="http://www.beacosta.com/blog/?p=53">Beatriz Costa post</a>) to implement the Drag&#8217;n'Drop behaviour on the mode generic ItemsControl class</li>
</ul>
<p>There are plenty of things that can be achieved using attached properties. In this post, I talked about implementing Drag&#8217;n'Drop using an attached property, but a lot of things that could be done using inheritance can be done using attached properties.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2008/08/thinking-in-wpf-attached-properties/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Read on a forum: only problems with WPF&#8230;</title>
		<link>http://www.japf.fr/2008/07/am-i-missing-something-about-wpf/</link>
		<comments>http://www.japf.fr/2008/07/am-i-missing-something-about-wpf/#comments</comments>
		<pubDate>Fri, 04 Jul 2008 14:02:43 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[WPF]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[patterns]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=16</guid>
		<description><![CDATA[A couple of days ago, I found a post on a WPF forum with the title &#8220;Only problems with WPF&#8230;&#8221;, and it basically looks like the following: &#8220;When I installed VS.net express 2008, I was happy because I thought that with WPF I&#8217;ll be able to build nice UI. Unfortunately, I have the felling this [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of days ago, I found a post on a WPF forum with the title &#8220;Only problems with WPF&#8230;&#8221;, and it basically looks like the following:</p>
<p><em>&#8220;When I installed VS.net express 2008, I was happy because I thought that with WPF I&#8217;ll be able to build nice UI. Unfortunately, I have the felling this is more like a source of problems&#8230; I spent several hours to fill a ListView with some data and the result is dirtier than with WinForms. The designer in VS.net sucks and  many controls are missing. It was so simple before&#8230;&#8221;</em></p>
<p>I wanted to give my point of view about that kind of reasoning because a couple of months ago, I think I had more or less the same&#8230; So, what makes WPF so different ? Or why I cannot just use my WinForms background and start to build wonderfull apps ?</p>
<p>I think that WPF is more than just a new framework or new classes. It is more than that because when you work with WPF, you have to change the way you think and design your apps.</p>
<ul>
<li>You have to change the way your work with controls because you can apply new Style and Template to them so that you can completely change the way they look</li>
<li>You have to change the way you design your apps because of DataBinding and Commands</li>
<li>You have to&#8230; forget almost every things you already know, and be ready to learn again !</li>
</ul>
<p>If I can give an advice, I would suggest to read a good WPF book. A couple of weeks ago, I finished to read WPF unleashed, by Adam Nathan <sup class='footnote'><a href='#fn-16-1' id='fnref-16-1'>1</a></sup>, and I really think that having this kind of the book is the right way start working with WPF. A lot of cool website are also full of interesting resources, <a href="http://www.codeproject.com">CodeProject</a> is maybe the one that I like the most.</p>
<p>To conlude, I would like to give a quick example <sup class='footnote'><a href='#fn-16-2' id='fnref-16-2'>2</a></sup>.</p>
<p>Imagine you&#8217;d like to build an application to browse a catalog of products. For each product, you need to see the name of the product, its designer, a description and an image. You also would like to have navigation build around the use of 2 arrows to browse products:</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2008/07/demo1.jpg" rel="lightbox[16]"><img class="alignnone size-medium wp-image-17" title="demo1" src="http://www.japf.fr/wp-content/uploads/2008/07/demo1-300x196.jpg" alt="Building a WPF application" width="300" height="196" /></a></p>
<p>How could you achieve this ? You might think about several ways to do it. The point is that you can do this by only using a ListBox and changing its style ! Isn&#8217;t amazing ? Go ahead and check the full video at <a href="http://sessions.visitmix.com/">http://sessions.visitmix.com/</a>.
<div class='footnotes'>
<div class='footnotedivider'></div>
<ol>
<li id='fn-16-1'>available at <a href="http://www.amazon.com/Windows-Presentation-Foundation-Unleashed-WPF/dp/0672328917">amazon.com</a> <span class='footnotereverse'><a href='#fnref-16-1'>&#8617;</a></span></li>
<li id='fn-16-2'>I was inspired by CT06: Applications = Designers + Developpers at Mix08 available <a href="http://sessions.visitmix.com/">here</a> <span class='footnotereverse'><a href='#fnref-16-2'>&#8617;</a></span></li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2008/07/am-i-missing-something-about-wpf/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
