<?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; WPF</title>
	<atom:link href="http://www.japf.fr/tag/wpf/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.japf.fr</link>
	<description>Jeremy Alles Presentation Foundation: WPF, Silverlight, Windows Phone 7, Windows 8</description>
	<lastBuildDate>Fri, 27 Jan 2012 07:57:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>BUILD: WinRT, Silverlight, WPF, XAML</title>
		<link>http://www.japf.fr/2011/09/build-winrt-silverlight-wpf-xaml/</link>
		<comments>http://www.japf.fr/2011/09/build-winrt-silverlight-wpf-xaml/#comments</comments>
		<pubDate>Fri, 16 Sep 2011 03:23:39 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[Metro]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Windows 8]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[metro]]></category>
		<category><![CDATA[windows8]]></category>
		<category><![CDATA[winrt]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=976</guid>
		<description><![CDATA[This blog post is part of my BUILD series. I&#8217;m having a very busy week here in Anaheim ! I&#8217;m meeting many new people and had the chance to enjoy the conference from the inside. I&#8217;m also playing with this new Windows 8 slate Microsoft gave us ! I&#8217;m not going to do a blog [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.japf.fr/wp-content/uploads/2011/09/build_logo.png" rel="lightbox[976]"><img title="build_logo" src="http://www.japf.fr/wp-content/uploads/2011/09/build_logo.png" alt="" width="132" height="47" /></a></p>
<p><em>This blog post is part of my <a href="http://build.japf.fr">BUILD series</a>.</em></p>
<p>I&#8217;m having a very busy week here in Anaheim ! I&#8217;m meeting many new people and had the chance to enjoy the conference from the inside. I&#8217;m also playing with this new Windows 8 slate Microsoft gave us ! I&#8217;m not going to do a blog post trying to summarize everything because there is just so much to say.I&#8217;m going to try to share my point of view on what I&#8217;ve seen here.</p>
<p><strong>Our new platform</strong></p>
<p><strong></strong>The original picture shown during the keynote to introduce the new platform was this one:</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2011/09/win8-platform-and-tools.jpg" rel="lightbox[976]"><img class="alignnone size-full wp-image-978" title="win8-platform-and-tools" src="http://www.japf.fr/wp-content/uploads/2011/09/win8-platform-and-tools.jpg" alt="" width="490" height="276" /></a></p>
<p>There has been a lot of confusion about that because of having XAML with C# in the Metro Style Apps without any reference to the CLR&#8230; <a href="http://dougseven.com/">Doug Steven</a> did a pretty great job (blog post is <a href="http://dougseven.com/2011/09/15/a-bad-picture-is-worth-a-thousand-long-discussions/">here</a>) by discussing with key people from the engineering team of Microsoft and creates this new more accurate picture:</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2011/09/win8-new-platform.png" rel="lightbox[976]"><img class="alignnone size-full wp-image-977" title="win8-new-platform" src="http://www.japf.fr/wp-content/uploads/2011/09/win8-new-platform.png" alt="" width="490" height="275" /></a></p>
<p>Here is a quick summary:</p>
<ul>
<li>there is only one CLR</li>
<li>.Net framework 4.5 is used in both Metro apps and Classic apps</li>
<li>it&#8217;s the same MSIL for Metro apps and Classic apps</li>
<li>in the Metro platform, we have a subset of the .Net framework (for example no OpenFileDialog&#8230;)</li>
</ul>
<p><strong>New opportunities</strong></p>
<p>Before //BUILD we had already many choices to choose our development environment. we now have even more:</p>
<ul>
<li>WPF and managed code for classic desktop apps</li>
<li>Silverlight in a web environment</li>
<li>Silverlight out of browser</li>
<li>WinRT + XAML for Metro apps</li>
<li>WinRT + HTML for Metro apps</li>
</ul>
<p>I personally think that Silverlight in a web browser has not a great future. Microsoft just announced for example that the immersive version of IE will not run any plugins (so no Silverlight in the Metro UI) and we &#8216;ll know Microsoft is pushing HTML5 very strongly.</p>
<p>For classic desktop apps we have 2 options: WPF and Silverlight. Each of them has advantages and the choice we&#8217;ll have to do will depend on our constraints (deployment, business needs, connectivity&#8230;). I think there is room for the 2 platforms there.</p>
<p>For the Metro UI, you can choose between XAML and HTML. Microsoft told us they will keep a good feature parity between the 2 options. If you choose XAML and managed code you&#8217;ll be able to leverage a subset of the .Net framework.</p>
<p>I think another important aspect is that Metro will be available on Windows 8 only. Even though this new version of the OS might have a fast deployment rate (thanks to the slates), in many companies I don&#8217;t think it will be that fast.This, plus the fact that some LOB apps will not benefit the Metro UI leaves a lot of work to do in the desktop applications world (where we have both WPF and SL)&#8230; For WPF, we now have a new version coming in .Net 4.5. You can check out the new stuff <a href="http://msdn.microsoft.com/en-us/library/bb613588(v=VS.110).aspx">here</a> in the documentation.</p>
<p>In my next blog post I&#8217;m going to try to go deeper in the new WinRT/XAML world and see how it looks like for us, WPF and Silverlight developers.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2011/09/build-winrt-silverlight-wpf-xaml/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>MVVM Framework explorer updated</title>
		<link>http://www.japf.fr/2011/09/mvvm-framework-explorer-updated/</link>
		<comments>http://www.japf.fr/2011/09/mvvm-framework-explorer-updated/#comments</comments>
		<pubDate>Mon, 05 Sep 2011 11:57:50 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Windows Phone]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[model-view-viewmodel]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[wp7]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=954</guid>
		<description><![CDATA[I just updated my MVVM frameworks explorer Silverlight application. You can find the updated application here. Here is the top 5 of MVVM frameworks supporting WPF, Silverlight and Windows Phone 7: MVVM Light (61k downloads) nRoute (19k downloads) Caliburn Micro (18k downloads) Simple MVVM toolkit (5k downloads) Catel (5k downloads)]]></description>
			<content:encoded><![CDATA[<p>I just updated my MVVM frameworks explorer Silverlight application. You can find the updated application <a href="http://www.japf.fr/silverlight/mvvm/index.html">here</a>.</p>
<p>Here is the top 5 of MVVM frameworks supporting WPF, Silverlight and Windows Phone 7:</p>
<ol>
<li><a href="http://mvvmlight.codeplex.com/">MVVM Light</a> (61k downloads)</li>
<li><a href="http://nroute.codeplex.com/">nRoute</a> (19k downloads)</li>
<li><a href="http://caliburnmicro.codeplex.com/">Caliburn Micro</a> (18k downloads)</li>
<li><a href="http://simplemvvmtoolkit.codeplex.com/">Simple MVVM toolkit</a> (5k downloads)</li>
<li><a href="http://catel.codeplex.com/">Catel</a> (5k downloads)</li>
</ol>
<p><a href="http://www.japf.fr/wp-content/uploads/2011/09/mvvm-frameworks.png" rel="lightbox[954]"><img class="alignnone size-full wp-image-955" title="mvvm-frameworks" src="http://www.japf.fr/wp-content/uploads/2011/09/mvvm-frameworks.png" alt="" width="464" height="80" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2011/09/mvvm-framework-explorer-updated/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>When enough ViewModel is enough</title>
		<link>http://www.japf.fr/2011/04/when-enough-viewmodel-is-enough/</link>
		<comments>http://www.japf.fr/2011/04/when-enough-viewmodel-is-enough/#comments</comments>
		<pubDate>Tue, 19 Apr 2011 12:26:59 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Windows Phone]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[wp7]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=906</guid>
		<description><![CDATA[In the last few years, we&#8217;ve seen the WPF and Silverlight community embracing the MVVM methodology. As one of the early adopters of MVVM (one of my first post about the subject was late 2008), I&#8217;ve seen the pattern evolving both in the web community and with developers I&#8217;ve met in my daily life. Today, [...]]]></description>
			<content:encoded><![CDATA[<p>In the last few years, we&#8217;ve seen the WPF and Silverlight community embracing the MVVM methodology. As one of the early adopters of MVVM (one of <a href="http://www.japf.fr/2008/12/why-should-i-use-model-view-viewmodel-pattern/">my first post</a> about the subject was late 2008), I&#8217;ve seen the pattern evolving both in the web community and with developers I&#8217;ve met in my daily life.</p>
<p>Today, I&#8217;d like to share with you a simple concept I try to stick to when I&#8217;m doing WPF, Silverlight or Windows Phone 7 development. It can be summarized as &#8220;Enough ViewModel is enough&#8221;.</p>
<p>The simple idea behind this slogan is that there ARE stuff which are view-related and SHOULD NOT be embedded in the ViewModel layer. I&#8217;ve seen too many developers going the &#8220;100% viewmodel way&#8221; which means for them absolutely no code-behind without any dispensation.</p>
<p>For example, I came across this code. It&#8217;s the ViewModel layer associated to a simple view where the user fills various input and has immediate feedback about the progress (like &#8220;75% of the fields are completed&#8221;). If by the way you&#8217;re interested in implementing this behavior you can check out <a href="http://www.codeproject.com/KB/smart/WPF_Validation_Attribute.aspx">this article</a> I wrote on CodeProject)</p>
<p>The following code is simplified to the sake of the article:</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('p906code3'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p9063"><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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
</pre></td><td class="code" id="p906code3"><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> ViewModel
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// data field acts as the model object behind the VM layer</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> Data data<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// missing code...</span>
    <span style="color: #008080; font-style: italic;">// public properties used by the view using databinding</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> ViewModel<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// very simplified for this article...</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">data</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Data<span style="color: #008000;">&#40;</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;">data</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SelectedValuesChanged</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> EventHandler<span style="color: #008000;">&#40;</span>data_SelectedValuesChanged<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;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> UpdateProgress<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// some code...</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #6666cc; font-weight: bold;">void</span> data_SelectedValuesChanged<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<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;">UpdateProgress</span><span style="color: #008000;">&#40;</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;">Initialize</span><span style="color: #008000;">&#40;</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;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Initialize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        var item <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">data</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetItem</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;id1&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>item <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            item<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyChanged</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> PropertyChangedEventHandler<span style="color: #008000;">&#40;</span>item_PropertyChanged<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            item<span style="color: #008000;">.</span><span style="color: #0000FF;">SelectedValuesChanged</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> EventHandler<span style="color: #008000;">&#40;</span>item_SelectedValuesChanged<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var value <span style="color: #0600FF; font-weight: bold;">in</span> item<span style="color: #008000;">.</span><span style="color: #0000FF;">Values</span><span style="color: #008000;">&#41;</span>
                value<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyChanged</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> PropertyChangedEventHandler<span style="color: #008000;">&#40;</span>item_PropertyChanged<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        item <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">data</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetItem</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;id2&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>item <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            item<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyChanged</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> PropertyChangedEventHandler<span style="color: #008000;">&#40;</span>item_PropertyChanged<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            item<span style="color: #008000;">.</span><span style="color: #0000FF;">SelectedValuesChanged</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> EventHandler<span style="color: #008000;">&#40;</span>item_SelectedValuesChanged<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var value <span style="color: #0600FF; font-weight: bold;">in</span> item<span style="color: #008000;">.</span><span style="color: #0000FF;">Values</span><span style="color: #008000;">&#41;</span>
                value<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyChanged</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> PropertyChangedEventHandler<span style="color: #008000;">&#40;</span>item_PropertyChanged<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: #6666cc; font-weight: bold;">void</span> item_PropertyChanged<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, <span style="color: #000000;">System.<span style="color: #0000FF;">ComponentModel</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyChangedEventArgs</span> e<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;">UpdateProgress</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #6666cc; font-weight: bold;">void</span> item_SelectedValuesChanged<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<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;">UpdateProgress</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></pre></td></tr></table></div>

<p>The idea is simple, as soon as the user changes a value in the View, we must compute the current progress. Because the ViewModel have several levels, we end-up having to register to every single PropertyChanged event which leads to cumbersome code. By the way, this code can also creates memory leaks since we register a lot of event handlers without removing them, but that&#8217;s another discussion&#8230;</p>
<p>Here is my way to solve this problem:</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('p906code4'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p9064"><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
</pre></td><td class="code" id="p906code4"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">partial</span> <span style="color: #6666cc; font-weight: bold;">class</span> View <span style="color: #008000;">:</span> UserControl
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> ViewModel viewmodel<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> View<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        InitializeComponent<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;">viewmodel</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ViewModel<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;">AddHandler</span><span style="color: #008000;">&#40;</span>
            FocusManager<span style="color: #008000;">.</span><span style="color: #0000FF;">LostFocusEvent</span>,
            <span style="color: #008000;">new</span> RoutedEventHandler<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnLostFocus</span><span style="color: #008000;">&#41;</span>,
            <span style="color: #0600FF; font-weight: bold;">true</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> OnLostFocus<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, RoutedEventArgs e<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;">viewmodel</span><span style="color: #008000;">.</span><span style="color: #0000FF;">UpdateProgress</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></pre></td></tr></table></div>

<p>What is wrong with this way ? The View has code-behind ? That&#8217;s not a big deal: the code is more readable, maintainable. It makes also more sense: when a view-related operation occurs (in this case, focus has changed), update the progress.</p>
<p>The simple message I&#8217;d like to spread is the fact that there is nothing wrong with the fact to have <strong>sometime</strong>, a <strong>little bit of code-behind</strong> in your view<strong> if</strong> that facilitates your architecture. There is no need to create a complicated infrastructure with behaviors, commands or bindings just to keep the view empty if that does not make sense.</p>
<p>Another great example in a real application is available in the <a href="http://stores.lulu.com/joshsmithonwpf">Advanced MVVM</a> book by <a href="http://joshsmithonwpf.wordpress.com/">Josh Smith</a>.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2011/04/when-enough-viewmodel-is-enough/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>WPF databinding trick (part 2)</title>
		<link>http://www.japf.fr/2011/04/wpf-databinding-trick-part-2/</link>
		<comments>http://www.japf.fr/2011/04/wpf-databinding-trick-part-2/#comments</comments>
		<pubDate>Mon, 04 Apr 2011 07:04:12 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[binding]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=861</guid>
		<description><![CDATA[Ok, let&#8217;s see another strange behavior that you might have already seen with the WPF databinding engine. INotifyPropertyChanged When we teach WPF to new developers, at some point we need to introduce the INotifyPropertyChanged interface. Usually, the kind of speech which is given looks like this one: &#8220;When you&#8217;re doing a binding to a standard [...]]]></description>
			<content:encoded><![CDATA[<p>Ok, let&#8217;s see another strange behavior that you might have already seen with the WPF databinding engine.</p>
<p><strong>INotifyPropertyChanged</strong></p>
<p>When we teach WPF to new developers, at some point we need to introduce the INotifyPropertyChanged interface. Usually, the kind of speech which is given looks like this one:</p>
<p>&#8220;<em>When you&#8217;re doing a binding to a standard CLR property, you must add some kind of notification mechanisms in order to tell the binding when the value changes. In WPF, this is standardized by using the common INotifyPropertyChanged interface. This interface contains a single event which must be raised with the name of the property which has changed. Whenever this event is fired, the databinding engine is able to see the change and update the corresponding binding.</em>&#8221;</p>
<p><strong>Demo application</strong></p>
<p>Nothing really exciting here, right. Now, you start to do a demo with the following code:</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('p861code8'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p8618"><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
</pre></td><td class="code" id="p861code8"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">partial</span> <span style="color: #6666cc; font-weight: bold;">class</span> MainWindow <span style="color: #008000;">:</span> Window
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> MainWindow<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        InitializeComponent<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> <span style="color: #008000;">new</span> DataObject<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> Data <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;test &quot;</span> <span style="color: #008000;">&#125;</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;">class</span> DataObject
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> data<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Data
    <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;">data</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;">data</span> <span style="color: #008000;">=</span> value<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>Here the DataObject class I use as DataContext does not implement INotifyPropertyChanged.Then I added some XAML in the MainWindow:</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('p861code9'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p8619"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p861code9"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Window</span> <span style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;TestDataContext.MainWindow&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">xmlns:x</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">Title</span>=<span style="color: #ff0000;">&quot;MainWindow&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;350&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;525&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;StackPanel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBox</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Data}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBox</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Data}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBox</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Data}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/StackPanel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Window<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>And the goal is to display 3 TextBoxes all databound to the same property. Now, we run the application, types some text in one of the TextBox and change the focus in order to update the binding. Most of us would expect to have the 2 other Textboxes with the old value: when the focus has been lost the new string value has been pushed into the Data property, but the other Textboxes have no way to detect this change.</p>
<p>Actually if you run this application, you&#8217;ll see all the Textboxes being updated That&#8217;s strange&#8230;</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2011/04/exemplebinding.png" rel="lightbox[861]"><img src="http://www.japf.fr/wp-content/uploads/2011/04/exemplebinding.png" alt="" title="exemplebinding" width="374" height="454" class="alignnone size-full wp-image-868" /></a></p>
<p><strong>Why does it works ?</strong></p>
<p>Ok, let&#8217;s get into the dirty details. Here are what happens during initialization:</p>
<ul type="disc">
<li>XAML is parsed</li>
<li>for the 3 TextBox
<ul type="disc">
<li>the Binding is initialized</li>
<li>a BindingExpression is       created. The BindingExpression is a IWeakEventListener.</li>
<li>its AttachOverride method is       called</li>
<li>if the UpdateOnLostFocus       flag is set (which is the case because the default value of       UpdateSourceTrigger is LostFocus), the static        LostFocusEventManager type is used and the AddListener is called
<ul type="circle"></ul>
</li>
</ul>
<ul type="disc">
<li>during the initialization of       the binding, the PropertyPathWorker class is added and at some point the        ReplaceItem method gets called</li>
<li> then the following code         gets execute</li>
</ul>
</li>
</ul>

<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('p861code10'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p86110"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p861code10"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>source <span style="color: #008000;">is</span> INotifyPropertyChanged<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	PropertyChangedEventManager<span style="color: #008000;">.</span><span style="color: #0000FF;">AddListener</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: #0600FF; font-weight: bold;">else</span>
<span style="color: #008000;">&#123;</span>
	PropertyDescriptor descriptor <span style="color: #008000;">=</span> GetDescriptor<span style="color: #008000;">&#40;</span>source, path<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	ValueChangedEventManager<span style="color: #008000;">.</span><span style="color: #0000FF;">AddListener</span><span style="color: #008000;">&#40;</span>…<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Which means:</p>
<ul type="disc">
<li>if the source implements      INotifyPropertyChanged, use that to track the changes to the property</li>
<li>otherwise, use the      ValueChanged event of the cached PropertyDescriptor instance to track the changes</li>
</ul>
<p>What happens when one of the TextBox lost focus:</p>
<ul type="disc">
<li>the focus changes to another      control, the previously focused TextBox gets is LostFocus event raised</li>
<li>the LostFocusEventManager      gets the notification</li>
<li>the notification is      transferred to the BindingExpression (which is a IWeakEventListener) by      calling the IWeakEventListener.ReceiveWeakEvent method</li>
<li>several method calls&#8230; ending      in the PropertyPathWorker class where the PropertyDescriptor.SetValue      methods is called (where the PropertyDescriptor is the one which has been cached during initialization)</li>
<li>this method raises the      ValueChanged event at the PropertyDescriptor level</li>
<li>which is catched by the      ValueChangedEventManager</li>
<li>and the associated dependency      property (Text) is updated</li>
<li>and voila !</li>
</ul>
<p>So there are no miracle behind the demo application I was talking about, just the fact the binding engine is smart enough to cache the PropertyDescriptor used for setting value to CLR property and using the ValueChanged event to get notifications if the source does not implement INotifyPropertyChanged.</p>
<p>Happy coding !</p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2011/04/wpf-databinding-trick-part-2/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>WPF databinding trick (part 1)</title>
		<link>http://www.japf.fr/2011/03/wpf-databinding-trick-part-1/</link>
		<comments>http://www.japf.fr/2011/03/wpf-databinding-trick-part-1/#comments</comments>
		<pubDate>Fri, 25 Mar 2011 14:14:54 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[WPF]]></category>
		<category><![CDATA[binding]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=844</guid>
		<description><![CDATA[The last week, one of my colleague was doing a WPF training session and she ended up having a very strange behavior with the WPF databinding engine. I&#8217;ll describe a not well-known behavior of the WPF databinding engine and I&#8217;ll try to explain how its works under the scene. Binding the Text property of a [...]]]></description>
			<content:encoded><![CDATA[<p>The last week, one of my colleague was doing a WPF training session and she ended up having a very strange behavior with the WPF databinding engine. I&#8217;ll describe a not well-known behavior of the WPF databinding engine and I&#8217;ll try to explain how its works under the scene.</p>
<p><strong>Binding the Text property of a TextBlock to a collection of objects</strong></p>
<p>This is something you&#8217;re not supposed to do, but you&#8217;ll see it actually works in an unexpected way. In the code-behind of our Window, we have the following C# code:</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('p844code15'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p84415"><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
</pre></td><td class="code" id="p844code15"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">partial</span> <span style="color: #6666cc; font-weight: bold;">class</span> MainWindow <span style="color: #008000;">:</span> Window
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> MainWindow<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        InitializeComponent<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        Person person1 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Person <span style="color: #008000;">&#123;</span> Age <span style="color: #008000;">=</span> <span style="color: #FF0000;">45</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
        Person person2 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Person <span style="color: #008000;">&#123;</span> Age <span style="color: #008000;">=</span> <span style="color: #FF0000;">63</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
        Person person3 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Person <span style="color: #008000;">&#123;</span> Age <span style="color: #008000;">=</span> <span style="color: #FF0000;">71</span> <span style="color: #008000;">&#125;</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> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span>Person<span style="color: #008000;">&gt;</span> <span style="color: #008000;">&#123;</span> person1, person2, person3 <span style="color: #008000;">&#125;</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;">class</span> Person
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> Age
    <span style="color: #008000;">&#123;</span>
        get<span style="color: #008000;">;</span>
        set<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The associated XAML looks like the following:</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('p844code16'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p84416"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p844code16"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Window</span> <span style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;WpfApplicationDataContext.MainWindow&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">xmlns:x</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span> </span>
<span style="color: #009900;">        <span style="color: #000066;">Title</span>=<span style="color: #ff0000;">&quot;MainWindow&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;350&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;525&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;StackPanel<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;">Margin</span>=<span style="color: #ff0000;">&quot;10&quot;</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding Age}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/StackPanel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Window<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Here we&#8217;re doing something not usual: we&#8217;re binding the Text property of the TextBlock (which is of type string) to a collection of objects.</p>
<p><strong>What do you think the Window will display ?</strong></p>
<p>The first time I saw this code, I thought the Window will remain empty. The DataContext is set to a List and we&#8217;re trying to find an &#8220;Age&#8221; property on this collection: THAT CANNOT WORK.</p>
<p>Actually, it does&#8230;</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2011/03/window.png" rel="lightbox[844]"><img src="http://www.japf.fr/wp-content/uploads/2011/03/window.png" alt="" title="window" width="263" height="105" class="alignnone size-full wp-image-852" /></a></p>
<p><strong>What&#8217;s happening behind the scene ?</strong></p>
<p>Let&#8217;s take a deep breath a take a look at what&#8217;s happening behind the scene to make this works.</p>
<p><em>1. A CollectionView is created</em></p>
<p>The first thing I was thinking while talking about the problem with my colleague was the use of a collection view. As you probably now, every time you create a binding to a collection property, the engine creates a collection view for you. This view will serve as an intermediate layer between your source and the target to manage selection, filtering and grouping. A good introduction to collection views is available <a href="http://marlongrech.wordpress.com/2008/11/22/icollectionview-explained/">here</a>.</p>
<p>What is important to know is that collection views are shared along same objects instance. It means that when you use the CollectionViewSource.GetDefaultView() method, you&#8217;re either creating a default view for the collection (if no one has requested one before) or getting the default view (if it has been created by calling the same method before you).</p>
<p>To make sure I was right about my hypothesis, I added the following code in the C#:</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('p844code17'); return false;">View Code</a> CHSARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p84417"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p844code17"><pre class="chsarp" style="font-family:monospace;">var collectionView = CollectionViewSource.GetDefaultView(this.DataContext);
collectionView.MoveCurrentToNext();
this.MouseDoubleClick += (s, e) =&gt; collectionView.MoveCurrentToNext();</pre></td></tr></table></div>

<p>And the Window is now displaying the second item of the list:</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2011/03/window2.png" rel="lightbox[844]"><img src="http://www.japf.fr/wp-content/uploads/2011/03/window2.png" alt="" title="window2" width="263" height="105" class="alignnone size-full wp-image-854" /></a></p>
<p>So we&#8217;re definitively dealing with a collection view. The next question was, where the value comes from, I specified &#8220;Age&#8221; as binding path&#8230;</p>
<p><em>2. The CurrentItem property of ICollectionView<br />
</em></p>
<p>If you take a look at the documentation about ICollectionView, you&#8217;ll find this property:</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2011/03/CurrentItem.png" rel="lightbox[844]"><img class="alignnone size-full wp-image-849" title="CurrentItem" src="http://www.japf.fr/wp-content/uploads/2011/03/CurrentItem.png" alt="" width="537" height="30" /></a></p>
<p>So it looks like the Text property of my TextBlock is databound to this CurrentItem property while I explicitly set to the Age property&#8230;</p>
<p><em>3. The magic of the PropertyPathWorker class</em></p>
<p>Using Reflector, I looked for potential types using the ICollectionView.CurrentItem property. I found an interesting class: PropertyPathWorker.In the source code of the .Net framework, this type is defines as &#8220;<em>the workhorse for CLR binding</em>&#8220;.</p>
<p>In particular, take a look at this method:</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('p844code18'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p84418"><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="p844code18"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> ReplaceItem<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> k, <span style="color: #6666cc; font-weight: bold;">object</span> newO, <span style="color: #6666cc; font-weight: bold;">object</span> parent<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// some code...</span>
&nbsp;
    view <span style="color: #008000;">=</span> CollectionViewSource<span style="color: #008000;">.</span><span style="color: #0000FF;">GetDefaultCollectionView</span><span style="color: #008000;">&#40;</span>parent, TreeContext<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// some more code...</span>
&nbsp;
    newO <span style="color: #008000;">=</span> view<span style="color: #008000;">.</span><span style="color: #0000FF;">CurrentItem</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>newO <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        GetInfo<span style="color: #008000;">&#40;</span>k, newO, <span style="color: #0600FF; font-weight: bold;">ref</span> svs<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        svs<span style="color: #008000;">.</span><span style="color: #0000FF;">collectionView</span> <span style="color: #008000;">=</span> view<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span> 
&nbsp;
    <span style="color: #008080; font-style: italic;">// and bam ! we're now using view.CurrentItem as source for our binding</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>So we&#8217;ve a special case when we&#8217;re creating a binding with a collection view: the CurrentItem property is automatically used and merge with the specificied path. In our case, it&#8217;s like creating a binding to CurrentItem.Age.</p>
<p><em>3. And voila !</em></p>
<p>Finally we&#8217;ve a lot going on within the engine to make this works. Of course the original binding was not something we would do in normal application but it was kind of cool doing the investigations to find out why it was working ! Hope you learn something cool about the databinding engine <img src='http://www.japf.fr/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Next week I&#8217;ll try to write a similar article about another strange behavior you might have already seen&#8230;</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2011/03/wpf-databinding-trick-part-1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Set up the main window of your WPF project</title>
		<link>http://www.japf.fr/2010/12/set-up-main-window-of-your-wpf-project/</link>
		<comments>http://www.japf.fr/2010/12/set-up-main-window-of-your-wpf-project/#comments</comments>
		<pubDate>Wed, 01 Dec 2010 08:59:11 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=704</guid>
		<description><![CDATA[Intro: In this post, I&#8217;m exploring various techniques we can use in order to have a custom Window in a WPF project. Today you start a new application. You open Visual Studio, &#8220;File&#8221;, &#8220;New&#8221;, &#8220;Project&#8221;, &#8220;WPF Application&#8221; and you start typing code&#8230; At some point, you might want to tune the main window of your [...]]]></description>
			<content:encoded><![CDATA[<p><em>Intro: In this post, I&#8217;m exploring various techniques we can use in order to have a custom Window in a WPF project.</em></p>
<p>Today you start a new application. You open Visual Studio, &#8220;File&#8221;, &#8220;New&#8221;, &#8220;Project&#8221;, &#8220;WPF Application&#8221; and you start typing code&#8230; At some point, you might want to tune the main window of your application in order to have a custom header. The most common reason to do so is when you want to render some UI elements outside the standard client area of a window:</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2010/11/ClientArea.png" rel="lightbox[704]"><img class="alignnone size-full wp-image-711" title="ClientArea" src="http://www.japf.fr/wp-content/uploads/2010/11/ClientArea.png" alt="" width="241" height="209" /></a></p>
<p>For example in order to have something similar to Office 2010 (with icons inside the Window&#8217;s header):</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2010/11/OutsideClientArea.png" rel="lightbox[704]"><img class="alignnone size-full wp-image-707" title="OutsideClientArea" src="http://www.japf.fr/wp-content/uploads/2010/11/OutsideClientArea.png" alt="" width="114" height="111" /></a></p>
<p>Also, you want to make sure you application works also great on Windows XP. This is probably not your personal choice but some guy from the marketing told you: it MUST runs on XP <img src='http://www.japf.fr/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Let see what are the various options you can choose</p>
<p><strong>Option1: use the WindowStyle = None option on the &#8220;standard&#8221; WPF Window</strong></p>
<ul>
<li>How to do?</li>
</ul>
<p>In the XAML (or in the C# code), simply set the value of the WindowStyle property of your Window to None:</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('p704code24'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p70424"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p704code24"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Window</span> <span style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;WpfApplication1.MainWindow&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">xmlns:x</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">Title</span>=<span style="color: #ff0000;">&quot;MainWindow&quot;</span> </span>
<span style="color: #009900;">        <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;250&quot;</span>  </span>
<span style="color: #009900;">        <span style="color: #000066;">WindowStyle</span>=<span style="color: #ff0000;">&quot;None&quot;</span> </span>
<span style="color: #009900;">        <span style="color: #000066;">WindowStartupLocation</span>=<span style="color: #ff0000;">&quot;CenterScreen&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></pre></td></tr></table></div>

<ul>
<li>How does it look on Window XP ?</li>
</ul>
<p><a href="http://www.japf.fr/wp-content/uploads/2010/12/Option1_WindowsXP.png" rel="lightbox[704]"><img class="alignnone size-full wp-image-727" title="Option1_WindowsXP" src="http://www.japf.fr/wp-content/uploads/2010/12/Option1_WindowsXP.png" alt="" width="204" height="241" /></a></p>
<ul>
<li>How does it look on Windows 7 ?</li>
</ul>
<p><a href="http://www.japf.fr/wp-content/uploads/2010/11/Option1_Windows7.png" rel="lightbox[704]"><img class="alignnone size-full wp-image-715" title="Option1_Windows7" src="http://www.japf.fr/wp-content/uploads/2010/11/Option1_Windows7.png" alt="" width="214" height="241" /></a></p>
<ul>
<li>Advantages</li>
</ul>
<p>- Same solution for Windows XP and Windows 7.</p>
<p>- The client area now occupies the whole Window. We are then able to render elements at the top of the Window in order to re-create the standard window header.</p>
<p>- Very simple to use (minimal changes in your code).</p>
<ul>
<li>Drawbacks</li>
</ul>
<p>- A lot of work is needed to create the actual header: show the title of the Window, its context menu and the standard window buttons (Minimize, Restore, Close)</p>
<p>- Several problems appears when the Window gets maximized:</p>
<p>The Window occupies the whole screen and hide Windows&#8217;s taskbar. You need some interop code to change the MaxHeight/MaxWidth of the Window. You can see this post for more details: <a href="http://blogs.msdn.com/b/llobo/archive/2006/08/01/maximizing-window-_2800_with-windowstyle_3d00_none_2900_-considering-taskbar.aspx">Maximizing window (with WindowStyle=None) considering Taskbar</a></p>
<p>Even though you fix the previous problem, the border of the window is still visible when it gets maximized:</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2010/11/BorderStillVisible.png" rel="lightbox[704]"><img class="alignnone size-full wp-image-717" title="BorderStillVisible" src="http://www.japf.fr/wp-content/uploads/2010/11/BorderStillVisible.png" alt="" width="147" height="157" /></a></p>
<p><strong>Option2: use Option1 + ResizeMode =NoResize </strong><strong>in order to remove the remaining border</strong></p>
<p>You can easily fix the previous problem by registering an event handler on the  StateChanged event of the Window. In the event handler, you can set  ResizeMode to NoResize (when the Window is maximized) so that the remaining border disappear.</p>
<ul>
<li>How to do?</li>
</ul>
<p>In the constructor of the Window, you can add the following code:</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('p704code25'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p70425"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p704code25"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">StateChanged</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: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">WindowState</span> <span style="color: #008000;">==</span> WindowState<span style="color: #008000;">.</span><span style="color: #0000FF;">Maximized</span><span style="color: #008000;">&#41;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ResizeMode</span> <span style="color: #008000;">=</span> ResizeMode<span style="color: #008000;">.</span><span style="color: #0000FF;">NoResize</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ResizeMode</span> <span style="color: #008000;">=</span> ResizeMode<span style="color: #008000;">.</span><span style="color: #0000FF;">CanResize</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<ul>
<li>How does it look on Window XP ?</li>
</ul>
<p><a href="http://www.japf.fr/wp-content/uploads/2010/12/Option2_BorderGoneXP.png" rel="lightbox[704]"><img class="alignnone size-full wp-image-728" title="Option2_BorderGoneXP" src="http://www.japf.fr/wp-content/uploads/2010/12/Option2_BorderGoneXP.png" alt="" width="159" height="123" /></a></p>
<ul>
<li>How does it look on Windows 7 ?</li>
</ul>
<p><a href="http://www.japf.fr/wp-content/uploads/2010/11/Option2_BorderGone7.png" rel="lightbox[704]"><img class="alignnone size-full wp-image-720" title="Option2_BorderGone7" src="http://www.japf.fr/wp-content/uploads/2010/11/Option2_BorderGone7.png" alt="" width="147" height="155" /></a></p>
<ul>
<li>Advantages</li>
</ul>
<p>- Same solution for Windows XP and Windows 7.</p>
<p>- You fixed the previous issue easily.</p>
<ul>
<li>Drawbacks</li>
</ul>
<p>- No special drawbacks.</p>
<p><strong>Option3: use Option2 + AllowTransparency = True<br />
</strong></p>
<p>By using Option1 or Option2, you can have a much bigger client area than with a traditional Window. However, you still miss the glass effect or Aero. You might try to get this effect by adding transparency to your Window. The way to do so in WPF, is simply to set the value of the AllowTransparency property to true. Note that enabling transparency on the Window imply setting WindowStyle to None.</p>
<ul>
<li>How to do?</li>
</ul>
<p>You can use the following XAML code:</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('p704code26'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p70426"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p704code26"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Window</span> <span style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;WpfApplication_Option3.MainWindow&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">xmlns:x</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">Title</span>=<span style="color: #ff0000;">&quot;MainWindow&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;350&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;525&quot;</span> </span>
<span style="color: #009900;">        <span style="color: #000066;">WindowStyle</span>=<span style="color: #ff0000;">&quot;None&quot;</span> </span>
<span style="color: #009900;">        <span style="color: #000066;">AllowsTransparency</span>=<span style="color: #ff0000;">&quot;True&quot;</span> </span>
<span style="color: #009900;">        <span style="color: #000066;">Background</span>=<span style="color: #ff0000;">&quot;Transparent&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></pre></td></tr></table></div>

<ul>
<li>How does it look on Window XP ?</li>
</ul>
<p><a href="http://www.japf.fr/wp-content/uploads/2010/12/Option3_WindowsXP.png" rel="lightbox[704]"><img class="alignnone size-full wp-image-729" title="Option3_WindowsXP" src="http://www.japf.fr/wp-content/uploads/2010/12/Option3_WindowsXP.png" alt="" width="336" height="224" /></a></p>
<ul>
<li>How does it look on Windows 7 ?</li>
</ul>
<p><a href="http://www.japf.fr/wp-content/uploads/2010/11/Option3_Windows7.png" rel="lightbox[704]"><img class="alignnone size-full wp-image-721" title="Option3_Windows7" src="http://www.japf.fr/wp-content/uploads/2010/11/Option3_Windows7.png" alt="" width="334" height="233" /></a></p>
<ul>
<li>Advantages</li>
</ul>
<p>- Minimal changes in your existing code</p>
<ul>
<li>Drawbacks</li>
</ul>
<p>- On Windows 7, you&#8217;ll have to do some serious work in order to have the DWM Blur effect (you might need to combine some shaders effects&#8230;)</p>
<p>- On Windows 7, you lost the drop shadow of your Window</p>
<p>- You might get into performance issues by turning on this option.</p>
<p>For more details, you can check out this MSDN blog post &#8220;<a href="http://blogs.msdn.com/b/dwayneneed/archive/2008/09/09/transparent-windows-in-wpf.aspx">Transparent Windows in WPF</a>&#8220;:</p>
<blockquote><p>&#8220;The performance of WPF&#8217;s transparent windows is a topic of much  concern.  WPF is designed to render via DirectX.  We do offer a software  rendering fallback, but that is intended to provide a full-featured  fallback, not for high performance.  The layered window APIs, on the  other hand, take a GDI HDC parameter.  This causes different issues for  XP and Vista.</p>
<p><em>Experience suggests that a full-screen constantly updating layered  window on good XP machine can consume as little as 3% CPU in overhead.</em></p>
<p><em>Experience suggests that a full-screen constantly updating layered  window on good Vista machine can consume as much as 30% CPU in  overhead.&#8221;</em></p></blockquote>
<p><strong>Option4: use interop achieve the same effect as Option2 but without having to set AllowTransparency to true</strong></p>
<p>As we just saw, using adding transparency to a WPF window by using the AllowTransparency property does not look the good way to go. In this last section, we&#8217;re going to see another way using Windows native APIs.</p>
<p>The APIs we&#8217;re interested in are in the DwmApi.dll library. Here is the associated <a href="http://msdn.microsoft.com/en-us/library/ms748975.aspx">MSDN documentation</a>:</p>
<blockquote><p><em><a href="http://msdn.microsoft.com/en-us/library/aa969512.aspx">DwmExtendFrameIntoClientArea</a> s the DWM function that extends the frame into the client area. It takes two parameters; a window handle and a <a href="http://msdn.microsoft.com/en-us/library/bb773244.aspx">MARGINS</a> structure. <a href="http://msdn.microsoft.com/en-us/library/bb773244.aspx">MARGINS</a> is used to tell the DWM how much extra the frame should be extended into the client area.</em></p>
<p><em>To use the <a href="http://msdn.microsoft.com/en-us/library/aa969512.aspx">DwmExtendFrameIntoClientArea</a> function, a window handle must be obtained. In WPF, the window handle can be obtained from the <a href="http://msdn.microsoft.com/en-us/library/system.windows.interop.hwndsource.handle.aspx">Handle</a> property of an <a href="http://msdn.microsoft.com/en-us/library/system.windows.interop.hwndsource.aspx">HwndSource</a>. In the following example, the frame is extended into the client area on the <a href="http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.loaded.aspx">Loaded</a> event of the window.</em></p></blockquote>
<p>Using this API, you can achieve this kind of effect without having to use the AllowTransparency (on Vista or 7 only of course):</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2010/12/Option4_NativeApi.png" rel="lightbox[704]"><img class="alignnone size-full wp-image-734" title="Option4_NativeApi" src="http://www.japf.fr/wp-content/uploads/2010/12/Option4_NativeApi.png" alt="" width="299" height="230" /></a></p>
<p>On Windows XP, you don&#8217;t have DWM and then you&#8217;ll not be able to have the glass effect. However, you can still extend the client area of your Window using some other Windows native API.</p>
<p>The easiest way to do all this work is to use the <a href="http://code.msdn.microsoft.com/WPFShell">WPF Shell Integration Library</a>, available on CodePlex:</p>
<blockquote><p><em>The  custom chrome feature allows applications control over the outer  frame  of the window so that WPF content can be drawn over the title  bar.  This  allows applications to integrate with Aero glass to emulate  the Office  2007/2010 look and feel, or to completely replace the frame  with its own  content without having to manage all the system behaviors  that get lost  when using WindowStyle.None.</em></p></blockquote>
<p>This project also contains Windows 7 taskbar features (those features are now directly integrated in the .Net framework 4.0).</p>
<ul>
<li>How to do ?</li>
</ul>
<p>You have to setup 2 distinct styles for Windows 7 and Windows XP. You can tweak them but here is the general organization of those styles:</p>
<p>On Windows XP:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><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('p704code27'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p70427"><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
34
35
36
37
38
39
40
41
42
43
44
45
46
</pre></td><td class="code" id="p704code27"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Style</span> <span style="color: #000066;">x:Key</span>=<span style="color: #ff0000;">&quot;GradientStyle&quot;</span> <span style="color: #000066;">TargetType</span>=<span style="color: #ff0000;">&quot;{x:Type local:SelectableChromeWindow}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;shell:WindowChrome.WindowChrome&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter.Value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;shell:WindowChrome</span></span>
<span style="color: #009900;">        <span style="color: #000066;">ResizeBorderThickness</span>=<span style="color: #ff0000;">&quot;6&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">CaptionHeight</span>=<span style="color: #ff0000;">&quot;40&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">CornerRadius</span>=<span style="color: #ff0000;">&quot;6,0,6,20&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">GlassFrameThickness</span>=<span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Setter.Value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Setter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;Template&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter.Value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ControlTemplate</span> <span style="color: #000066;">TargetType</span>=<span style="color: #ff0000;">&quot;{x:Type local:SelectableChromeWindow}&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<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Border</span> <span style="color: #000066;">BorderThickness</span>=<span style="color: #ff0000;">&quot;6&quot;</span> <span style="color: #000066;">BorderBrush</span>=<span style="color: #ff0000;">&quot;#3b5998&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Border.Background<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;LinearGradientBrush</span> <span style="color: #000066;">StartPoint</span>=<span style="color: #ff0000;">&quot;0,0&quot;</span> <span style="color: #000066;">EndPoint</span>=<span style="color: #ff0000;">&quot;0,50&quot;</span> <span style="color: #000066;">MappingMode</span>=<span style="color: #ff0000;">&quot;Absolute&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;GradientStop</span> <span style="color: #000066;">Offset</span>=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">Color</span>=<span style="color: #ff0000;">&quot;#8b9dc3&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;GradientStop</span> <span style="color: #000066;">Offset</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Color</span>=<span style="color: #ff0000;">&quot;#3b5998&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/LinearGradientBrush<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Border.Background<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ContentPresenter</span> <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;6,26,6,6&quot;</span> <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;{TemplateBinding Content}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></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;Button</span> <span style="color: #000066;">shell:WindowChrome.IsHitTestVisibleInChrome</span>=<span style="color: #ff0000;">&quot;True&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Top&quot;</span> </span>
<span style="color: #009900;">                <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Left&quot;</span> </span>
<span style="color: #009900;">                <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(shell:WindowChrome.WindowChrome).ResizeBorderThickness}&quot;</span> </span>
<span style="color: #009900;">                <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;Image</span> <span style="color: #000066;">Source</span>=<span style="color: #ff0000;">&quot;{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Icon}&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;32&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;32&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Button<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Button</span> <span style="color: #000066;">shell:WindowChrome.IsHitTestVisibleInChrome</span>=<span style="color: #ff0000;">&quot;True&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Top&quot;</span> </span>
<span style="color: #009900;">                <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Right&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;{Binding Source={x:Static shell:SystemParameters2.Current}, Path=WindowCaptionButtonsLocation, Converter={StaticResource CaptionButtonMarginConverter}}&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;{Binding Source={x:Static shell:SystemParameters2.Current}, Path=WindowCaptionButtonsLocation.Width}&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;{Binding Source={x:Static shell:SystemParameters2.Current}, Path=WindowCaptionButtonsLocation.Height}&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">Command</span>=<span style="color: #ff0000;">&quot;{x:Static shell:SystemCommands.CloseWindowCommand}&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">CommandParameter</span>=<span style="color: #ff0000;">&quot;{Binding ElementName=ThisWindow}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Bold</span> <span style="color: #000066;">shell:WindowChrome.IsHitTestVisibleInChrome</span>=<span style="color: #ff0000;">&quot;False&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>XXX<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Bold<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Button<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>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ControlTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Setter.Value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Setter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Style<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>As you can see, on Windows XP, you have to create the minimize/restore/close buttons yourself.</p>
<p>On Windows 7:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><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('p704code28'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p70428"><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
</pre></td><td class="code" id="p704code28"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Style</span> <span style="color: #000066;">x:Key</span>=<span style="color: #ff0000;">&quot;GlassStyle&quot;</span> <span style="color: #000066;">TargetType</span>=<span style="color: #ff0000;">&quot;{x:Type local:SelectableChromeWindow}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;shell:WindowChrome.WindowChrome&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter.Value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;shell:WindowChrome</span> <span style="color: #000066;">GlassFrameThickness</span>=<span style="color: #ff0000;">&quot;-1&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Setter.Value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Setter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;Template&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter.Value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ControlTemplate</span> <span style="color: #000066;">TargetType</span>=<span style="color: #ff0000;">&quot;{x:Type local:SelectableChromeWindow}&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<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ContentPresenter</span></span>
<span style="color: #009900;">                <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;{Binding Source={x:Static shell:SystemParameters2.Current}, Path=WindowNonClientFrameThickness}&quot;</span> <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;{TemplateBinding Content}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Button</span> <span style="color: #000066;">shell:WindowChrome.IsHitTestVisibleInChrome</span>=<span style="color: #ff0000;">&quot;True&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Top&quot;</span> </span>
<span style="color: #009900;">                <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Left&quot;</span> </span>
<span style="color: #009900;">                <span style="color: #000066;">Margin</span>=<span style="color: #ff0000;">&quot;{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(shell:WindowChrome.WindowChrome).ResizeBorderThickness}&quot;</span> </span>
<span style="color: #009900;">                <span style="color: #000066;">Padding</span>=<span style="color: #ff0000;">&quot;8&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Image</span> <span style="color: #000066;">Source</span>=<span style="color: #ff0000;">&quot;{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Icon}&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;32&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;32&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Button<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>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ControlTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Setter.Value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Setter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Style<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<ul>
<li>How does it look on Window XP ?</li>
</ul>
<p><a href="http://www.japf.fr/wp-content/uploads/2010/12/Option4_WindowsXP.png" rel="lightbox[704]"><img class="alignnone size-full wp-image-730" title="Option4_WindowsXP" src="http://www.japf.fr/wp-content/uploads/2010/12/Option4_WindowsXP.png" alt="" width="168" height="194" /></a></p>
<ul>
<li>How does it look on Windows 7 ?</li>
</ul>
<p><a href="http://www.japf.fr/wp-content/uploads/2010/12/Option4_Windows7.png" rel="lightbox[704]"><img class="alignnone size-full wp-image-723" title="Option4_Windows7" src="http://www.japf.fr/wp-content/uploads/2010/12/Option4_Windows7.png" alt="" width="175" height="202" /></a></p>
<ul>
<li>Advantages</li>
</ul>
<p>- No performance issue since AllowTransparency is not used</p>
<p>- On Windows 7 the header looks like standard OS windows</p>
<ul>
<li>Drawbacks</li>
</ul>
<p>- You have 2 distinct solutions for Windows XP and Windows 7. On Windows XP, you have to add the Window&#8217;s buttons manually (reduce, maximize, close)</p>
<p>- You must rely on a new assembly Microsoft.Windows.Shell. However, if you don&#8217;t want to, you can import the source code in your project: the features are dispatched in two .cs files and released by Microsoft under <a href="http://www.microsoft.com/opensource/licenses.mspx">the MS-PL License</a>.</p>
<p><strong>Conclusion</strong></p>
<p>In any WPF application, the Windows frame itself matters. If you want to render graphical elements outside the client area, you must be careful. You basically have 2 options:</p>
<p>- stick to managed code only and use the AllowTransparency property. However, you must be aware of potential performance issues. Also, you&#8217;ll need to do a lot of work on your own.</p>
<p>- use Window native APIs. You&#8217;ll have to distinguish Windows OS which have DWM (Vista and 7) and XP. You&#8217;ll have optimal performance. The easiest way to do is to use <a href="http://code.msdn.microsoft.com/WPFShell">the Shell Integration library project</a>.</p>
<p><strong>Interesting Links</strong></p>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/ms748975.aspx">Extend glass frame into a WPF application</a> (MSDN)</li>
<li><a href="http://blogs.msdn.com/b/wpfsdk/archive/2008/09/08/custom-window-chrome-in-wpf.aspx">Custom Window chrome in WPF</a> (MSDN Blog)</li>
<li><a href="http://blogs.msdn.com/b/visualstudio/archive/2010/03/23/wpf-in-visual-studio-part-5-window-management.aspx">WPF in Visual Studio 2010: Window Management</a> (Visual Studio 2010 Team Blog)</li>
<li><a href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/cd81f6d2-60a9-4353-b203-285bc377a1f8/">WPF AllowTransparency problem, good on XP, bad on Vista</a> (MSDN Forums)</li>
<li><a href="http://blogs.msdn.com/b/dwayneneed/archive/2008/09/09/transparent-windows-in-wpf.aspx">Transparent Window in WPF</a> (MSDN Blog)</li>
<li><a href="http://blogs.msdn.com/b/adam_nathan/archive/2006/05/04/589686.aspx">Aero glass inside a WPF Window</a> (MSDN Blog)</li>
<li><a href="http://jmorrill.hjtcentral.com/Home/tabid/428/EntryId/403/Glass-Behavior-for-WPF.aspx">A glass effect using a WPF Behavior</a> (from <a href="http://jmorrill.hjtcentral.com/">Jeremiah Morrill</a>)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2010/12/set-up-main-window-of-your-wpf-project/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A WPF behavior to improve grouping in your ListBox</title>
		<link>http://www.japf.fr/2010/11/a-wpf-behavior-to-improve-grouping-in-your-listbox/</link>
		<comments>http://www.japf.fr/2010/11/a-wpf-behavior-to-improve-grouping-in-your-listbox/#comments</comments>
		<pubDate>Fri, 12 Nov 2010 14:43:46 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[WPF]]></category>
		<category><![CDATA[attached behavior]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=690</guid>
		<description><![CDATA[A couple of weeks ago I started prototyping with a friend a behavior in order to improve the grouping in the ListBox control. Today, I&#8217;m finally taking time to write a post about this adventure My overall goal was to have the group headers always visible and smoothly moving while the content of the control [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of weeks ago I started prototyping with a friend a behavior in order to improve the grouping in the ListBox control. Today, I&#8217;m finally taking time to write a post about this adventure <img src='http://www.japf.fr/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>My overall goal was to have the group headers always visible and smoothly moving while the content of the control was scrolled. Here is a video showing the demo application running:</p>
<p><img src="http://www.japf.fr/wp-content/plugins/flash-video-player/default_video_player.gif" /></p>
<p><a href="http://www.japf.fr/wp-content/uploads/2010/11/ListBoxAnimatedGroup.zip">Download the code (VS2010 required)</a></p>
<p>Of course I wanted to have the cleanest implementation possible, and as I think you already know, this where the WPF behavior comes to the rescue ! Let&#8217;s see how this is possible.</p>
<p><strong>Note:</strong></p>
<ul>
<li>You must be aware that enabling group in your ListBox will disable UI virtualization. This might not be a problem if the databound collections is small (&lt;100/200) but if the collection is very large, you might get into performance problem. However this limitation might be fixed in the next release of WPF (see my post about it <a href="http://www.japf.fr/2010/10/pdc10-wpf-vnext/">here</a>).</li>
<li>This solution is not 100% compatible with keyboard selection and should be improved to work properly.</li>
</ul>
<p><strong>Anatomy of a ListBox when grouping is enabled</strong></p>
<p>In order to create this behavior, I had to carefully look the Visual Tree of a ListBox when grouping is enabled. I used Snoop to do so, and I was able to get the following picture:</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2010/11/anatomy.png" rel="lightbox[690]"><img class="alignnone size-full wp-image-696" title="anatomy" src="http://www.japf.fr/wp-content/uploads/2010/11/anatomy.png" alt="" width="310" height="417" /></a></p>
<p>As you can see, for each group, we have a GroupItem control. However, we cannot direcly apply a transform to this element because it both contains the header (in a ContentPresenter) and the items in the group (in an ItemsPresenter).</p>
<p><strong>Introducing the OverlayGroupingBehavior</strong></p>
<p>The behavior actually walks in the visual tree to find out the interesting part. In our case, we want to attach a TranslateTransform to the headers of each GroupItem. Then, using simple mathematics, we can figure out the needed Y-translation to move the header at the top position of the ListBox.</p>
<p>Here are some details about the behavior:</p>
<ul>
<li>the class inherits Behavior&lt;ListBox&gt;</li>
<li>we override the OnAttached method, and in this method we register an event handler for the LayoutUpdated of the associated ListBox</li>
<li>in the LayoutUpdated handler, we update the transforms</li>
</ul>

<div class="wp_codebox_msgheader wp_codebox_hide"><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('p690code32'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p69032"><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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
</pre></td><td class="code" id="p690code32"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Linq</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Windows</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Windows.Controls</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Windows.Interactivity</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Windows.Media</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">DemoAnimatedGroup.Helpers</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> DemoAnimatedGroup<span style="color: #008000;">.</span><span style="color: #0000FF;">Behaviors</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> OverlayGroupingBehavior <span style="color: #008000;">:</span> Behavior<span style="color: #008000;">&lt;</span>ListBox<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnAttached<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;">AssociatedObject</span><span style="color: #008000;">.</span><span style="color: #0000FF;">LayoutUpdated</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> EventHandler<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnLayoutUpdated</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> OnLayoutUpdated<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            ListBoxItem topListBoxItem1<span style="color: #008000;">;</span>
            GroupItem topGroupItem1, topGroupItem2<span style="color: #008000;">;</span>
            ContentPresenter topPresenter1, topPresenter2 <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
            <span style="color: #6666cc; font-weight: bold;">double</span> topOffset1, topOffset2 <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// find the first ListBoxItem which is at the top of the control</span>
            topListBoxItem1 <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetItemAtMinimumYOffset</span><span style="color: #008000;">&lt;</span>ListBoxItem<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>topListBoxItem1 <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
                <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// get all group items order by their distance to the top</span>
            var groupItems <span style="color: #008000;">=</span> TreeHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">FindVisualChildren</span><span style="color: #008000;">&lt;</span>GroupItem<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AssociatedObject</span><span style="color: #008000;">&#41;</span>
                                       <span style="color: #008000;">.</span><span style="color: #0000FF;">OrderBy</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetYOffset</span><span style="color: #008000;">&#41;</span>
                                       <span style="color: #008000;">.</span><span style="color: #0000FF;">ToList</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// from the GroupItem, find the ContentPresenter on which we can apply the transform</span>
            topGroupItem1 <span style="color: #008000;">=</span> TreeHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">FindVisualAncestor</span><span style="color: #008000;">&lt;</span>GroupItem<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>topListBoxItem1<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            topPresenter1 <span style="color: #008000;">=</span> TreeHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">FindVisualChild</span><span style="color: #008000;">&lt;</span>ContentPresenter<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>topGroupItem1<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            topOffset1 <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetYOffset</span><span style="color: #008000;">&#40;</span>topPresenter1<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// try to find the next GroupItem and its presenter</span>
            var index <span style="color: #008000;">=</span> groupItems<span style="color: #008000;">.</span><span style="color: #0000FF;">IndexOf</span><span style="color: #008000;">&#40;</span>topGroupItem1<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>index <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span> <span style="color: #008000;">&lt;</span> groupItems<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                topGroupItem2 <span style="color: #008000;">=</span> groupItems<span style="color: #008000;">.</span><span style="color: #0000FF;">ElementAt</span><span style="color: #008000;">&#40;</span>index <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                topPresenter2 <span style="color: #008000;">=</span> TreeHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">FindVisualChild</span><span style="color: #008000;">&lt;</span>ContentPresenter<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>topGroupItem2<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                topOffset2 <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetYOffset</span><span style="color: #008000;">&#40;</span>topPresenter2<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>            
            <span style="color: #008000;">&#125;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// update transforms</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>topOffset2 <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">0</span> <span style="color: #008000;">||</span> topOffset2 <span style="color: #008000;">&gt;</span> topPresenter1<span style="color: #008000;">.</span><span style="color: #0000FF;">ActualHeight</span><span style="color: #008000;">&#41;</span>
                <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SetGroupItemOffset</span><span style="color: #008000;">&#40;</span>topPresenter1, topOffset1<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> 
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>topPresenter2 <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
                topPresenter2<span style="color: #008000;">.</span><span style="color: #0000FF;">RenderTransform</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">private</span> T GetItemAtMinimumYOffset<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">where</span> T <span style="color: #008000;">:</span> UIElement
        <span style="color: #008000;">&#123;</span>
            var minOffset <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">.</span><span style="color: #0000FF;">MaxValue</span><span style="color: #008000;">;</span>
            T topItem <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var item <span style="color: #0600FF; font-weight: bold;">in</span> TreeHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">FindVisualChildren</span><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: #008000;">.</span><span style="color: #0000FF;">AssociatedObject</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                var offset <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetYOffset</span><span style="color: #008000;">&#40;</span>item<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Abs</span><span style="color: #008000;">&#40;</span>offset<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&lt;=</span> Math<span style="color: #008000;">.</span><span style="color: #0000FF;">Abs</span><span style="color: #008000;">&#40;</span>minOffset<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    minOffset <span style="color: #008000;">=</span> offset<span style="color: #008000;">;</span>
                    topItem <span style="color: #008000;">=</span> item<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;">return</span> topItem<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> SetGroupItemOffset<span style="color: #008000;">&#40;</span>ContentPresenter groupHeader, <span style="color: #6666cc; font-weight: bold;">double</span> offset<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>groupHeader<span style="color: #008000;">.</span><span style="color: #0000FF;">RenderTransform</span> <span style="color: #0600FF; font-weight: bold;">as</span> TranslateTransform <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
                groupHeader<span style="color: #008000;">.</span><span style="color: #0000FF;">RenderTransform</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TranslateTransform<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>TranslateTransform<span style="color: #008000;">&#41;</span>groupHeader<span style="color: #008000;">.</span><span style="color: #0000FF;">RenderTransform</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Y</span> <span style="color: #008000;">-=</span> offset<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;">double</span> GetYOffset<span style="color: #008000;">&#40;</span>UIElement uiElement<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            var transform <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>MatrixTransform<span style="color: #008000;">&#41;</span> uiElement<span style="color: #008000;">.</span><span style="color: #0000FF;">TransformToVisual</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AssociatedObject</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> transform<span style="color: #008000;">.</span><span style="color: #0000FF;">Matrix</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OffsetY</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><strong>Using the behavior</strong></p>
<p>It&#8217;s more than easy to enable this functionality in any existing app. In the XAML, all you have to do is to attach the behavior to the targeted ListBox:</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('p690code33'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p69033"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p690code33"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ListBox</span> <span style="color: #000066;">ItemsSource</span>=<span style="color: #ff0000;">&quot;{Binding Cities}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;i:Interaction.Behaviors<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Behaviors:OverlayGroupingBehavior</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/i:Interaction.Behaviors<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>
        <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: #808080; font-style: italic;">&lt;!-- data template... --&gt;</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>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ListBox.GroupStyle<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;GroupStyle<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #808080; font-style: italic;">&lt;!-- group style --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/GroupStyle<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ListBox.GroupStyle<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ListBox<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>If you&#8217;re not already grouping items in your ListBox, here is how to do 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('p690code34'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p69034"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p690code34"><pre class="csharp" style="font-family:monospace;">var cv <span style="color: #008000;">=</span> CollectionViewSource<span style="color: #008000;">.</span><span style="color: #0000FF;">GetDefaultView</span><span style="color: #008000;">&#40;</span>viewmodel<span style="color: #008000;">.</span><span style="color: #0000FF;">Cities</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
cv<span style="color: #008000;">.</span><span style="color: #0000FF;">GroupDescriptions</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> PropertyGroupDescription<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Country&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p><strong>Useful resources</strong></p>
<ul>
<li>An introduction to <a href="http://marlongrech.wordpress.com/2008/11/22/icollectionview-explained/">the possibilities of the ICollectionView interface</a> by <a href="http://marlongrech.wordpress.com">Marlon Grech</a></li>
<li>An introduction to the concept of behaviors in WPF <a href="http://electricbeach.org/?p=147">here</a> and <a href="http://electricbeach.org/?p=147">here</a> by <a href="http://electricbeach.org">Christian Schormann</a></li>
</ul>
<p><a href="http://www.japf.fr/wp-content/uploads/2010/11/ListBoxAnimatedGroup.zip">Download the code (VS2010 required)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2010/11/a-wpf-behavior-to-improve-grouping-in-your-listbox/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://www.japf.fr/wp-content/uploads/2010/11/OverlayGroupingBehavior.mp4" length="607948" type="video/mp4" />
		</item>
		<item>
		<title>[PDC10] WPF vNext</title>
		<link>http://www.japf.fr/2010/10/pdc10-wpf-vnext/</link>
		<comments>http://www.japf.fr/2010/10/pdc10-wpf-vnext/#comments</comments>
		<pubDate>Fri, 29 Oct 2010 05:41:55 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[Events]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[future]]></category>
		<category><![CDATA[pdc]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=684</guid>
		<description><![CDATA[Day 1 of PDC 2010 is now over and I had the chance to watch the &#8220;WPF vNext&#8221; session by Rob Relyea. You can follow this link if you want to watch the video on-demand. History Rob starts with an history of Microsoft products from Windows 95 and IE 1.0&#8230; The Blend team was created [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.japf.fr/wp-content/uploads/2010/10/pdc10.png" rel="lightbox[684]"><img class="alignnone size-full wp-image-685" title="pdc10" src="http://www.japf.fr/wp-content/uploads/2010/10/pdc10.png" alt="" width="219" height="65" /></a></p>
<p>Day 1 of PDC 2010 is now over and I had the chance to watch the &#8220;WPF vNext&#8221; session by <a href="http://blogs.windowsclient.net/rob_relyea/">Rob Relyea</a>. You can follow <a href="http://player.microsoftpdc.com/Session/c1533143-2bab-43e0-a3ce-114dcdd1143e">this link</a> if you want to watch the video on-demand.</p>
<p><strong>History</strong></p>
<p>Rob starts with an history of Microsoft products from Windows 95 and IE 1.0&#8230;</p>
<p>The Blend team was created to build tools that would meet developers and designers needs. They worked closely with the Avalon team (codename for WPF) to make sure the platform was adequate and toolable.</p>
<p>Then the Cider team started their work in order to have a good integration in Visual Studio.</p>
<p>At this point, .Net 3.0 hasn&#8217;t shipped yet (it was in 2006), but Microsoft already has the vision of having XAML-based technology in the browser (with what would become Silverlight) and on the phone.</p>
<p><strong>Silverlight &amp; WPF</strong></p>
<p><em>Silverlight</em></p>
<ul>
<li> focuses on premium media experiences and business applications</li>
<li>suitable for most other types of application</li>
</ul>
<p><em>WPF</em></p>
<ul>
<li>complex ISV (Independent Software Vendors) applications</li>
<li>key scenarios includes DX and Hwnd interop</li>
</ul>
<p><em>Convergence</em></p>
<ul>
<li>bringing key features of WPF into Silverlight</li>
<li>WPF will support Silverlight hosting in the next version</li>
</ul>
<p><strong>ISV needs</strong></p>
<ul>
<li>great Windows applications</li>
<li>modern UI</li>
<li>seamless integration</li>
<li>rich content</li>
</ul>
<p><strong>Microsoft adoption of WPF</strong></p>
<ul>
<li>Visual Studio 2010</li>
<li>Expression Studio</li>
<li>Web Matrix</li>
<li>Powershell ISE</li>
<li><em>more to announce from Microsoft, but not during this PDC&#8230;</em></li>
</ul>
<p><strong>WPF vNext</strong></p>
<ul>
<li>integration of the <strong>Ribbon </strong>in WPF</li>
<li>improved <strong>collections handling in background thread</strong> (simplify the problem related to the UI thread)</li>
<li>improved <strong>UI virtualizing and grouping</strong> (right now virtualization is disabled as soon as you group data in an ItemsControl)</li>
<li>seamless integration: <strong>new SilverlightHost control</strong> (so we&#8217;ll be able to have DeepZoom in WPF !)</li>
<li><strong>hwnd-based will no longer have airspace problem</strong> (for example, a Winforms control is always rendered on top of all other WPF controls).</li>
</ul>
<p>As a conclusion, it&#8217;s good to see Microsoft finally giving some information about the future of WPF. The most important part I guess is to understand that Microsoft is still doing improvements on WPF and positioning WPF as the key technology for building apps with modern UI on Windows. We might have more and more applications switching to Silverlight but I&#8217;m sure they are still a lot of need for a technology like WPF which can use the whole power of the Windows OS (wheter it&#8217;s interop with native code, DirectX or other technologies).</p>
<p>For more information, you can also check-out a blog post from <a href="http://10rem.net/blog">Pete Brown</a> entitled &#8220;<a href="http://10rem.net/blog/2010/10/28/the-present-and-future-of-wpf">The Present and Future of WPF</a>&#8220;.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2010/10/pdc10-wpf-vnext/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>WPF internals part 3 : how Z-Index works ?</title>
		<link>http://www.japf.fr/2010/10/wpf-internals-part-3-how-z-index-works/</link>
		<comments>http://www.japf.fr/2010/10/wpf-internals-part-3-how-z-index-works/#comments</comments>
		<pubDate>Thu, 21 Oct 2010 17:33:24 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[WPF]]></category>
		<category><![CDATA[panel]]></category>
		<category><![CDATA[zindex]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=664</guid>
		<description><![CDATA[Almost 1 year ago, I started a series of blog posts entitled &#8220;WPF Internals&#8221;. I though I&#8217;d have more time to write entries on this subject but I wrote only 2 subjects: WPF internals part 1 : what are the core WPF classes ? WPF internals part 2 : how the WPF controls are organized [...]]]></description>
			<content:encoded><![CDATA[<p>Almost 1 year ago, I started a series of blog posts entitled &#8220;WPF Internals&#8221;. I though I&#8217;d have more time to write entries on this subject but I wrote only 2 subjects:</p>
<ul>
<li>WPF internals part 1 : <a href="http://www.japf.fr/2009/10/wpf-internals-part-1-what-are-the-core-wpf-classes/">what are the core WPF classes ?</a></li>
<li>WPF internals part 2 : <a href="http://www.japf.fr/2010/01/wpf-internals-how-the-wpf-controls-are-organised/">how the WPF controls are organized ?</a></li>
</ul>
<p>Today, I&#8217;m ready to share with you part 3: how the Z-Index functionality works ? As for the other article of this series of blog post, the information I&#8217;m sharing here is based on my personal understanding of how stuff works. It might be wrong on some points ! </p>
<p>The Z-Index is a property which can be set in order to control the order of appearance of your control:</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2010/10/z-order.png" rel="lightbox[664]"><img src="http://www.japf.fr/wp-content/uploads/2010/10/z-order.png" alt="" title="z-order" width="225" height="120" class="alignnone size-full wp-image-677" /></a></p>
<p>In WPF it&#8217;s the <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.panel.aspx">Panel</a> type which defines an attached property called <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.panel.zindex.aspx">Zindex</a>:</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('p664code41'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p66441"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p664code41"><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: #0600FF; font-weight: bold;">readonly</span> DependencyProperty ZIndexProperty <span style="color: #008000;">=</span> DependencyProperty<span style="color: #008000;">.</span><span style="color: #0000FF;">RegisterAttached</span><span style="color: #008000;">&#40;</span>
	<span style="color: #666666;">&quot;ZIndex&quot;</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;">typeof</span><span style="color: #008000;">&#40;</span>Panel<span style="color: #008000;">&#41;</span>, 
	<span style="color: #008000;">new</span> FrameworkPropertyMetadata<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #008000;">new</span> PropertyChangedCallback<span style="color: #008000;">&#40;</span>Panel<span style="color: #008000;">.</span><span style="color: #0000FF;">OnZIndexPropertyChanged</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>which register a PropertyChangedCallback to be notified whenever its value changes. This method finally calls another method:</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('p664code42'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p66442"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p664code42"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">internal</span> <span style="color: #6666cc; font-weight: bold;">void</span> InvalidateZState<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;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsZStateDirty</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span>_uiElementCollection <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">InvalidateZOrder</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: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsZStateDirty</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, the value of the IsZStateDirty property is set to true. We&#8217;ll soon see when this value is used. The InvalidateZOrder() is actually found in the <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.visual.aspx">Visual</a> class. Here is a brief reminder of the core WPF types:</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2010/10/WhereIsVisual.png" rel="lightbox[664]"><img class="alignnone size-full wp-image-666" title="WhereIsVisual" src="http://www.japf.fr/wp-content/uploads/2010/10/WhereIsVisual.png" alt="" width="156" height="545" /></a></p>
<p>So, in the <a href="http://msdn.microsoft.com/en-us/library/system.windows.media.visual.aspx">Visual</a> type we have the InvalidateZOrder() method:</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('p664code43'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p66443"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p664code43"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>FriendAccessAllowed<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">internal</span> <span style="color: #6666cc; font-weight: bold;">void</span> InvalidateZOrder<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;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">VisualChildrenCount</span> <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</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;">SetFlags</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span>, VisualFlags<span style="color: #008000;">.</span><span style="color: #0000FF;">NodeRequiresNewRealization</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008080; font-style: italic;">/* … */</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Which, as you can see, update the value of an enumeration (VisualFlags). Then the chain of method call stops here. The next interesting steps is when the <a href="http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.getvisualchild.aspx">GetVisualChild</a> method (from the <a href="http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.aspx">FrameworkElement</a> type) gets called in the <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.panel.aspx">Panel</a> type:</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('p664code44'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p66444"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p664code44"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsZStateDirty</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;">RecomputeZState</span><span style="color: #008000;">&#40;</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 RecomputeZState is a private method of the <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.panel.aspx">Panel</a> class. The goal of this method is to update an array of int which is used as a lookup-table in order to convert the visual elements from their logical positions to their visual positions. At the end of this method, which is by the way highly-optimized with stuff like this</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('p664code45'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p66445"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p664code45"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">int</span> z <span style="color: #008000;">=</span> _elements<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span>
<span style="color: #008000;">?</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>z <span style="color: #008000;">=</span> _elements<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&#40;</span>ZIndexProperty<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">:</span> zIndexDefaultValue<span style="color: #008000;">;</span>
&nbsp;
stableKeyValues<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>Int64<span style="color: #008000;">&#41;</span>z <span style="color: #008000;">&lt;&lt;</span> <span style="color: #FF0000;">32</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> i<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
lutRequired <span style="color: #008000;">|=</span> z <span style="color: #008000;">&lt;</span> prevZ<span style="color: #008000;">;</span>
prevZ <span style="color: #008000;">=</span> z<span style="color: #008000;">;</span>
&nbsp;
isDiverse <span style="color: #008000;">|=</span> <span style="color: #008000;">&#40;</span>z <span style="color: #008000;">!=</span> consonant<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>the IZStateDirty value is set to false, and the zLut (z-order look up table, an int[]) is up-to-date. The the <a href="http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.getvisualchild.aspx">GetVisualChild</a> method simply use the lookup table to convert logical position to visual position</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('p664code46'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p66446"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p664code46"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">int</span> num <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span>_zLut <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</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>_zLut<span style="color: #008000;">&#91;</span>index<span style="color: #008000;">&#93;</span> <span style="color: #008000;">:</span> index<span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span>_uiElementCollection<span style="color: #008000;">&#91;</span>num<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p><strong>Summary:</strong></p>
<ul>
<li>The Z-Index functionality of WPF is implemented using an attached property</li>
<li>This attached property is defined in the Panel type</li>
<li>When the value of the attached property changes, a flag is set at the Visual level and at the Panel level</li>
<li>When the GetVisualChild methods gets called on the Panel, the dirty status of the Z-Index is check</li>
<li>If necessary, a lookup-table is computed to convert logical position (in the Children collection) from visual position</li>
<li>Changing the ZIndex property of a child object does not change its position within the collection. The ordering within the collection remains the same</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2010/10/wpf-internals-part-3-how-z-index-works/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<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('p630code48'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p63048"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p630code48"><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>
	</channel>
</rss>

