<?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; mvvm</title>
	<atom:link href="http://www.japf.fr/tag/mvvm/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>Attributes-based validation in a WPF MVVM application</title>
		<link>http://www.japf.fr/2010/07/attributes-based-validation-in-a-wpf-mvvm-application/</link>
		<comments>http://www.japf.fr/2010/07/attributes-based-validation-in-a-wpf-mvvm-application/#comments</comments>
		<pubDate>Thu, 29 Jul 2010 07:29:32 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[codeproject]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=630</guid>
		<description><![CDATA[Today, I&#8217;m proud to share with you my very first article available on CodeProject. This article presents a technique which can be used in order to add validation in a WPF MVVM application based on attribute. Basically, it means that you can write validation logic like that (notice the attribute associated to this property): ?View [...]]]></description>
			<content:encoded><![CDATA[<p>Today, I&#8217;m proud to share with you my very first article available on <a href="http://www.codeproject.com/KB/smart/WPF_Validation_Attribute.aspx">CodeProject</a>. This article presents a technique which can be used in order to add validation in a WPF MVVM application based on attribute. Basically, it means that you can write validation logic like that (notice the attribute associated to this property):</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('p630code2'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6302"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p630code2"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Required<span style="color: #008000;">&#40;</span>ErrorMessage <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Field 'FirstName' is required.&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> FirstName
<span style="color: #008000;">&#123;</span>
    get
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">firstName</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    set
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">firstName</span> <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnPropertyChanged</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;FirstName&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Of course the article comes with a nice demo application:</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2010/07/02.png" rel="lightbox[630]"><img class="alignnone size-full wp-image-631" title="02" src="http://www.japf.fr/wp-content/uploads/2010/07/02.png" alt="" width="411" height="160" /></a></p>
<p>You can read the full article here: <a href="http://www.codeproject.com/KB/smart/WPF_Validation_Attribute.aspx">Attributes-based validation in a WPF MVVM application</a></p>
<p><em><br />
</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2010/07/attributes-based-validation-in-a-wpf-mvvm-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MVVM Frameworks Explorer updated</title>
		<link>http://www.japf.fr/2010/07/mvvm-frameworks-explorer-updated/</link>
		<comments>http://www.japf.fr/2010/07/mvvm-frameworks-explorer-updated/#comments</comments>
		<pubDate>Mon, 19 Jul 2010 18:44:25 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[Windows Phone]]></category>
		<category><![CDATA[frameworks]]></category>
		<category><![CDATA[mvvm]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=625</guid>
		<description><![CDATA[Today I&#8217;m releasing a new version of my MVVM Frameworks Explorer application. You can find the updated version here: http://www.japf.fr/silverlight/mvvm/index.html Here is a list of the changes in this new version: application updated to Silverlight 4 support is now detailed for WPF, Silverlight and Windows Phone new frameworks added (MEFedMVVM&#8230;) framework&#8217;s logo added download count [...]]]></description>
			<content:encoded><![CDATA[<p>Today I&#8217;m releasing a new version of my MVVM Frameworks Explorer application. You can find the updated version here: <a href="http://www.japf.fr/silverlight/mvvm/index.html">http://www.japf.fr/silverlight/mvvm/index.html</a></p>
<p><a href="http://www.japf.fr/wp-content/uploads/2009/11/silverlight-mvvm-app.png" rel="lightbox[625]"><img class="alignnone size-full wp-image-370" title="silverlight-mvvm-app" src="http://www.japf.fr/wp-content/uploads/2009/11/silverlight-mvvm-app.png" alt="" width="341" height="177" /></a></p>
<p>Here is a list of the changes in this new version:</p>
<ul>
<li>application updated to Silverlight 4</li>
<li>support is now detailed for WPF, Silverlight and Windows Phone</li>
<li>new frameworks added (MEFedMVVM&#8230;)</li>
<li>framework&#8217;s logo added</li>
<li>download count added (based on the information I found on CodePlex website)</li>
<li>about window</li>
</ul>
<p>As always, feel free to give feedback <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/2010/07/mvvm-frameworks-explorer-updated/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Leveraging expression trees to unit test ViewModel classes</title>
		<link>http://www.japf.fr/2010/03/leveraging-expression-trees-to-unit-test-viewmodel-classes/</link>
		<comments>http://www.japf.fr/2010/03/leveraging-expression-trees-to-unit-test-viewmodel-classes/#comments</comments>
		<pubDate>Tue, 23 Mar 2010 13:36:53 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[model-view-viewmodel]]></category>
		<category><![CDATA[moq]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=483</guid>
		<description><![CDATA[Introduction: In this article, I&#8217;m describing a technique which leverage the expression trees of C# 3.0 in order to facilitate the unit testing of ViewModel&#8217;s properties. My final goal is to be able to unit test a ViewModel property in 1 line. Without any doubt MVVM is now the most used framework to leverage WPF [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Introduction:</strong> In this article, I&#8217;m describing a technique which leverage the expression trees of C# 3.0 in order to facilitate the unit testing of ViewModel&#8217;s properties. My final goal is to be able to unit test a ViewModel property in 1 line.</p>
<p>Without any doubt MVVM is now the most used framework to leverage WPF and Silverlight functionalities in the best way ! During the last <a href="http://live.visitmix.com">Mix</a>, 3 sessions were dedicated to this methodology (you can watch the videos online <a href="http://live.visitmix.com/Videos">here</a>).</p>
<p>As you already know one of the key advantage of the MVVM methodology is to <strong>improve the testability</strong> of the overall application by reducing the amount of code in the code-behind and producing ViewModel classes which are testable. We use to say that ViewModel classes are testable because:</p>
<ul>
<li>they are not coupled to UI concepts (controls, focus, keyboard input&#8230;)</li>
<li>they can wrap model objects using interfaces (for instance a PersonViewModel wraps a IPerson object)</li>
<li>they are not subclassing a UI control (such as Button or Window)</li>
</ul>
<p>Today I&#8217;d like to share a technique I&#8217;m using to facilitate the unit tests of some properties of my ViewModel classes.</p>
<p>Let&#8217;s use a very simple ViewModel class as example:</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('p483code10'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p48310"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code" id="p483code10"><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> PersonViewModel <span style="color: #008000;">:</span> ViewModelBase
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> IPerson person<span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">bool</span> isSelected<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Name 
  <span style="color: #008000;">&#123;</span>
     get
     <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">person</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">;</span>
     <span style="color: #008000;">&#125;</span>
     set
     <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">person</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span> <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnPropertyChanged</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Name&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
     <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> IsSelected
  <span style="color: #008000;">&#123;</span>
    get
    <span style="color: #008000;">&#123;</span>
      <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">isSelected</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    set
    <span style="color: #008000;">&#123;</span>
      <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">isSelected</span> <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
      <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnPropertyChanged</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;IsSelected&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">// rest of the code omitted for simplicity</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The Name property, as usually with the MVVM pattern gets its value from the wrapped model object. The easiest way to unit test this property is to use a mocking library. Here is a example using <a href="http://code.google.com/p/moq/">MOQ</a> (my favourite mocking library):</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('p483code11'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p48311"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p483code11"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> TestName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  var mockPerson <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Mock<span style="color: #008000;">&lt;</span>IPerson<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  var vm <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PersonViewModel<span style="color: #008000;">&#40;</span>mockPerson<span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  vm<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Jeremy&quot;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">// verify that the Name property of the IPerson interface has been set</span>
  mockPerson<span style="color: #008000;">.</span><span style="color: #0000FF;">VerifySet</span><span style="color: #008000;">&#40;</span>p <span style="color: #008000;">=&gt;</span> p<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Jeremy&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The Selected property is different because it doesn&#8217;t wrap a model property. It&#8217;s an information that is added to the ViewModel layer in order to control a UI-related property (for example the IsSelected property of a ListBoxItem). This technique is heavily used to have ViewModel classes interact with the WPF or Silverlight TreeView or ListBox control (you can check out this <a href="http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx">excellent article</a> of Josh Smith for more detail).</p>
<p>In order to unit test this property, we must:<br />
1/ ensure the PropertyChanged event of the INotifyPropertyChanged is  raised properly<br />
2/ ensure we can write a value and read back the  correct value</p>
<p>Here is a sample code which does this unit test:</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('p483code12'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p48312"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p483code12"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> TestName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
var vm <span style="color: #008000;">=</span> <span style="color: #008000;">new</span>  PersonViewModel<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #6666cc; font-weight: bold;">bool</span> propertyChanged <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
&nbsp;
vm<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyChanged</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> propertyChanged <span style="color: #008000;">=</span> e<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyName</span> <span style="color: #008000;">==</span>  <span style="color: #666666;">&quot;Name&quot;</span><span style="color: #008000;">;</span>
vm<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;newName&quot;</span><span style="color: #008000;">;</span>
&nbsp;
Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">IsTrue</span><span style="color: #008000;">&#40;</span>propertyChanged<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;newName&quot;</span>, vm<span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>It quickly become cumbersome to copy/paste this unit test for all the ViewModel properties we have. That&#8217;s the reason I started thinking about another way to do it&#8230;</p>
<p>Here is the feature I&#8217;m proposing:</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('p483code13'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p48313"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p483code13"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> TestName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
var vm <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PersonViewModel<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
TestHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">TestProperty</span><span style="color: #008000;">&#40;</span>vm, v <span style="color: #008000;">=&gt;</span> v<span style="color: #008000;">.</span><span style="color: #0000FF;">IsSelected</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>In this sample, I&#8217;m telling I want to test the IsSelected property of the PersonViewModel type. The advantages are:<br />
1/ less code involved : 1 line to test 1 property<br />
2/ intellisense support in order to prevent typing error and no more &#8220;magic&#8221; string to give the name of the property<br />
3/ refactoring the name of the property will refactor this sample code too<br />
4/ automatic generation of default test values behind the scene</p>
<p><strong>How does it works ?</strong></p>
<ul>
<li>TestProperty treats the second parameter as an Expression&lt;Func&gt; and not as a Func directly</li>
<li>Using expression tree (the &#8220;v =&gt; v.IsSelected&#8221; part),  I&#8217;m able to retrieve the name of the property and its type</li>
<li>Using reflection, I&#8217;m able to get and set the value</li>
<li>Depending on the type of the property (string, bool, int, double), I have default values write and read back (with a test to ensure that the PropertyChanged event has been raised properly).</li>
</ul>
<p>Here is the code of the TestPropertyMethod:</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('p483code14'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p48314"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code" id="p483code14"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> TestProperty<span style="color: #008000;">&lt;</span>T, U<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>T viewmodel, Expression<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span>T, U<span style="color: #008000;">&gt;&gt;</span> expression<span style="color: #008000;">&#41;</span>
    <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> INotifyPropertyChanged
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>expression<span style="color: #008000;">.</span><span style="color: #0000FF;">Body</span> <span style="color: #008000;">is</span> MemberExpression<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        MemberExpression memberExpression <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>MemberExpression<span style="color: #008000;">&#41;</span> expression<span style="color: #008000;">.</span><span style="color: #0000FF;">Body</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>expression<span style="color: #008000;">.</span><span style="color: #0000FF;">Body</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Type</span> <span style="color: #008000;">==</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            TestViewModelProperty<span style="color: #008000;">&#40;</span>viewmodel, memberExpression<span style="color: #008000;">.</span><span style="color: #0000FF;">Member</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span>, <span style="color: #0600FF; font-weight: bold;">true</span>, <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>expression<span style="color: #008000;">.</span><span style="color: #0000FF;">Body</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Type</span> <span style="color: #008000;">==</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            TestViewModelProperty<span style="color: #008000;">&#40;</span>viewmodel, memberExpression<span style="color: #008000;">.</span><span style="color: #0000FF;">Member</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span>, <span style="color: #666666;">&quot;value1&quot;</span>, <span style="color: #666666;">&quot;value2&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span> 
        <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>expression<span style="color: #008000;">.</span><span style="color: #0000FF;">Body</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Type</span> <span style="color: #008000;">==</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            TestViewModelProperty<span style="color: #008000;">&#40;</span>viewmodel, memberExpression<span style="color: #008000;">.</span><span style="color: #0000FF;">Member</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span>, <span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">99</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>expression<span style="color: #008000;">.</span><span style="color: #0000FF;">Body</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Type</span> <span style="color: #008000;">==</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            TestViewModelProperty<span style="color: #008000;">&#40;</span>viewmodel, memberExpression<span style="color: #008000;">.</span><span style="color: #0000FF;">Member</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span>, <span style="color: #FF0000;">1.0</span>, <span style="color: #FF0000;">99.0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> NotSupportedException<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Type is not supported&quot;</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>And the TestViewModelProperty:</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('p483code15'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p48315"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p483code15"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> TestViewModelProperty<span style="color: #008000;">&lt;</span>T, U<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>T viewModel, <span style="color: #6666cc; font-weight: bold;">string</span> propertyName, U value1, U value2<span style="color: #008000;">&#41;</span>
    <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> INotifyPropertyChanged
<span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">bool</span> propertyChanged<span style="color: #008000;">;</span>
    viewModel<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyChanged</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> propertyChanged <span style="color: #008000;">=</span> e<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyName</span> <span style="color: #008000;">==</span> propertyName<span style="color: #008000;">;</span>
&nbsp;
    propertyChanged <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
    viewModel<span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span>propertyName, value1<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">IsTrue</span><span style="color: #008000;">&#40;</span>propertyChanged<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">IsTrue</span><span style="color: #008000;">&#40;</span>viewModel<span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&lt;</span>U<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>propertyName<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>value1<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    propertyChanged <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
    viewModel<span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span>propertyName, value2<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">IsTrue</span><span style="color: #008000;">&#40;</span>propertyChanged<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">IsTrue</span><span style="color: #008000;">&#40;</span>viewModel<span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&lt;</span>U<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>propertyName<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>value2<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>I&#8217;m using 2 extensions methods in order to get and set value from the ViewModel object using reflection. Here they are:</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('p483code16'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p48316"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p483code16"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> T GetValue<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> <span style="color: #6666cc; font-weight: bold;">object</span> obj, <span style="color: #6666cc; font-weight: bold;">string</span> propertyName<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    var propertyInfo <span style="color: #008000;">=</span> obj<span style="color: #008000;">.</span><span style="color: #0000FF;">GetType</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetProperty</span><span style="color: #008000;">&#40;</span>propertyName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span>propertyInfo<span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&#40;</span>obj, <span style="color: #0600FF; font-weight: bold;">null</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: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetValue<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> <span style="color: #6666cc; font-weight: bold;">object</span> obj, <span style="color: #6666cc; font-weight: bold;">string</span> propertyName, T value<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    var propertyInfo <span style="color: #008000;">=</span> obj<span style="color: #008000;">.</span><span style="color: #0000FF;">GetType</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetProperty</span><span style="color: #008000;">&#40;</span>propertyName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    propertyInfo<span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span>obj, value, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Please feel free to <a href="http://www.japf.fr/wp-content/uploads/2010/03/TestViewModelHelper.txt">download the source code</a> of the ViewModelTestHelper class.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2010/03/leveraging-expression-trees-to-unit-test-viewmodel-classes/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Minor update to the Silverlight MVVM frameworks explorer</title>
		<link>http://www.japf.fr/2010/01/minor-update-to-the-silverlight-mvvm-frameworks-explorer/</link>
		<comments>http://www.japf.fr/2010/01/minor-update-to-the-silverlight-mvvm-frameworks-explorer/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 12:48:19 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[model-view-viewmodel]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[toolkit]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=428</guid>
		<description><![CDATA[Thanks to the readers who gave me feedback on my Silverlight MVVM frameworks explorer I updated the application this morning in order to fix some problems. Here is the change set: fix incorrect URLs fix incorrect &#8220;Silverlight Support&#8221; options. As Laurent Bugnion said in the comments, his MVVM Light framework was the only one supporting [...]]]></description>
			<content:encoded><![CDATA[<p>Thanks to the readers who gave me feedback on my <a href="http://www.japf.fr/silverlight/mvvm/index.html">Silverlight MVVM frameworks explorer</a> I updated the application this morning in order to fix some problems.</p>
<p>Here is the change set:</p>
<ul>
<li>fix incorrect URLs</li>
<li>fix incorrect &#8220;Silverlight Support&#8221; options. As Laurent Bugnion said in the comments, his MVVM Light framework was the only one supporting Silverlight which was strange&#8230;</li>
<li>links now open in a new window</li>
</ul>
<p>Click on the following image to launch the Silverlight application.</p>
<p><a href="http://www.japf.fr/silverlight/mvvm/index.html"><img class="alignnone size-medium wp-image-370" title="silverlight-mvvm-app" src="http://www.japf.fr/wp-content/uploads/2009/11/silverlight-mvvm-app-300x156.png" alt="" width="314" height="163" /></a></p>
<p>About adding new frameworks, I&#8217;m not sure to add those which targets a much larger domain than MVVM itself. CompositeApplication guidance for example is a lot more than MVVM&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2010/01/minor-update-to-the-silverlight-mvvm-frameworks-explorer/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Review of 2009 blog posts</title>
		<link>http://www.japf.fr/2010/01/review-of-2009-blog-posts/</link>
		<comments>http://www.japf.fr/2010/01/review-of-2009-blog-posts/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 19:01:42 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[model-view-viewmodel]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[resharper]]></category>
		<category><![CDATA[visual studio]]></category>
		<category><![CDATA[vs2010]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=391</guid>
		<description><![CDATA[In the past year, I&#8217;ve posted more than 30 articles on my blog. Here is a summary of those posts (link in bold are those which got the most traffic during the year). Obviously, MVVM was a very hot topic during 2009 January XAML guidelines: interviews of WPF masters photoSuru: a WPF application with spectacular [...]]]></description>
			<content:encoded><![CDATA[<p>In the past year, I&#8217;ve posted more than 30 articles on my blog. Here is a summary of those posts (link in bold are those which got the most traffic during the year). Obviously, MVVM was a very hot topic during 2009 <img src='http://www.japf.fr/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>January</p>
<ul>
<li><a href="http://www.japf.fr/2009/01/xaml-guidelines-interviews-from-wpf-masters/">XAML guidelines: interviews of WPF masters</a></li>
<li><a href="http://www.japf.fr/2009/01/photosuru-a-wpf-application-with-spectacular-user-experience/">photoSuru: a WPF application with spectacular user experience</a></li>
<li><a href="http://www.japf.fr/2009/01/model-view-viewmodel-commands-and-inputbindings/">Model-View-ViewModel, Commands and InputBindings</a></li>
<li><a href="http://www.japf.fr/2009/01/filtering-with-or-without-icollectionview/">Filtering with or without ICollectionView ?</a></li>
<li><a href="http://www.japf.fr/2009/01/why-i-love-linq/">Why do I love Extension Methods in System.Linq ?</a></li>
<li><a href="http://www.japf.fr/2009/01/using-extension-methods-to-raise-an-event/">Using extension methods to raise an event</a></li>
</ul>
<p>February</p>
<ul>
<li><strong><a href="http://www.japf.fr/2009/02/very-simple-mvvm-demo-application/">Very simple MVVM demo application</a></strong></li>
<li><a href="http://www.japf.fr/2009/02/vs2010-ui/">VS2010 UI</a></li>
</ul>
<p>March</p>
<ul>
<li><a href="http://www.japf.fr/2009/03/french-article-about-mvvm-posted/">French article about MVVM posted !</a></li>
<li><strong><a href="http://www.japf.fr/2009/03/thinking-with-mvvm-data-templates-contentcontrol/">Thinking with MVVM: Data Templates + ContentControl</a></strong></li>
<li><a href="http://www.japf.fr/2009/03/mix09-starts-today/">MIX09 starts today !</a></li>
<li><a href="http://www.japf.fr/2009/03/first-look-at-expression-blend-3/">First look at Expression Blend 3 !</a></li>
<li><a href="http://www.japf.fr/2009/03/the-future-of-wpf-at-mix09/">The future of WPF at Mix09</a></li>
</ul>
<p>April</p>
<ul>
<li><a href="http://www.japf.fr/2009/04/adding-transitions-to-a-mvvm-based-dialog/">Adding transitions to a MVVM based dialog</a></li>
</ul>
<p>May</p>
<ul>
<li><a href="http://www.japf.fr/2009/05/browse-your-codebase-like-a-pro-with-resharper/">Browse your codebase like a pro with ReSharper</a></li>
<li><a href="http://www.japf.fr/2009/05/visual-studio-2010-beta1-available-with-net4-and-wpf4-included/">Visual Studio 2010: Beta1 available with .Net4 and WPF4 included</a></li>
<li><a href="http://www.japf.fr/2009/05/net4-wpf4-and-vs2010-interesting-links/">.Net4, WPF4 and VS2010 interesting links</a></li>
</ul>
<p>July</p>
<ul>
<li><a href="http://www.japf.fr/2009/07/beziersegment-demo-application/">BezierSegment demo application</a></li>
</ul>
<p>August</p>
<ul>
<li><a href="http://www.japf.fr/2009/08/learn-how-net-manages-memory-in-15min/">Learn how .Net manages memory in 15min</a></li>
<li><a href="http://www.japf.fr/2009/08/wpf-memory-leak-with-eventmanager-registerclasshandler/">WPF possible memory leak with EventManager.RegisterClassHandler</a></li>
</ul>
<p>September</p>
<ul>
<li><a href="http://www.japf.fr/2009/09/how-to-close-a-view-from-a-viewmodel/">How to close a View from a ViewModel ?</a></li>
</ul>
<p>October</p>
<ul>
<li><a href="http://www.japf.fr/2009/10/a-quick-tour-of-existing-mvvm-frameworks/">A quick tour of existing MVVM frameworks</a></li>
<li><a href="http://www.japf.fr/2009/10/the-cost-of-building-visual-trees/">The cost of building Visual Trees</a></li>
<li><a href="http://www.japf.fr/2009/10/announcing-the-wpf-internals-series/">Announcing the “WPF internals” series</a></li>
<li><a href="http://www.japf.fr/2009/10/analyzing-events-usage-using-a-resharper-plugin/">Analyzing events usage using a R# plugin</a></li>
<li><strong><a href="http://www.japf.fr/2009/10/wpf-internals-part-1-what-are-the-core-wpf-classes/">WPF internals part 1 : what are the core WPF classes ?</a></strong></li>
<li><a href="http://www.japf.fr/2009/10/vs2010-beta-2-coming-today/">VS2010 Beta 2 coming today !</a></li>
<li><a href="http://www.japf.fr/2009/10/measure-rendering-time-in-a-wpf-application/">How to measure rendering time in a WPF application ?</a></li>
</ul>
<p>November</p>
<ul>
<li><a href="http://www.japf.fr/2009/11/using-snoop-with-vs2010/">Using Snoop to take a look at VS2010 !</a></li>
<li><a href="http://www.japf.fr/2009/11/pdc09-some-announcements/">PDC09: some announcements</a></li>
<li><a href="http://www.japf.fr/2009/11/pdc09-some-wpf-sessions-are-available/">PDC09: some WPF sessions are available !</a></li>
<li><a href="http://www.japf.fr/2009/11/pdc09-wpf-application-performance-tuning/">PDC09: Advanced WPF Application Performance Tuning</a></li>
<li><a href="http://www.japf.fr/2009/11/pdc09-how-vs2010-was-built-with-wpf/">PDC09 : How VS2010 was built with WPF ?</a></li>
<li><strong><a href="http://www.japf.fr/2009/11/discover-compare-existing-mvvm-frameworks/">Discover and compare existing MVVM frameworks !</a></strong></li>
</ul>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;"><strong><a class="row-title" title="Edit “Discover and compare existing MVVM frameworks !”" href="post.php?action=edit&amp;post=369">Discover and compare existing MVVM frameworks !</a></strong></div>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2010/01/review-of-2009-blog-posts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Discover and compare existing MVVM frameworks !</title>
		<link>http://www.japf.fr/2009/11/discover-compare-existing-mvvm-frameworks/</link>
		<comments>http://www.japf.fr/2009/11/discover-compare-existing-mvvm-frameworks/#comments</comments>
		<pubDate>Thu, 26 Nov 2009 21:31:50 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[frameworks]]></category>
		<category><![CDATA[model-view-viewmodel]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[silverlifgt]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=369</guid>
		<description><![CDATA[A couple of weeks ago, I wrote a blog post where I compared the existing MVVM frameworks. This post became a bit famous in the WPF/Silverlight blog world and I received a lot of feedback to update the list, fix information, etc. I also got a request from Erik suggesting me to put all the [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of weeks ago, I wrote <a href="http://www.japf.fr/2009/10/a-quick-tour-of-existing-mvvm-frameworks/">a blog post</a> where I compared the existing MVVM frameworks. This post became a bit famous in the WPF/Silverlight blog world and I received a lot of feedback to update the list, fix information, etc. I also got a request from Erik suggesting me to put all the datas in a matrix.</p>
<p>Today I&#8217;m proud to announce the <a href="http://www.japf.fr/silverlight/mvvm/index.html">MVVM frameworks Silverlight application</a> (click the image to open the Silverlight3 page).</p>
<p><a href="http://www.japf.fr/silverlight/mvvm/index.html"><img class="alignnone size-full wp-image-370" title="silverlight-mvvm-app" src="http://www.japf.fr/wp-content/uploads/2009/11/silverlight-mvvm-app.png" alt="silverlight-mvvm-app" width="562" height="293" /></a></p>
<p>A couple of observation:</p>
<ul>
<li>please contact me via <a href="http://japf.fr/contact">this blog</a> or <a href="http://www.twitter.com/jalpf">twitter</a> if you find incorrect information</li>
<li>I&#8217;m not judging anybody&#8217;s work by giving rating, it&#8217;s just my personal feeling to have an easiest way to sort the data</li>
</ul>
<p>Hope you&#8217;ll like it <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/11/discover-compare-existing-mvvm-frameworks/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>A quick tour of existing MVVM frameworks</title>
		<link>http://www.japf.fr/2009/10/a-quick-tour-of-existing-mvvm-frameworks/</link>
		<comments>http://www.japf.fr/2009/10/a-quick-tour-of-existing-mvvm-frameworks/#comments</comments>
		<pubDate>Fri, 02 Oct 2009 09:00:27 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[command]]></category>
		<category><![CDATA[frameworks]]></category>
		<category><![CDATA[icommand]]></category>
		<category><![CDATA[model-view-viewmodel]]></category>
		<category><![CDATA[mvvm]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=259</guid>
		<description><![CDATA[[Article updated november, 26th: see my latest blog post for a better experience browsing the frameworks] One year ago MVVM wasn’t very famous. I remember the first article I read about it, about using MVVM to simplify the management of treeview controls. In the last six months, MVVM has been quickly promoted to THE methodology [...]]]></description>
			<content:encoded><![CDATA[<p><strong>[Article updated november, 26th: see <a href="http://www.japf.fr/2009/11/discover-compare-existing-mvvm-frameworks/">my latest blog post</a> for a better experience browsing the frameworks]</strong></p>
<p><span id="more-259"></span></p>
<p>One year ago MVVM wasn’t very famous. I remember the first article I read about it, about <a href="http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx">using MVVM to simplify the management of treeview controls</a>. In the last six months, MVVM has been quickly promoted to THE methodology to use when developing WPF applications. During this amount of time, famous WPF developers started to merge their existing MVVM classes into libraries. Those libraries are now known as MVVM frameworks and contain classes designed to help developers to use MVVM in their projects.</p>
<p>In this article, I’m going to make a quick tour of the available MVVM frameworks.</p>
<p><strong>If your own framework is not in this list or if I made a mistake while describing your work, please let me know using the contact form (available at the top of this page).</strong></p>
<p>Common features:</p>
<ul>
<li>ViewModelBase class (for the implementation of the INotifyPropertyChanged interface)</li>
<li>RelayCommand like class to link UI command to ViewModel’s handlers</li>
<li>Unit tests comes with the framework</li>
</ul>
<p><a href="http://sachabarber.net/?page_id=523">Cinch</a></p>
<ul>
<li>Author: <a href="http://sachabarber.net">Sacha Barber</a></li>
<li>Silverlight support: no</li>
<li>Documentation: excellent, 6 articles published on CodeProject</li>
<li>Hosting : <a href="http://cinch.codeplex.com/">CodePlex</a></li>
<li>License:  Code Project Open License</li>
<li>Features:
<ul>
<li>attached behaviors</li>
<li>validation suing IDataErrorInfo</li>
<li>support for IEditableObject</li>
<li>weak events creation and subscription</li>
<li>mediator messaging using weak events</li>
<li>IOC/DI support (using Unity)</li>
<li>services: event logger, message box, open save dialog, popup</li>
<li>threading helpers</li>
<li>support for menu items</li>
<li>closeable viewmodels</li>
<li>MVVM code generator</li>
</ul>
</li>
</ul>
<p><a href="http://www.galasoft.ch/mvvm/getstarted/">MVVM Light Toolkit </a></p>
<ul>
<li>Author: <a href="http://www.galasoft.ch/index.html">Laurent Bugnion</a></li>
<li>Silverlight support: yes</li>
<li>Documentation: many articles available on Laurent’s blog + other developers as well</li>
<li>Hosting: <a href="http://mvvmlight.codeplex.com/">CodePlex</a></li>
<li>License: MIT license</li>
<li>Features:
<ul>
<li>MSI installer</li>
<li>VS project and item template</li>
<li>VS code snippets</li>
<li>Messenger system for inter-viewmodel communication</li>
</ul>
</li>
</ul>
<p><a href="http://www.julmar.com/blog/mark/2009/08/04/MVVMHelpersV103.aspx">MVVM Helpers</a></p>
<ul>
<li>Author: <a href="http://www.julmar.com/blog/mark/">Mark Smith</a></li>
<li>Silverlight support: no</li>
<li>Documentation: some articles on Mark’s blog</li>
<li>Hosting: personal website</li>
<li>License: not defined</li>
<li>Features:
<ul>
<li>attached behaviors</li>
<li>viewmodel creation using markup extension</li>
<li>attributes based validation</li>
<li>IOC/DI using ServiceProvider approach</li>
<li>closeable viewmodel</li>
<li>wait cursor (using new WaitCursor() { // your code here })</li>
</ul>
</li>
</ul>
<p><a href="http://mvvmfoundation.codeplex.com/">MVVM Foundation</a></p>
<ul>
<li>Author: <a href="http://joshsmithonwpf.wordpress.com/">Josh Smith</a></li>
<li>Silverlight support: no</li>
<li>Documentation: articles about the Messenger implementation on Josh or Marlon Grech’s blog</li>
<li>Hosting: <a href="http://mvvmfoundation.codeplex.com/">CodePlex</a></li>
<li>License: MS-PL</li>
<li>Features:
<ul>
<li>Messenger system for inter-viewmodel communication</li>
<li>PropertyChanged event monitor</li>
</ul>
</li>
</ul>
<p><a href="http://www.caliburnproject.org/">Caliburn</a></p>
<ul>
<li>Author: <a href="http://robeisenberg.com/Work.aspx">Rob Eisenberg</a></li>
<li>Silverlight support: yes</li>
<li>Documentation: complete <a href="http://caliburn.codeplex.com/Wiki/View.aspx?title=Table%20Of%20Contents">online documentation</a> available</li>
<li>Hosting: CodePlex http://www.codeplex.com/caliburn</li>
<li>License: MIT license</li>
<li>Features:
<ul>
<li> commands are built on top of Actions and thus share many of the same features, including multiple input parameters, filters and automatic asynchronous execution</li>
<li>presenters that handles UI lifecycle issue such as handling activation, deactivation and shutdown semantics for various UI components</li>
<li>Caliburn applications are fully testable</li>
<li>various utilities such as a background task manager</li>
<li>supports various UI pattern (not MVVM only)</li>
<li>dependency injection container</li>
</ul>
</li>
</ul>
<p><a href="http://wpfonyx.codeplex.com/">Onyx</a></p>
<ul>
<li>Author: William e Kempf</li>
<li>Silverlight support: no</li>
<li>Documentation: basic <a href="http://wpfonyx.codeplex.com/Wiki/View.aspx?title=Introduction">introduction</a> available on CodePlex</li>
<li>Hosting: <a href="http://wpfonyx.codeplex.com/">CodePlex</a></li>
<li>License: not specified</li>
<li>Features:
<ul>
<li> ServiceLocator pattern</li>
<li>ViewModel creation using a custom markup extension</li>
<li>UI related services such as IDisplayMessage</li>
</ul>
</li>
</ul>
<p><a href="http://calcium.codeplex.com/">Calcium</a></p>
<ul>
<li>Author: <a href="http://danielvaughan.orpius.com/">Daniel Vaughan</a></li>
<li>Silverlight support: no</li>
<li>Documentation: 2 very detailed articles on CodeProject (<a href="http://www.codeproject.com/KB/WPF/CalciumPart01.aspx">part1</a> and <a href="http://www.codeproject.com/KB/WPF/CalciumPart02.aspx">part2</a>)</li>
<li>Hosting: <a href="http://calcium.codeplex.com/">CodePlex</a></li>
<li>License: use, copy, modify, and/or distribute and keep the copyright!</li>
<li>Features:
<ul>
<li> Module Manager for enabling or disabling of modules at runtime</li>
<li>messaging services for interacting with the user from the client or server using the same API</li>
<li>Command Service to associate WPF ICommands with content interfaces that only become active when an active view or viewmodel implements the interface</li>
<li>Region Adapters for ToolBars and Menus</li>
<li>Client-server logging ready to work out-of-the-box</li>
<li>Includes modules, such as a Web Browser, Text Editor, Output Window, and many more</li>
<li>Tabbed interface with dirty file indication (reusable across modules)</li>
</ul>
</li>
</ul>
<p><a href="http://www.orktane.com/Blog/">nRoute</a></p>
<ul>
<li>Author: <a href="http://www.orktane.com/Blog/">Rishi</a></li>
<li>Silverlight support: yes</li>
<li>Documentation: many articles available on author&#8217;s blog (see CodePlex project home page for the links)</li>
<li>Hosting: <a href="http://nroute.codeplex.com/">CodePlex</a></li>
<li>License: MS-PL</li>
<li>Features:
<ul>
<li> support Blend3 behaviors and triggers models</li>
<li>resource locator framework</li>
<li>view services: OpenFileDialog, ShowMessage&#8230;</li>
<li>uses attributes to map View and ViewModel together</li>
</ul>
</li>
</ul>
<p><a href="http://nitomvvm.codeplex.com/">Nito MVVM</a></p>
<ul>
<li>Author: Shammah</li>
<li>Silverlight support: no</li>
<li>Documentation: no</li>
<li>Hosting: <a href="http://nitomvvm.codeplex.com/">CodePlex</a></li>
<li>License: not specified</li>
<li>Features:
<ul>
<li> various MVVM friendly implementations of the ICommand interface</li>
</ul>
</li>
</ul>
<p><a href="http://karlshifflett.wordpress.com/">Ocean</a></p>
<ul>
<li>Author: <a href="http://karlshifflett.wordpress.com/">Karl Shifflet</a></li>
<li> Silverlight support: no</li>
<li>Documentation: articles available on Karl&#8217;s blog</li>
<li>Hosting: personal website</li>
<li>License: not specified</li>
<li>Features:
<ul>
<li> written in VB.Net</li>
<li>attribute based validation</li>
<li>viewmodel base classes: relaycommand, closeableviewmodel&#8230;</li>
<li>SQL server data access layer</li>
</ul>
</li>
</ul>
<p><a href="http://coremvvm.codeplex.com/">Basic MVVM framework</a></p>
<ul>
<li>Author: <a href="http://blogs.msdn.com/llobo/default.aspx">Lester Lobo</a></li>
<li> Silverlight support: no</li>
<li>Documentation: sample application available with the library</li>
<li>Hosting: <a href="http://coremvvm.codeplex.com/">CodePlex</a></li>
<li>License: MS-PL</li>
<li>Features:
<ul>
<li> delegating Commands\Keybinding</li>
<li>messaging between VM&#8217;s</li>
<li>handling events as commands with attached behaviors</li>
<li>handling dialogs (and more) as services</li>
<li>VS code snippets</li>
</ul>
</li>
</ul>
<p><a href="http://goldlight.codeplex.com/">GoodLight</a></p>
<ul>
<li>Author: <a href="http://peteohanlon.wordpress.com/">Peter O&#8217;Hanlon</a><em> </em><em></em></li>
<li> Silverlight support: yes</li>
<li>Documentation: sample application available with the library</li>
<li>Hosting: <a href="http://goldlight.codeplex.com/">CodePlex</a></li>
<li>License: MS-PL</li>
<li>Features:
<ul>
<li> &#8220;workspace&#8221; management (set of document that can be closed)</li>
<li>skin support</li>
<li>messaging between VM&#8217;s</li>
</ul>
</li>
</ul>
<p>As you can see, each frameworks has its pros and cons. If you want a light Silverlight-compatible framework, Laurent&#8217;s framework is very nice. If you want a very rich framework, Sacha&#8217;s one is excellent. Whatever what your requirements are, each framework is very interesting and browsing the source code is a good way to understrand how stuff works and find cool ideas for your own needs.</p>
<p>To conclude, I&#8217;d like to thanks all those people who work very hard for the community ! An interesting discussion started a couple of days ago on the <a href="http://groups.google.com/group/wpf-disciples">WPF disciples Google group</a> about the creation of a common framework. Have a look <a href="http://groups.google.com/group/wpf-disciples/browse_thread/thread/6b6c60b835cfd8f2">here</a>.</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.japf.fr%2f2009%2f10%2fa-quick-tour-of-existing-mvvm-frameworks%2f"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.japf.fr%2f2009%2f10%2fa-quick-tour-of-existing-mvvm-frameworks%2f" border="0" alt="kick it on DotNetKicks.com" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2009/10/a-quick-tour-of-existing-mvvm-frameworks/feed/</wfw:commentRss>
		<slash:comments>30</slash:comments>
		</item>
		<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('p252code21'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p25221"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p252code21"><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('p252code22'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p25222"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p252code22"><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('p252code23'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p25223"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p252code23"><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('p252code24'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p25224"><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="p252code24"><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>Adding transitions to a MVVM based dialog</title>
		<link>http://www.japf.fr/2009/04/adding-transitions-to-a-mvvm-based-dialog/</link>
		<comments>http://www.japf.fr/2009/04/adding-transitions-to-a-mvvm-based-dialog/#comments</comments>
		<pubDate>Sat, 04 Apr 2009 18:25:09 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[WPF]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[model-view-viewmodel]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[transition]]></category>
		<category><![CDATA[user experience]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=182</guid>
		<description><![CDATA[In my last blog post about MVVM, I showed how it is natural to build a common WPF dialog using DataBinding and Templates with the Model-View-ViewModel methodology. Because I&#8217;m having a lot of feedback on posts I wrote about how transitions can be done in WPF, I decided to reuse my previous demo application and [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://www.japf.fr/?p=154">last blog post</a> about MVVM, I showed how it is natural to build a common WPF dialog using DataBinding and Templates with the Model-View-ViewModel methodology. Because I&#8217;m having a lot of feedback on posts I wrote about how transitions can be done in WPF, I decided to reuse my previous demo application and add transitions when switching from one element to another. Here is the result of the demo application:</p>
<p><img src="http://www.japf.fr/wp-content/plugins/flash-video-player/default_video_player.gif" /></p>
<p>I didn&#8217;t wanted to implement all the transitions, instead I decided to use the famous <a href="http://www.codeplex.com/fluidkit">FluidKit</a> library and to wrap its transition control into a reusable and &#8220;MVVM compliant&#8221; control. I thank <a href="http://blog.pixelingene.com/">Pavan Podila</a> for giving me feedback while designing this control.</p>
<p>Basically, based on Pavan suggestions I created an INavigationAware interface and subclass transitions I wanted to reuse from FluidKit. The INavigationAware interface allows me to specify that the transition supports going forward and going backward (regarding the previous and current selection of the user in the menu).</p>
<p>The control itself (that I call a NavigationPresenter) is very simple, I just use 2 ContentPresenter that I switch when the Content property changes using the TransitionPresenter control from the FluidKit library. The NavigationPresenter works with 3 dependency properties:</p>
<ul>
<li>GoForward (bool): to specifiy the way of the transition</li>
<li>Transition: to specify the transition to use</li>
<li>Content: to specify the content of the control</li>
</ul>
<p>Here is the XAML for using the NavigationPresenter</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('p182code26'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p18226"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p182code26"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- The ContentProperty is not bound directly to the SelectedItem of the ListBox because the</span>
<span style="color: #808080; font-style: italic;">     GoForward property must be updated BEFORE the content changes. The CurrentContent property</span>
<span style="color: #808080; font-style: italic;">     is defined in the ViewModel class and updated everytime the selection of the ListBox changes,</span>
<span style="color: #808080; font-style: italic;">     after setting up the GoForward value. --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;NavigationTransition:NavigationPresenter</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;{Binding CurrentContent}&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">Transition</span>=<span style="color: #ff0000;">&quot;{Binding ElementName=transitionComboBox, Path=SelectedItem.Tag}&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">GoForward</span>=<span style="color: #ff0000;">&quot;{Binding DataContext.GoForward, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type AnimatedContentPresenter:MainDialog}}}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>            
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/NavigationTransition:NavigationPresenter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>The sample application comes with 3 animations but more can be used from the FluidKit library. It&#8217;s also possible to create your own transition (inherit from Transition base class). You can download the sample application <a href="http://www.japf.fr/download/AnimatedContentPresenter.zip">here</a>. Hope you&#8217;ll like it !</p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2009/04/adding-transitions-to-a-mvvm-based-dialog/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Thinking with MVVM: Data Templates + ContentControl</title>
		<link>http://www.japf.fr/2009/03/thinking-with-mvvm-data-templates-contentcontrol/</link>
		<comments>http://www.japf.fr/2009/03/thinking-with-mvvm-data-templates-contentcontrol/#comments</comments>
		<pubDate>Fri, 13 Mar 2009 14:53:04 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[model-view-viewmodel]]></category>
		<category><![CDATA[mvvm]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=154</guid>
		<description><![CDATA[I&#8217;m still playing with MVVM at work and I got a new occasion to setup a dialog using the MVVM methodology. I really love the process of creating User Interface using MVVM, the process is so much natural ! Basically, I needed to create a &#8220;setup&#8221; dialog in the application I&#8217;m currently working on. I [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m still playing with MVVM at work and I got a new occasion to setup a dialog using the MVVM methodology.</p>
<p>I really love the process of creating User Interface using MVVM, the process is so much natural ! Basically, I needed to create a &#8220;setup&#8221; dialog in the application I&#8217;m currently working on. I wanted to achieve something like this:</p>
<p style="text-align: center;"><a href="http://www.japf.fr/wp-content/uploads/2009/03/setup-dialog.png" rel="lightbox[154]"><img class="size-medium wp-image-155 aligncenter" title="setup-dialog" src="http://www.japf.fr/wp-content/uploads/2009/03/setup-dialog-300x180.png" alt="setup-dialog" width="300" height="180" /></a></p>
<p style="text-align: left;">Of course, I wanted to create this dialog using the power of WPF. My primary objective when I create a new dialog is to keep the code-behind empty. Using MVVM it took me about 10 minutes to create the basic structure of the dialog withouth writting a single line of code in the xaml.cs files.</p>
<p style="text-align: left;">Here is the process I followed to create this dialog. This is the &#8220;thinking with MVVM&#8221; part because I&#8217;m explaining the reasoning I had <img src='http://www.japf.fr/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  You can download the source code for this example <a href="http://www.japf.fr/download/ConfigurationDialogExample.zip">here</a>.</p>
<p style="text-align: left;"><strong>Setup the main dialog</strong></p>
<p style="text-align: left;">When I need quick prototypes, I often don&#8217;t use Blend and type the XAML directly into Visual Studio editor. That was the very first part I did to write this XAML:</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('p154code33'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15433"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p154code33"><pre class="xml" style="font-family:monospace;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid.ColumnDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ColumnDefinition</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ColumnDefinition</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;3*&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid.ColumnDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ListBox</span> <span style="color: #000066;">Grid.Column</span>=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;5&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Border</span> <span style="color: #000066;">Grid.Column</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;5&quot;</span> <span style="color: #000066;">BorderBrush</span>=<span style="color: #ff0000;">&quot;#FF7F9DB9&quot;</span> <span style="color: #000066;">BorderThickness</span>=<span style="color: #ff0000;">&quot;1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #808080; font-style: italic;">&lt;!-- Content goes here --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Border<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p style="text-align: left;">Pretty easy isn&#8217;t it. Please note the &#8220;Content goes here&#8221; comment. I knew I wanted to put the settings dialogs here, but I didn&#8217;t know how&#8230; I named this file <em>ConfigurationDialog.xaml</em>.</p>
<p style="text-align: left;"><strong>Create sub-configuration dialogs</strong></p>
<p style="text-align: left;">For this example, I created 2 sub-configuration dialogs just for the demo. I just put a TextBlock into a Grid to demonstrate the principles. Of course, we should add real configuration controls such as CheckBox, TextBox, etc. Those 2 new files are <em>GeneralSettingsView</em> and <em>AdvancedSettingsView</em> and are UserControls.</p>
<p style="text-align: left;"><strong>Create a ViewModel class for each View</strong></p>
<p style="text-align: left;">I used to use an abstract base class for all my ViewModel classes where I implement the INofityPropertyChanged interface. I also use a trick from <a href="http://joshsmithonwpf.wordpress.com/">Josh Smith</a> to throw an exception if the property name doesn&#8217;t exist when the PropertyChanged event is raised.</p>
<p style="text-align: left;">That gives us 3 new files: <em>ConfigurationDialogViewModel</em>, <em>GeneralSettingsViewModel</em> and <em>AdvancedSettingsViewModel.</em> Because I wanted configuration dialogs to have a common <em>Name</em> property (to identify them in the UI), I created a base class <em>SettingsViewModelBase</em>.</p>
<p style="text-align: left;"><strong>Setup the ViewModel of the main View</strong></p>
<p style="text-align: left;">I needed the main View to expose the other configuration views available. I used an ObservableCollection for that:</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('p154code34'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15434"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p154code34"><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> ConfigurationDialogViewModel <span style="color: #008000;">:</span> ViewModelBase
<span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> ObservableCollection<span style="color: #008000;">&lt;</span>SettingsViewModelBase<span style="color: #008000;">&gt;</span> settings<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> ObservableCollection<span style="color: #008000;">&lt;</span>SettingsViewModelBase<span style="color: #008000;">&gt;</span> Settings
        <span style="color: #008000;">&#123;</span>
            get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">settings</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> ConfigurationDialogViewModel<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;">settings</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ObservableCollection<span style="color: #008000;">&lt;</span>SettingsViewModelBase<span style="color: #008000;">&gt;</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;">settings</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> GeneralSettingsViewModel<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">settings</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> AdvancedSettingsViewModel<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p style="text-align: left;"><strong>Setup the main View  to consume datas from its ViewModel</strong></p>
<p style="text-align: left;">I did some changes in the XAML to databind the ListBox&#8217;s ItemsSource property to the ViewModel. Because I always set the DataContext property of a view to its associated ViewModel, there is no ambiguity:</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('p154code35'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15435"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p154code35"><pre class="xml" style="font-family:monospace;">ItemsSource=&quot;{Binding Settings}&quot;.</pre></td></tr></table></div>

<p style="text-align: left;">The ListBox control has no idea how a <em>SettingsViewModelBase </em>object (that is in the associated ObservableCollection) should be displayed. We need a simple DataTemplate to specify this information:</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('p154code36'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15436"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p154code36"><pre class="xml" style="font-family:monospace;">            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ListBox.ItemTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DataTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Name}&quot;</span> <span style="color: #000066;">Padding</span>=<span style="color: #ff0000;">&quot;10&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/DataTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ListBox.ItemTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p style="text-align: left;"><strong>And finish using my favourite part !</strong></p>
<p style="text-align: left;">We just setup a ListBox to consume a collection of <em>SettingsViewModelBase </em>class to build a menu, fine. By using a very simple DataTemplate, we said: <em>&#8220;I want to render the SettingsViewModelBase object in the ListBox using a TextBlock&#8221;</em>.</p>
<p style="text-align: left;">We can take this reasoning one step further. We have a View associated to each SettingsViewModelBase. ListBox was fine to have a collection of controls. How could we display only one control ?</p>
<p style="text-align: left;">We can use a ContentControl of course ! Because we want do display the dialog that is currently selected in the menu, we can use a simple DataBinding (here, the ListBox has been named ListBoxMenu):</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('p154code37'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15437"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p154code37"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ContentControl</span> <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;{Binding ElementName=ListBoxMenu, Path=SelectedItem}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></td></tr></table></div>

<p style="text-align: left;">One more time, we got a rendering problem. The ContentControl doens&#8217;t know how to render a <em>SettingsViewModelBase </em>object. Well, it&#8217;s not a big deal, just specify it:</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('p154code38'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15438"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p154code38"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Window.Resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DataTemplate</span> <span style="color: #000066;">DataType</span>=<span style="color: #ff0000;">&quot;{x:Type ViewModel:GeneralSettingsViewModel}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;View:GeneralSettingsView</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/DataTemplate</span></span>
<span style="color: #009900;">        <span style="color: #000000; font-weight: bold;">&lt;DataTemplate</span> <span style="color: #000066;">DataType</span>=<span style="color: #ff0000;">&quot;{x:Type ViewModel:AdvancedSettingsViewModel}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;View:AdvancedSettingsView</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/DataTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Window.Resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p style="text-align: left;">What I&#8217;m saying here is that GeneralSettingsViewModel should be rendered using a GeneralSettingsView. That&#8217;s exactly what we need ! Because the Views are created using a DataTemplate, we do not need to setup the DataContext, it will be automatically registered to the templated object, the ViewModel.</p>
<p style="text-align: left;"><strong>Conclusion</strong></p>
<p style="text-align: left;">The example I describe here is very simple. The goal was to show the process of creating a dialog thinking in the &#8220;MVVM&#8221; way. It was very natural to use the concepts I explain here. The code-behinds are completely empty and ViewModel classes are testable ! Because the View are XAML only, I can give them to a designer that will tweak them to make them have a nice look.</p>
<p style="text-align: left;">You can download the source code for this example <a href="http://www.japf.fr/download/ConfigurationDialogExample.zip">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2009/03/thinking-with-mvvm-data-templates-contentcontrol/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
