<?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>On developing Pochet.NET</title>
	<atom:link href="http://www.pochet.net/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pochet.net/blog</link>
	<description>Thoughts on developing a Silverlight LOB application by Emiel Jongerius</description>
	<lastBuildDate>Mon, 14 Jan 2013 20:17:00 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.1</generator>
		<item>
		<title>Memory Leak in PanelDragDropTarget</title>
		<link>http://www.pochet.net/blog/2010/10/16/memory-leak-in-paneldragdroptarget/</link>
		<comments>http://www.pochet.net/blog/2010/10/16/memory-leak-in-paneldragdroptarget/#comments</comments>
		<pubDate>Sat, 16 Oct 2010 14:10:07 +0000</pubDate>
		<dc:creator>Emiel</dc:creator>
				<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.pochet.net/blog/?p=271</guid>
		<description><![CDATA[While implementing drag-and-drop grouping for the Silverlight DataGrid I noticed that the PanelDragDropTarget contains a memory leak: Whenever you add a user control containing a PanelDragDropTarget to a page and then remove the user control from this page, the user control is not properly released. When you replace the PanelDragDropTarget with another control, e.g. a [...]]]></description>
			<content:encoded><![CDATA[<p>While implementing drag-and-drop grouping for the Silverlight <code>DataGrid</code> I noticed that the <code>PanelDragDropTarget</code> contains a memory leak:</p>
<p>Whenever you add a user control containing a <code>PanelDragDropTarget</code> to a page and then remove the user control from this page, the user control is not properly released.</p>
<p>When you replace the <code>PanelDragDropTarget</code> with another control, e.g. a <code>ListBox</code>, the user control is released.</p>
<p>The root cause of this problem is probably the fact that the base class <code>DropDragTarget</code> attaches some events to the <code>Canvas</code> of the <code>Application.Current</code> which are not removed.</p>
<p>I have created a sample project that demonstrates the problem using the <a href="http://www.pochet.net/blog/2010/06/03/determining-leaked-objects/">MemoryLeakDetector</a> which can be downloaded <a href='http://www.pochet.net/blog/wp-content/uploads/2010/10/MemoryLeakDragDropTarget.zip'>here</a>.</p>
<p>This issue has been reported to the Silverlight Toolkit team <a href="http://silverlight.codeplex.com/workitem/7356">here</a> where you can vote for this issue to be solved.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pochet.net/blog/2010/10/16/memory-leak-in-paneldragdroptarget/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Datagrid: support for grouping on columns use IValueConverters to display their value and conditonal rowstyling</title>
		<link>http://www.pochet.net/blog/2010/09/06/datagrid-support-for-grouping-on-columns-that-use-ivalueconverters-to-display-their-value-and-conditonal-rowstyling/</link>
		<comments>http://www.pochet.net/blog/2010/09/06/datagrid-support-for-grouping-on-columns-that-use-ivalueconverters-to-display-their-value-and-conditonal-rowstyling/#comments</comments>
		<pubDate>Mon, 06 Sep 2010 13:55:35 +0000</pubDate>
		<dc:creator>Emiel</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[DataGrid]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.pochet.net/blog/?p=229</guid>
		<description><![CDATA[Introduction Some further enhancements have been made to the DataGrid wrapper that has been the subject of this previous post: Support for grouping on columns that use IValueConverter to display their value Conditional row styling This post is a code drop for these enhancements. Conditional row styling The following code gives a sample on how [...]]]></description>
			<content:encoded><![CDATA[<p style="display:block"><a href="http://www.pochet.net/blog/wp-content/uploads/2010/09/DataGridUpdate.jpg"><img src="http://www.pochet.net/blog/wp-content/uploads/2010/09/DataGridUpdate-300x218.jpg" alt="DataGrid update" title="DataGridUpdate" class="aligncenter size-full wp-image-209" border="0" /></a></p>
<h3 style="clear:both;display:block">Introduction</h3>
<p>
Some further enhancements have been made to the <code>DataGrid</code> wrapper that has been the subject of this <a href="http://www.pochet.net/blog/2010/08/29/update-postings-on-datagrid/">previous post</a>:
</p>
<ul>
<li>Support for grouping on columns that use <code>IValueConverter</code> to display their value</li>
<li>Conditional row styling</li>
</ul>
<p>
This post is a code drop for these enhancements.
</p>
<h3>Conditional row styling</h3>
<p>
The following code gives a sample on how to use conditional row styling:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> MainPage<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;
    theDataGrid<span style="color: #008000;">.</span><span style="color: #0000FF;">ItemsSource</span> <span style="color: #008000;">=</span> PersonFactory<span style="color: #008000;">.</span><span style="color: #0000FF;">GetAllPersons</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    theDataGrid<span style="color: #008000;">.</span><span style="color: #0000FF;">RowStyles</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> MyDataGridRowConditionalStyle<span style="color: #008000;">&#40;</span>
        <span style="color: #008000;">&#40;</span>o<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;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>Person<span style="color: #008000;">&#41;</span>o<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Contains</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;A&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>,
        <span style="color: #008000;">new</span> MyDataGridRowStyle<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            Background <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SolidColorBrush<span style="color: #008000;">&#40;</span>Colors<span style="color: #008000;">.</span><span style="color: #0000FF;">Yellow</span><span style="color: #008000;">&#41;</span>,
            ToolTip <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Name contains an 'A'&quot;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    theDataGrid<span style="color: #008000;">.</span><span style="color: #0000FF;">RowStyles</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> MyDataGridRowConditionalStyle<span style="color: #008000;">&#40;</span>
        <span style="color: #008000;">&#40;</span>o<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;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>Person<span style="color: #008000;">&#41;</span>o<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Contains</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;C&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>,
        <span style="color: #008000;">new</span> MyDataGridRowStyle<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            Foreground <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SolidColorBrush<span style="color: #008000;">&#40;</span>Colors<span style="color: #008000;">.</span><span style="color: #0000FF;">Red</span><span style="color: #008000;">&#41;</span>,
            ToolTip <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Name contains a 'C'&quot;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    theDataGrid<span style="color: #008000;">.</span><span style="color: #0000FF;">RowStyles</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> MyDataGridRowConditionalStyle<span style="color: #008000;">&#40;</span>
        <span style="color: #008000;">&#40;</span>o<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;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>Person<span style="color: #008000;">&#41;</span>o<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Contains</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;N&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>,
        <span style="color: #008000;">new</span> MyDataGridRowStyle<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            FontSize <span style="color: #008000;">=</span> <span style="color: #FF0000;">14</span>,
            FontWeight <span style="color: #008000;">=</span> FontWeights<span style="color: #008000;">.</span><span style="color: #0000FF;">ExtraBold</span>,
            ToolTip <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Name contains an 'N'&quot;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
The result can be seen in the picture above
</p>
<h3>Download</h3>
<p>
The sources can be downloaded <a href='http://www.pochet.net/blog/wp-content/uploads/2010/09/DataGridTest-Update.zip'>here</a>.
</p>
<h3>Additional remarks</h3>
<p>
A solution for conditional cell styling can be found <a href="http://weblogs.asp.net/joewrobel/archive/2009/01/25/conditional-formatting-in-the-silverlight-datagrid.aspx">here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.pochet.net/blog/2010/09/06/datagrid-support-for-grouping-on-columns-that-use-ivalueconverters-to-display-their-value-and-conditonal-rowstyling/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Update postings on Datagrid</title>
		<link>http://www.pochet.net/blog/2010/08/29/update-postings-on-datagrid/</link>
		<comments>http://www.pochet.net/blog/2010/08/29/update-postings-on-datagrid/#comments</comments>
		<pubDate>Sun, 29 Aug 2010 20:51:06 +0000</pubDate>
		<dc:creator>Emiel</dc:creator>
				<category><![CDATA[DataGrid]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.pochet.net/blog/?p=217</guid>
		<description><![CDATA[Recently I wrote two posts on the Silverlight Datagrid: Synchronizing group row headers with column headers in Silverlight DataGrid Drag and Drop grouping in Datagrid Andy noticed that in a comment that the synchronizing of the group row headers with the column headers did not work correctly when the group headers were scrolled out of [...]]]></description>
			<content:encoded><![CDATA[<p>
Recently I wrote two posts on the Silverlight Datagrid:
</p>
<ul>
<li><a href="http://www.pochet.net/blog/2010/08/20/synchronizing-group-row-headers-with-column-headers-in-silverlight-datagrid/">Synchronizing group row headers with column headers in Silverlight DataGrid</a></li>
<li><a href="http://www.pochet.net/blog/2010/08/25/drag-and-drop-grouping-in-datagrid/">Drag and Drop grouping in Datagrid</a></li>
</ul>
<p>
Andy noticed that in <a href="http://www.pochet.net/blog/2010/08/20/synchronizing-group-row-headers-with-column-headers-in-silverlight-datagrid/#comment-186">a comment</a> that the synchronizing of the group row headers with the column headers did not work correctly when the group headers were scrolled out of view and then back into view again.
</p>
<p>
After investigating the reason of this behavior I have decided to take another approach and to re-implement this functionality. Some additional testing of this new implementation reveals that the issue reported by Andy has been solved. The only quirk now is that whenever the short description of the <code>Display</code> attribute is set on a property of the data and the <code>Header</code> of the column is explicitly set, the column will show the header and the group row the short descriptions.
</p>
<p>
This new approach to synchronizing the group headers with the column headers is now also used in the implementation of the Drag and Drop grouping. As it turned out, this synchronizing approach simplified the implementation of Drag and drop grouping, because the cloning of the <code>CollectionView</code> was not needed anymore. Also after applying the grouping the selected elements would still be selected.
</p>
<p>
Finally some additional changes have been made:
</p>
<ul>
<li>A dialog for selecting columns has been added</li>
<li>Reordering of the groups is supported</li>
<li>The column by which is grouped is automatically hidden</li>
<li>The components are wrapped in a reusable DLL</li>
</ul>
<p>
The modified code can be downloaded <a href='http://www.pochet.net/blog/wp-content/uploads/2010/08/DataGridTest-2.zip'>here</a>.
</p>
<h3>Update</h3>
<p>The code from this post has been updated. A newer version can be downloaded from <a href="http://www.pochet.net/blog/2010/09/06/datagrid-support-for-grouping-on-columns-that-use-ivalueconverters-to-display-their-value-and-conditonal-rowstyling/">here</a><br/><br/></p>
]]></content:encoded>
			<wfw:commentRss>http://www.pochet.net/blog/2010/08/29/update-postings-on-datagrid/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Drag and Drop grouping in Datagrid</title>
		<link>http://www.pochet.net/blog/2010/08/25/drag-and-drop-grouping-in-datagrid/</link>
		<comments>http://www.pochet.net/blog/2010/08/25/drag-and-drop-grouping-in-datagrid/#comments</comments>
		<pubDate>Wed, 25 Aug 2010 14:08:31 +0000</pubDate>
		<dc:creator>Emiel</dc:creator>
				<category><![CDATA[DataGrid]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.pochet.net/blog/?p=207</guid>
		<description><![CDATA[Update The code for the functionality in this post has been updated in Update postings on Datagrid Introduction The DataGrid of the Silverlight Toolkit supports grouping but does not have the ability to drag column headers to the area above the grid to group the data, unlike some other 3rd party datagrids. This blog presents [...]]]></description>
			<content:encoded><![CDATA[<h2 style='color:red;font-weight:bold;'>Update</h2>
<p style='color:red;font-weight:bold;'>
The code for the functionality in this post has been updated in <a href="http://www.pochet.net/blog/2010/08/29/update-postings-on-datagrid/" style='color:red;font-weight:bold;text-decoration:underline;'>Update postings on Datagrid</a>
</p>
<p><a href="http://www.pochet.net/blog/wp-content/uploads/2010/08/pic2.jpg"><img src="http://www.pochet.net/blog/wp-content/uploads/2010/08/pic2.jpg" alt="" title="Groupable DataGrid" width="441" height="374" class="aligncenter size-full wp-image-209" border="0"/></a></p>
<h3><a name="introduction">Introduction</a></h3>
<p> The <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid%28VS.95%29.aspx">DataGrid</a> of the <a href="http://silverlight.codeplex.com/">Silverlight Toolkit</a> supports grouping but does not have the ability to drag column headers to the area above the grid to group the data, unlike some other 3<sup>rd</sup> party datagrids. </p>
<p> This blog presents a solution to implement this functionality. The solution is based upon the <a href="http://leeontech.wordpress.com/2010/04/25/drag-and-drop-grouping-in-datagrid/">solution</a> which can be found on the blog <a href="http://leeontech.wordpress.com/">Lee&#8217;s corner</a>.  </p>
<h3><a name="contents">Contents</a></h3>
<p>
<ul>
<li><a href="#introduction">Introduction</a></li>
<li><a href="#shortcomings">Shortcomings of Lee&#8217;s solution</a></li>
<li><a href="#make_reusable">Make reusable</a></li>
<li><a href="#replace_template">Alternative for replacing the template</a></li>
<li><a href="#prevent_reordering">Preventing column order changes when grouping</a></li>
<li><a href="#match_headers">Match column headers with group headers</a>
<ul>
<li><a href="#group_rows">Match with group rows within the <code>MyDataGrid</code></a></li>
<li><a href="#group_panel">Match with <code>MyDataGridGroupHeaderPanel</code></a></li>
</ul>
</li>
<li><a href="#download">Download</a></li>
</ul>
<h3><a name="shortcomings">Shortcomings of Lee&#8217;s solution</a></h3>
<p> The <a href="http://leeontech.wordpress.com/2010/04/25/drag-and-drop-grouping-in-datagrid/">solution</a> presented on <a href="http://leeontech.wordpress.com/">Lee&#8217;s corner</a> has the following shortcomings: </p>
<ul>
<li>The solution is not a reusable component</li>
<li>The solution is not themable because the <code>Template</code> is replaced</li>
<li>When grouping on column, the order of the columns is also changed as <a href="http://leeontech.wordpress.com/2010/04/25/drag-and-drop-grouping-in-datagrid/#comment-997">noticed</a> by Robbie Symborski</li>
<li>Headers on columns do not match group headers</li>
</ul>
<p> The following paragraphs outline the approaches taken to resolve these shortcomings. </p>
<h3><a name="make_reusable">Make reusable</a></h3>
<p> Because integrating the group header panel within the datagrid without changing the template seemed quite complex, the solution consists of the following two components:
<ul>
<li><code>MyDataGrid</code>: inherited from <code>DataGrid</code></li>
<li><code>MyDataGridGroupHeaderPanel</code>: group panel that can be linked to the <code>MyDataGrid</code> by using the attribute <code>DataGrid</code></li>
</ul>
<p> Using these components a groupable grid can be made as follows: </p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid.RowDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RowDefinition</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;40&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RowDefinition</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;*&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.RowDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;app:MyDataGridGroupHeaderPanel</span> <span style="color: #000066;">DataGrid</span>=<span style="color: #ff0000;">&quot;{Binding ElementName=datagrid1}&quot;</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;0&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;app:MyDataGrid</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;datagrid1&quot;</span> <span style="color: #000066;">Grid.Row</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;/app:MyDataGrid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>
The underlying <code>DataGrid</code> relies on a <code>ICollectionView</code> to perform the grouping and sorting. Unfortunately, there is no way to get hold of this data from within the <code>DataGrid</code> itself. The solution is to use an <code>ICollectionView</code> for the <code>ItemsSource</code> and keep a reference to this <code>ICollectionView</code>. In order to be able to still support ordinary <code>IEnumerable</code> item sources the <code>ItemsSource</code> property of the underlying <code>DataGrid</code> is hidden by a new definition as follows:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> ICollectionView CollectionView <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">private</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #008000;">new</span> IEnumerable ItemsSource
<span style="color: #008000;">&#123;</span>
    get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> GetValue<span style="color: #008000;">&#40;</span>ItemsSourceProperty<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">as</span> IEnumerable<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    set <span style="color: #008000;">&#123;</span> SetValue<span style="color: #008000;">&#40;</span>ItemsSourceProperty, value<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #008000;">new</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> DependencyProperty ItemsSourceProperty <span style="color: #008000;">=</span>
    DependencyProperty<span style="color: #008000;">.</span><span style="color: #0000FF;">Register</span><span style="color: #008000;">&#40;</span>
    <span style="color: #666666;">&quot;ItemsSource&quot;</span>,
    <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>IEnumerable<span style="color: #008000;">&#41;</span>,
    <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>MyDataGrid<span style="color: #008000;">&#41;</span>,
    <span style="color: #008000;">new</span> PropertyMetadata<span style="color: #008000;">&#40;</span>OnItemsSourcePropertyChanged<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnItemsSourcePropertyChanged<span style="color: #008000;">&#40;</span>DependencyObject d, DependencyPropertyChangedEventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    MyDataGrid grid <span style="color: #008000;">=</span> d <span style="color: #0600FF; font-weight: bold;">as</span> MyDataGrid<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>grid <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>
        <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    grid<span style="color: #008000;">.</span><span style="color: #0000FF;">SetItemsSource</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>IEnumerable<span style="color: #008000;">&#41;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">NewValue</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetItemsSource<span style="color: #008000;">&#40;</span>IEnumerable itemsSource<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    ICollectionView newValue <span style="color: #008000;">=</span> CreateCollectionView<span style="color: #008000;">&#40;</span>itemsSource<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    CollectionView <span style="color: #008000;">=</span> newValue<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ItemsSource</span> <span style="color: #008000;">=</span> newValue<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> ICollectionView CreateCollectionView<span style="color: #008000;">&#40;</span>IEnumerable newItemsSource<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>newItemsSource <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>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    ICollectionViewFactory collectionViewFactory <span style="color: #008000;">=</span> newItemsSource <span style="color: #0600FF; font-weight: bold;">as</span> ICollectionViewFactory<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>collectionViewFactory <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>
        <span style="color: #0600FF; font-weight: bold;">return</span> collectionViewFactory<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateView</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;
    ICollectionView collectionView <span style="color: #008000;">=</span> newItemsSource <span style="color: #0600FF; font-weight: bold;">as</span> ICollectionView<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>collectionView <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>
        <span style="color: #0600FF; font-weight: bold;">return</span> collectionView<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> PagedCollectionView<span style="color: #008000;">&#40;</span>newItemsSource<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<h3><a name="replace_template">Alternative for replacing the template</a></h3>
<p>
As stated in <a href="http://leeontech.wordpress.com/2010/04/25/drag-and-drop-grouping-in-datagrid/">here</a> the only reason to replace the <code>Template</code> is to wrap the <code>DataGridColumnHeadersPresenter</code> in a <code>PanelDragDropTarget</code> to allow the <code>DataGridColumnHeaders</code> to be draggable. This can also be done by overriding the <code>OnApplyTemplate()</code> of <code>MyDataGrid</code> like this:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">PanelDragDropTarget _dropTarget<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnApplyTemplate<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;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnApplyTemplate</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    DataGridColumnHeadersPresenter columnHeadersPresenter <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetTemplateChild</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;ColumnHeadersPresenter&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">as</span> DataGridColumnHeadersPresenter<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>columnHeadersPresenter <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>
        <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    Grid columnHeadersPresenterParent <span style="color: #008000;">=</span> columnHeadersPresenter<span style="color: #008000;">.</span><span style="color: #0000FF;">Parent</span> <span style="color: #0600FF; font-weight: bold;">as</span> Grid<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>columnHeadersPresenterParent <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>
        <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #6666cc; font-weight: bold;">int</span> indexOfPresenter <span style="color: #008000;">=</span> columnHeadersPresenterParent<span style="color: #008000;">.</span><span style="color: #0000FF;">Children</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IndexOf</span><span style="color: #008000;">&#40;</span>columnHeadersPresenterParent<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>indexOfPresenter <span style="color: #008000;">==</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        indexOfPresenter <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    _dropTarget <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PanelDragDropTarget<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> 
    <span style="color: #008000;">&#123;</span> 
        HorizontalContentAlignment <span style="color: #008000;">=</span> HorizontalAlignment<span style="color: #008000;">.</span><span style="color: #0000FF;">Stretch</span>, 
        VerticalContentAlignment <span style="color: #008000;">=</span> VerticalAlignment<span style="color: #008000;">.</span><span style="color: #0000FF;">Stretch</span>, 
        AllowedSourceEffects <span style="color: #008000;">=</span> DragDropEffects<span style="color: #008000;">.</span><span style="color: #0000FF;">Copy</span> 
    <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
    _dropTarget<span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span>Grid<span style="color: #008000;">.</span><span style="color: #0000FF;">ColumnProperty</span>, <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    columnHeadersPresenterParent<span style="color: #008000;">.</span><span style="color: #0000FF;">Children</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Remove</span><span style="color: #008000;">&#40;</span>columnHeadersPresenter<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    columnHeadersPresenter<span style="color: #008000;">.</span><span style="color: #0000FF;">ClearValue</span><span style="color: #008000;">&#40;</span>Grid<span style="color: #008000;">.</span><span style="color: #0000FF;">ColumnProperty</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    columnHeadersPresenter<span style="color: #008000;">.</span><span style="color: #0000FF;">ClearValue</span><span style="color: #008000;">&#40;</span>Grid<span style="color: #008000;">.</span><span style="color: #0000FF;">RowProperty</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    _dropTarget<span style="color: #008000;">.</span><span style="color: #0000FF;">Content</span> <span style="color: #008000;">=</span> columnHeadersPresenter<span style="color: #008000;">;</span>
    columnHeadersPresenterParent<span style="color: #008000;">.</span><span style="color: #0000FF;">Children</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Insert</span><span style="color: #008000;">&#40;</span>indexOfPresenter, _dropTarget<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<h3><a name="prevent_reordering">Preventing column order changes when grouping</a></h3>
<p>
The problem in preventing column order changes when regrouping is how to determine when a column header movement is a column order change or a group change. The approach taken in this solution is a pragmatic one: when the control key is pressed during the mousedown on a column header the user is starting a group operation, otherwise the user is starting a column reorder operation. This can be achieved by addint the folowing to code to <code>MyDataGrid</code>:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">virtual</span> <span style="color: #6666cc; font-weight: bold;">bool</span> AllowGrouping
<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: #008000;">&#40;</span>Keyboard<span style="color: #008000;">.</span><span style="color: #0000FF;">Modifiers</span> <span style="color: #008000;">&amp;</span> ModifierKeys<span style="color: #008000;">.</span><span style="color: #0000FF;">Control</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">!=</span> ModifierKeys<span style="color: #008000;">.</span><span style="color: #0000FF;">None</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;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnMouseLeftButtonDown<span style="color: #008000;">&#40;</span>MouseButtonEventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    CanUserReorderColumns <span style="color: #008000;">=</span> <span style="color: #008000;">!</span>AllowGrouping<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnMouseLeftButtonDown</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
and to add the following line to the <code>OnApplyTemplate()</code> override:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">_dropTarget<span style="color: #008000;">.</span><span style="color: #0000FF;">ItemDragStarting</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> e<span style="color: #008000;">.</span><span style="color: #0000FF;">Cancel</span> <span style="color: #008000;">=</span> <span style="color: #008000;">!</span>AllowGrouping<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">Cancel</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> e<span style="color: #008000;">.</span><span style="color: #0000FF;">Handled</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> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span></pre></div></div>

<h3><a name="match_headers">Match column headers with group headers</a></h3>
<p>
The column headers must match with both:</p>
<ul>
<li>the group headers on the group rows within the <code>MyDataGrid</code></li>
<li>the group buttons on <code>MyDataGridGroupHeaderPanel</code></li>
</ul>
<p>These are separate problems which will be dealt with in the following paragraphs.
</p>
<h4><a name="group_rows">Match with group rows within the <code>MyDataGrid</code></a></h4>
<p>
Following <a href="http://www.pochet.net/blog/2010/08/20/synchronizing-group-row-headers-with-column-headers-in-silverlight-datagrid/">this article</a> the matching with group rows within the <code>MyDataGrid</code> can be achieved by adding the following code to <code>MyDataGrid</code>:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><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> OnLoadingRowGroup<span style="color: #008000;">&#40;</span>DataGridRowGroupHeaderEventArgs e<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;">OnLoadingRowGroup</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #6666cc; font-weight: bold;">string</span> fieldName <span style="color: #008000;">=</span> e<span style="color: #008000;">.</span><span style="color: #0000FF;">RowGroupHeader</span><span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyName</span><span style="color: #008000;">;</span>
&nbsp;
    e<span style="color: #008000;">.</span><span style="color: #0000FF;">RowGroupHeader</span><span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyName</span> <span style="color: #008000;">=</span> GetColumnCaptionFromBindingPath<span style="color: #008000;">&#40;</span>fieldName<span style="color: #008000;">&#41;</span> <span style="color: #008000;">??</span> fieldName<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;">string</span> GetColumnCaptionFromBindingPath<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> bindingPath<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>DataGridBoundColumn column <span style="color: #0600FF; font-weight: bold;">in</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Columns</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>column <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>
            <span style="color: #0600FF; font-weight: bold;">continue</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        Binding binding <span style="color: #008000;">=</span> column<span style="color: #008000;">.</span><span style="color: #0000FF;">Binding</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>binding <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>
            <span style="color: #0600FF; font-weight: bold;">continue</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>binding<span style="color: #008000;">.</span><span style="color: #0000FF;">Path</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Path</span> <span style="color: #008000;">==</span> bindingPath<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> Convert<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span>column<span style="color: #008000;">.</span><span style="color: #0000FF;">Header</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
Unfortunately, this only seems to work on initial binding. Whenever an <code>ICollectionView</code> is bound to a <code>DataGrid</code> and the sorting or grouping is changed, either by changing the <code>SortDescriptions</code>, changing the <code>GroupDescriptions</code> or by changing the sort order by clicking on the column headers, the column name is used in stead of the header name. This is probably a bug within the <code>DataGrid</code> component. A work around this problem is to clone the <code>ICollectionView</code> and assign this clone to the <code>ItemsSource</code> of the <code>DataGrid</code> by replacing the definition of <code>SetItemsSource</code> by this
</p>

<div class="wp_syntax"><div class="code"><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> SetItemsSource<span style="color: #008000;">&#40;</span>IEnumerable itemsSource<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    ICollectionView oldValue <span style="color: #008000;">=</span> CollectionView<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>oldValue <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>
        <span style="color: #008000;">&#40;</span>oldValue<span style="color: #008000;">.</span><span style="color: #0000FF;">SortDescriptions</span> <span style="color: #0600FF; font-weight: bold;">as</span> INotifyCollectionChanged<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CollectionChanged</span> <span style="color: #008000;">-=</span> OnChangeRebindCollection<span style="color: #008000;">;</span>
        oldValue<span style="color: #008000;">.</span><span style="color: #0000FF;">GroupDescriptions</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CollectionChanged</span> <span style="color: #008000;">-=</span> OnChangeRebindCollection<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    ICollectionView newValue <span style="color: #008000;">=</span> CreateCollectionView<span style="color: #008000;">&#40;</span>itemsSource<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>newValue <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>
       <span style="color: #008000;">&#40;</span>newValue<span style="color: #008000;">.</span><span style="color: #0000FF;">SortDescriptions</span> <span style="color: #0600FF; font-weight: bold;">as</span> INotifyCollectionChanged<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CollectionChanged</span> <span style="color: #008000;">+=</span> OnChangeRebindCollection<span style="color: #008000;">;</span>
        newValue<span style="color: #008000;">.</span><span style="color: #0000FF;">GroupDescriptions</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CollectionChanged</span> <span style="color: #008000;">+=</span> OnChangeRebindCollection<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    CollectionView <span style="color: #008000;">=</span> newValue<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ItemsSource</span> <span style="color: #008000;">=</span> newValue<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
and to add the following functions:
</p>

<div class="wp_syntax"><div class="code"><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> OnChangeRebindCollection<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, NotifyCollectionChangedEventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    Rebind<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> Rebind<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    ItemsSource <span style="color: #008000;">=</span> CollectionView<span style="color: #008000;">.</span><span style="color: #0000FF;">Clone</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></div></div>

<p>
Unfortunately reassigning the <code>ItemsSource</code> or the <code>base.ItemsSource</code> does not work. Because the class <code>PagedCollectionView</code> that is used for implementing the <code>CollectionView</code> does not implement a <code>Clone</code> method, the following extension method is created:
</p>

<div class="wp_syntax"><div class="code"><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> ICollectionView Clone<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> ICollectionView source<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>source <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>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    PagedCollectionView result <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PagedCollectionView<span style="color: #008000;">&#40;</span>source<span style="color: #008000;">.</span><span style="color: #0000FF;">SourceCollection</span><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>source<span style="color: #008000;">.</span><span style="color: #0000FF;">Culture</span> <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>
        result<span style="color: #008000;">.</span><span style="color: #0000FF;">Culture</span> <span style="color: #008000;">=</span> source<span style="color: #008000;">.</span><span style="color: #0000FF;">Culture</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>source<span style="color: #008000;">.</span><span style="color: #0000FF;">Filter</span> <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>
        result<span style="color: #008000;">.</span><span style="color: #0000FF;">Filter</span> <span style="color: #008000;">=</span> source<span style="color: #008000;">.</span><span style="color: #0000FF;">Filter</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>SortDescription sortDescription <span style="color: #0600FF; font-weight: bold;">in</span> source<span style="color: #008000;">.</span><span style="color: #0000FF;">SortDescriptions</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        result<span style="color: #008000;">.</span><span style="color: #0000FF;">SortDescriptions</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>sortDescription<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;">foreach</span> <span style="color: #008000;">&#40;</span>GroupDescription groupDescription <span style="color: #0600FF; font-weight: bold;">in</span> source<span style="color: #008000;">.</span><span style="color: #0000FF;">GroupDescriptions</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        result<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>groupDescription<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    IPagedCollectionView pagedCollectionView <span style="color: #008000;">=</span> source <span style="color: #0600FF; font-weight: bold;">as</span> IPagedCollectionView<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>pagedCollectionView <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>
        result<span style="color: #008000;">.</span><span style="color: #0000FF;">PageSize</span> <span style="color: #008000;">=</span> pagedCollectionView<span style="color: #008000;">.</span><span style="color: #0000FF;">PageSize</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">return</span> result<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
Unfortunately, for some reason or another, the addition of
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    result<span style="color: #008000;">.</span><span style="color: #0000FF;">MoveCurrentTo</span><span style="color: #008000;">&#40;</span>source<span style="color: #008000;">.</span><span style="color: #0000FF;">CurrentItem</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>
leads to the following exception:
</p>
<blockquote><p>Cannot change or check the contents or current position of the PagedCollectionView while Refresh is being deferred.</p></blockquote>
<p>
This is probably a bug in the implementation of <code>PagedCollectionView</code>: probably the raising of the event <code>INotifyCollectionChanged.CollectionChanged</code> is made before all <code>IDisposable</code>&#8216;s returned from the internal calls to <code>DeferRefresh</code> have been disposed. As a consequence of this, it is not possible to keep the selected item selected after regrouping or sorting.
</p>
<p>
Finally: in order to enable prevention of multiple rebindings of the <code>ItemsSource</code>, the method <code>Rebind()</code> described in <a href="http://www.pochet.net/blog/2010/08/25/postponing-actions/">this article</a> is refactored as follows:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Rebind<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    PostponableRebind<span style="color: #008000;">.</span><span style="color: #0000FF;">Execute</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;">private</span> PostponableAction _postponableRebind<span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">private</span> PostponableAction PostponableRebind
<span style="color: #008000;">&#123;</span>
    get
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>_postponableRebind <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>
            _postponableRebind <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PostponableAction<span style="color: #008000;">&#40;</span>RebindImplementation<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> _postponableRebind<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> IDisposable PostponeRebind
<span style="color: #008000;">&#123;</span>
    get
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> PostponableRebind<span style="color: #008000;">.</span><span style="color: #0000FF;">PostponeAction</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;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> RebindImplementation<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    ItemsSource <span style="color: #008000;">=</span> CollectionView<span style="color: #008000;">.</span><span style="color: #0000FF;">Clone</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></div></div>

<h4><a name="group_panel">Match with <code>MyDataGridGroupHeaderPanel</code></a></h4>
<p>
In order to support column headers that differ from the column binding names in the <code>MyDataGridGroupHeaderPanel</code>, instances of the inner class <code>GroupDefinition</code> are used as items of the <code>ListBox</code> in stead of the actual headers. This inner class is defined as follows:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">class</span> GroupDefinition
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> GroupDefinition<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> field, <span style="color: #6666cc; font-weight: bold;">string</span> caption<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: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #008000;">&#40;</span>field<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;">throw</span> <span style="color: #008000;">new</span> ArgumentNullException<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;field&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        Field <span style="color: #008000;">=</span> field<span style="color: #008000;">;</span>
        Caption <span style="color: #008000;">=</span> caption <span style="color: #008000;">??</span> field<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;">string</span> Field <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">internal</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Caption <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">internal</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">string</span> ToString<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;">return</span> Caption<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
The <code>Drag</code> event handler that adds an item to the <code>ListBox</code> becomes:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">void</span> DragDropTarget_Drop<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, Microsoft<span style="color: #008000;">.</span><span style="color: #0000FF;">Windows</span><span style="color: #008000;">.</span><span style="color: #0000FF;">DragEventArgs</span> e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>DataGrid <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>
        <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #6666cc; font-weight: bold;">object</span> data <span style="color: #008000;">=</span> e<span style="color: #008000;">.</span><span style="color: #0000FF;">Data</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetData</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">Data</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetFormats</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</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>data <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>
        <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    DataGridColumnHeader dgch <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>data <span style="color: #0600FF; font-weight: bold;">as</span> ItemDragEventArgs<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Data</span> <span style="color: #0600FF; font-weight: bold;">as</span> SelectionCollection<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Item</span> <span style="color: #0600FF; font-weight: bold;">as</span> DataGridColumnHeader<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>dgch <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>
        <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #6666cc; font-weight: bold;">string</span> caption <span style="color: #008000;">=</span> Convert<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span>dgch<span style="color: #008000;">.</span><span style="color: #0000FF;">Content</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">string</span> fieldName <span style="color: #008000;">=</span> DataGrid<span style="color: #008000;">.</span><span style="color: #0000FF;">GetBindingPathFromColumnCaption</span><span style="color: #008000;">&#40;</span>caption<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>OnHeaderItemsChanged<span style="color: #008000;">.</span><span style="color: #0000FF;">PostponeAction</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>GroupDefinition item <span style="color: #0600FF; font-weight: bold;">in</span> listGroups<span style="color: #008000;">.</span><span style="color: #0000FF;">Items</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>item<span style="color: #008000;">.</span><span style="color: #0000FF;">Field</span> <span style="color: #008000;">==</span> fieldName<span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                listGroups<span style="color: #008000;">.</span><span style="color: #0000FF;">Items</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Remove</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;">break</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
        listGroups<span style="color: #008000;">.</span><span style="color: #0000FF;">Items</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> GroupDefinition<span style="color: #008000;">&#40;</span>fieldName, caption<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
Note that extra logic has been added to prevent a single column to be added twice. Unfortunately it is not possible to get the column definition from the <code>DataGridColumnHeader</code>, so the binding name has to be retrieved using the function <code>DataGrid.GetBindingPathFromColumnCaption</code> which is defined in <code>MyDataGrid</code> as
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> GetBindingPathFromColumnCaption<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> columnCaption<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    DataGridBoundColumn column <span style="color: #008000;">=</span> Columns<span style="color: #008000;">.</span><span style="color: #0000FF;">FirstOrDefault</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>c<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> Convert<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span>c<span style="color: #008000;">.</span><span style="color: #0000FF;">Header</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">==</span> columnCaption<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">as</span> DataGridBoundColumn<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>column <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>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    Binding binding <span style="color: #008000;">=</span> column<span style="color: #008000;">.</span><span style="color: #0000FF;">Binding</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>binding <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">||</span> binding<span style="color: #008000;">.</span><span style="color: #0000FF;">Path</span> <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>
        <span style="color: #0600FF; font-weight: bold;">return</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;">return</span> binding<span style="color: #008000;">.</span><span style="color: #0000FF;">Path</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Path</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
The actual synchronization of the <code>GroupDescriptions</code> is moved to the <code>CollectionChanged</code> event handler of the <code>ListBox</code> which is defined as follows:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#40;</span>listGroups<span style="color: #008000;">.</span><span style="color: #0000FF;">Items</span> <span style="color: #0600FF; font-weight: bold;">as</span> INotifyCollectionChanged<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CollectionChanged</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> OnHeaderItemsChanged<span style="color: #008000;">.</span><span style="color: #0000FF;">Execute</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;">;</span></pre></div></div>

<p>
with the following supporting definitions:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> PostponableAction _onHeaderItemsChanged<span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">private</span> PostponableAction OnHeaderItemsChanged
<span style="color: #008000;">&#123;</span>
    get
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>_onHeaderItemsChanged <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>
            _onHeaderItemsChanged <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PostponableAction<span style="color: #008000;">&#40;</span>OnHeaderItemsChangedImplementation<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> _onHeaderItemsChanged<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;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnHeaderItemsChangedImplementation<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>DataGrid <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>
        <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    ICollectionView view <span style="color: #008000;">=</span> DataGrid<span style="color: #008000;">.</span><span style="color: #0000FF;">CollectionView</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>view <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>
        <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    ObservableCollection<span style="color: #008000;">&lt;</span>GroupDescription<span style="color: #008000;">&gt;</span> groupDescriptions <span style="color: #008000;">=</span> view<span style="color: #008000;">.</span><span style="color: #0000FF;">GroupDescriptions</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>groupDescriptions <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>
        <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>DataGrid<span style="color: #008000;">.</span><span style="color: #0000FF;">PostponeRebind</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        groupDescriptions<span style="color: #008000;">.</span><span style="color: #0000FF;">Clear</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>listGroups<span style="color: #008000;">.</span><span style="color: #0000FF;">Items</span> <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>
            <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>GroupDefinition groupDefinition <span style="color: #0600FF; font-weight: bold;">in</span> listGroups<span style="color: #008000;">.</span><span style="color: #0000FF;">Items</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                groupDescriptions<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;">&#123;</span> PropertyName <span style="color: #008000;">=</span> groupDefinition<span style="color: #008000;">.</span><span style="color: #0000FF;">Field</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
The <code>ListBox</code> is initially filled by
</p>

<div class="wp_syntax"><div class="code"><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> FillGroupHeaderPanelFromGroupDescriptions<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;">using</span> <span style="color: #008000;">&#40;</span>OnHeaderItemsChanged<span style="color: #008000;">.</span><span style="color: #0000FF;">PostponeAction</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        listGroups<span style="color: #008000;">.</span><span style="color: #0000FF;">Items</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Clear</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>DataGrid <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>
            <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        ICollectionView collection <span style="color: #008000;">=</span> DataGrid<span style="color: #008000;">.</span><span style="color: #0000FF;">CollectionView</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>collection <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>
            <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>PropertyGroupDescription groupDescription <span style="color: #0600FF; font-weight: bold;">in</span> collection<span style="color: #008000;">.</span><span style="color: #0000FF;">GroupDescriptions</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            listGroups<span style="color: #008000;">.</span><span style="color: #0000FF;">Items</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> GroupDefinition<span style="color: #008000;">&#40;</span>groupDescription<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyName</span>, DataGrid<span style="color: #008000;">.</span><span style="color: #0000FF;">GetColumnCaptionFromBindingPath</span><span style="color: #008000;">&#40;</span>groupDescription<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyName</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<h3><a name="download">Download</a></h3>
<p>
The source accompanying this blog can be downloaded <a href='http://www.pochet.net/blog/wp-content/uploads/2010/08/DataGridGroupTest.zip'>here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pochet.net/blog/2010/08/25/drag-and-drop-grouping-in-datagrid/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Postponing actions</title>
		<link>http://www.pochet.net/blog/2010/08/25/postponing-actions/</link>
		<comments>http://www.pochet.net/blog/2010/08/25/postponing-actions/#comments</comments>
		<pubDate>Wed, 25 Aug 2010 08:10:27 +0000</pubDate>
		<dc:creator>Emiel</dc:creator>
				<category><![CDATA[.NET]]></category>

		<guid isPermaLink="false">http://www.pochet.net/blog/?p=200</guid>
		<description><![CDATA[Introduction Often modifications to an object (setting properties or calling methods etc.) cause synchronization actions to be executed. For instance, adding a SortDescription to an ICollectionView object can cause the object to perform a sorting operation on the items it contains. When multiple modifications to an object are made, it is desirable to postpone these [...]]]></description>
			<content:encoded><![CDATA[<h3><a name="introduction">Introduction</a></h3>
<p>
Often modifications to an object (setting properties or calling methods etc.) cause synchronization actions to be executed. For instance, adding a <code>SortDescription</code> to an <code>ICollectionView</code> object can cause the object to perform a sorting operation on the items it contains. When multiple modifications to an object are made, it is desirable to postpone these synchronization actions until all modifications are made.
</p>
<p>
This blog presents the <code>PostponableAction</code> class with which this kind of behavior can be implemented. This idea is based on and can be used as an implementation to the <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.icollectionview.deferrefresh%28v=VS.95%29.aspx"><code>DeferRefresh</code></a> method of the <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.icollectionview%28v=VS.95%29.aspx"><code>ICollectionView</code></a> interface.
</p>
<h3><a name="contents">Table of contents</a></h3>
<ul>
<li><a href="#introduction">Introduction</a></li>
<li><a href="#implementation">Implementation</a></li>
<li><a href="#usage">Usage</a></li>
<li><a href="#download">Download</a></li>
</ul>
<h3><a name="implementation">Implementation</a></h3>
<p>
The heart of the <code>PostponableAction</code> class is made up of the inner class <code>PostponableActionHelper</code>. The purpose of this class is to perform an action when the instance is disposed. This class is implemented as follows:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">class</span> PostponableActionHelper <span style="color: #008000;">:</span> IDisposable
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> Action _onDispose<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">bool</span> _disposed<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> PostponableActionHelper<span style="color: #008000;">&#40;</span>Action onDispose<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>onDispose <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>
            <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> ArgumentNullException<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;onDispose&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        _onDispose <span style="color: #008000;">=</span> onDispose<span style="color: #008000;">;</span>
        _disposed <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    ~PostponableActionHelper<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Dispose<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Dispose<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Dispose<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        GC<span style="color: #008000;">.</span><span style="color: #0000FF;">SuppressFinalize</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> Dispose<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span> disposing<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>_disposed<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        _onDispose<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        _disposed <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>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
The <code>PostponableAction</code> is implemented as follows:
</p>

<div class="wp_syntax"><div class="code"><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> PostponableAction
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> Action _action<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">int</span> _postponeLevel<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">bool</span> _mustExecute<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">Object</span> _lockObject <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> PostponableAction<span style="color: #008000;">&#40;</span>Action action<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>action <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>
            <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> ArgumentNullException<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;action&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        _action <span style="color: #008000;">=</span> action<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> Execute<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;">lock</span> <span style="color: #008000;">&#40;</span>_lockObject<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>_postponeLevel <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                _mustExecute <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
        _action<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> IDisposable PostponeAction
    <span style="color: #008000;">&#123;</span>
        get
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">lock</span> <span style="color: #008000;">&#40;</span>_lockObject<span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                _postponeLevel<span style="color: #008000;">++;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> PostponableActionHelper<span style="color: #008000;">&#40;</span>OnDisposePostponableActionHelper<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> IsPostponing
    <span style="color: #008000;">&#123;</span>
        get
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> _postponeLevel <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</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;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnDisposePostponableActionHelper<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;">lock</span> <span style="color: #008000;">&#40;</span>_lockObject<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>_postponeLevel <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span> <span style="color: #008000;">||</span> <span style="color: #008000;">!</span>_mustExecute<span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
            _mustExecute <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        _action<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></div></div>

<p>
Some notes on the implementation of <code>PostponableAction</code>:
</p>
<ul>
<li>The implementation is thread safe</li>
<li>If <code>Execute()</code> has not been called during the lifetime of a <code>PostponeAction</code> object, the action will not be executed when this object is disposed.</li>
<li>If <code>Execute()</code> has been called during the lifetime of a <code>PostponeAction</code> object, the action will be executed once when the last <code>PostponeAction</code> object is disposed.</li>
</ul>
<h3><a name="usage">Usage</a></h3>
<p>
The following class gives a sample of the usage of <code>PostponableAction</code>:
</p>

<div class="wp_syntax"><div class="code"><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> Work
<span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">private</span> PostponableAction _synchronize <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PostponableAction<span style="color: #008000;">&#40;</span>SynchronizeImplementation<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> SynchronizeImplementation<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 time consuming operations</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> Synchronize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		_synchronize<span style="color: #008000;">.</span><span style="color: #0000FF;">Execute</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> IDisposable PostponeSynchronize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		get
		<span style="color: #008000;">&#123;</span>
			<span style="color: #0600FF; font-weight: bold;">return</span> _synchronize<span style="color: #008000;">.</span><span style="color: #0000FF;">PostponeAction</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Operation1<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 operations followed by</span>
&nbsp;
		Synchronize<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> Operation2<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 operations followed by</span>
&nbsp;
		Synchronize<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> Operation3<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;">using</span> <span style="color: #008000;">&#40;</span>PostponeSynchronize<span style="color: #008000;">&#41;</span>
		<span style="color: #008000;">&#123;</span>
			Operation1<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			Operation2<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
When <code>Operation3</code> is called, the synchronization executed only once when the <code>using</code> block is exited.
</p>
<h3><a name="download">Download</a></h3>
<p>
The source for the <code>PostponableAction</code> class can be downloaded <a href='http://www.pochet.net/blog/wp-content/uploads/2010/08/PostponableAction.zip'>here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pochet.net/blog/2010/08/25/postponing-actions/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Synchronizing group row headers with column headers in Silverlight DataGrid</title>
		<link>http://www.pochet.net/blog/2010/08/20/synchronizing-group-row-headers-with-column-headers-in-silverlight-datagrid/</link>
		<comments>http://www.pochet.net/blog/2010/08/20/synchronizing-group-row-headers-with-column-headers-in-silverlight-datagrid/#comments</comments>
		<pubDate>Fri, 20 Aug 2010 09:32:58 +0000</pubDate>
		<dc:creator>Emiel</dc:creator>
				<category><![CDATA[DataGrid]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.pochet.net/blog/?p=193</guid>
		<description><![CDATA[Update The code for the functionality in this post has been updated in Update postings on Datagrid Grouping in the DataGrid in Silverlight is quite easy. What is not so obvious is how to change the caption of the column name on the group row. It turns out that you could use the LoadingRowGroup of [...]]]></description>
			<content:encoded><![CDATA[<h2 style='color:red;font-weight:bold;'>Update</h2>
<p style='color:red;font-weight:bold;'>
The code for the functionality in this post has been updated in <a href="http://www.pochet.net/blog/2010/08/29/update-postings-on-datagrid/" style='color:red;font-weight:bold;text-decoration:underline;'>Update postings on Datagrid</a>
</p>
<p>
Grouping in the DataGrid in Silverlight is <a href="http://msdn.microsoft.com/en-us/library/dd833072%28VS.95%29.aspx">quite easy</a>. What is not so obvious is how to change the caption of the column name on the group row. It turns out that you could use the <code>LoadingRowGroup</code> of the DataGrid:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">theGrid<span style="color: #008000;">.</span><span style="color: #0000FF;">LoadingRowGroup</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>
    e<span style="color: #008000;">.</span><span style="color: #0000FF;">RowGroupHeader</span><span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyName</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;A custom caption&quot;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
The original value of the <code>e.RowGroupHeader.PropertyName</code> is the <code>PropertyName</code> of the <code>PropertyGroupDescription</code>. Using this value the column header can be determined:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> GetColumnCaption<span style="color: #008000;">&#40;</span>DataGrid grid, <span style="color: #6666cc; font-weight: bold;">string</span> columnName<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>grid <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;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #008000;">&#40;</span>columnName<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;">return</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;">foreach</span> <span style="color: #008000;">&#40;</span>DataGridBoundColumn column <span style="color: #0600FF; font-weight: bold;">in</span> grid<span style="color: #008000;">.</span><span style="color: #0000FF;">Columns</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>column <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>
            <span style="color: #0600FF; font-weight: bold;">continue</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        Binding binding <span style="color: #008000;">=</span> column<span style="color: #008000;">.</span><span style="color: #0000FF;">Binding</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>binding <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>
            <span style="color: #0600FF; font-weight: bold;">continue</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>binding<span style="color: #008000;">.</span><span style="color: #0000FF;">Path</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Path</span> <span style="color: #008000;">==</span> columnName<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> Convert<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span>column<span style="color: #008000;">.</span><span style="color: #0000FF;">Header</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
Using this function synchronizing the group row header with column headers becomes almost trivial:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">theGrid<span style="color: #008000;">.</span><span style="color: #0000FF;">LoadingRowGroup</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: #6666cc; font-weight: bold;">string</span> columnName <span style="color: #008000;">=</span> e<span style="color: #008000;">.</span><span style="color: #0000FF;">RowGroupHeader</span><span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyName</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">string</span> columnCaption <span style="color: #008000;">=</span> GetColumnCaption<span style="color: #008000;">&#40;</span>sender <span style="color: #0600FF; font-weight: bold;">as</span> DataGrid, columnName<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><span style="color: #008000;">!</span><span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #008000;">&#40;</span>columnCaption<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        e<span style="color: #008000;">.</span><span style="color: #0000FF;">RowGroupHeader</span><span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyName</span> <span style="color: #008000;">=</span> columnCaption<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
Other interesting properties of <code>e.RowGroupHeader</code> are:
</p>
<ul>
<li><code>PropertyNameVisibility</code>: determines the visibility of the <code>PropertyName</code></li>
<li><code>ItemCountVisibility</code>: determines the visibility of the item count</li>
</ul>
<p>
The sourcecode for this blog can be found <a href='http://www.pochet.net/blog/wp-content/uploads/2010/08/DataGridTest.zip'>here</a>.
</p>
<h3>Update</h3>
<p>
When the DataGrid is sorted, the group row headers display their old captions again. See <a href="http://www.pochet.net/blog/2010/08/25/drag-and-drop-grouping-in-datagrid/#group_rows">this post</a> for a possible solution.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pochet.net/blog/2010/08/20/synchronizing-group-row-headers-with-column-headers-in-silverlight-datagrid/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>INotifyPropertyChanged: automatic dependent property and nested object support</title>
		<link>http://www.pochet.net/blog/2010/07/02/inotifypropertychanged-automatic-dependent-property-and-nested-object-support/</link>
		<comments>http://www.pochet.net/blog/2010/07/02/inotifypropertychanged-automatic-dependent-property-and-nested-object-support/#comments</comments>
		<pubDate>Fri, 02 Jul 2010 15:05:25 +0000</pubDate>
		<dc:creator>Emiel</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[INotifyPropertyChanged]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.pochet.net/blog/?p=159</guid>
		<description><![CDATA[<p>
The hardest problem with <code>INotifyPropertyChanged</code> is often overlooked: dependency management. When one property is changed, all of its dependent properties need to fire as well.
</p>
<p>
This blog presents another solution based on dynamic dependency determination.
</p>]]></description>
			<content:encoded><![CDATA[<h3><a name="summary">Summary</a></h3>
<p>
The hardest problem with <code>INotifyPropertyChanged</code> is often overlooked: dependency management. When one property is changed, all of its dependent properties need to fire as well.
</p>
<p>
This blog presents another solution based on dynamic dependency determination.
</p>
<h3><a name="contents">Table of contents</a></h3>
<ul>
<li><a href="#introduction">Introduction</a></li>
<li><a href="#problem">The problem</a>
<ul>
<li><a href="#problem_introduction">Introduction</a></li>
<li><a href="#problem_properties">Dependent properties</a></li>
<li><a href="#problem_objects">Nested objects</a></li>
</ul>
</li>
<li><a href="#solution">Solution</a></li>
<li><a href="#implementation">Implementation</a>
<ul>
<li><a href="#baseclass">Base class</a></li>
<li><a href="#storage">Storage of dependency information</a></li>
<li><a href="#notify">Notify changes</a></li>
<li><a href="#callstack">Property Get call stack</a></li>
<li><a href="#determining">Determining dependencies</a></li>
<li><a href="#nested">Nested object support</a></li>
<li><a href="#prevent_storm">Preventing a PopertyChanged event storm</a></li>
</ul>
</li>
<li><a href="#usage">Usage</a></li>
<li><a href="#example">Example</a>
<ul>
<li><a href="#person">Class Person</a></li>
<li><a href="#hellomessage">Class HelloMessage</a></li>
<li><a href="#test">Test</a></li>
</ul>
</li>
<li><a href="#download">Download</a></li>
<li><a href="#final">Final words</a></li>
</ul>
<h3><a name="introduction">Introduction</a></h3>
<p>
As stated by <a href="http://updatecontrols.net/cs/index.shtml">Michael L Perry</a> in <a href="http://www.pochet.net/blog/2010/06/25/inotifypropertychanged-implementations-an-overview/#comment-37">a comment</a> on my blog <a href=" http://www.pochet.net/blog/2010/06/25/inotifypropertychanged-implementations-an-overview">INotifyPropertyChanged implementations: An Overview</a>:
</p>
<p><cite><br />
The hardest problem with INotifyPropertyChanged is often overlooked: dependency management. When one property is changed, all of its dependent properties need to fire as well.<br />
</cite></p>
<p>
Here are some solutions other people have come up with:</p>
<ul>
<li>Michael L Perry created a <a href="http://updatecontrols.net/cs/index.shtml">solution based on <code>Independent</code> properties</a></li>
<li><a href="http://neilmosafi.blogspot.com">Neil Mosafi</a> also recognizes this problem in this <a href="http://neilmosafi.blogspot.com/2008/07/is-inotifypropertychanged-anti-pattern.html">blog post</a> and <a href="http://www.specopssoft.com">Tomas Elison</a> presented in <a href="http://neilmosafi.blogspot.com/2008/07/is-inotifypropertychanged-anti-pattern.html?showComment=1227214780424#c1816595287898117206">this comment</a> a solution based on attributes</li>
</ul>
<p>
This blog presents another solution based on dynamic dependency determination.
</p>
<h3><a name="problem">The problem</a></h3>
<h4><a name="problem_introduction">Introduction</a></h4>
<p>
There are two problems to solve:</p>
<ul>
<li>How to generate <code>INotifyPropertyChanged</code> notifications for dependent properties</li>
<li>How to generate <code>INotifyPropertyChanged</code> notifications for nested object hierarchies</li>
</ul>
<p>The following paragraphs give examples of both kind of problems.
</p>
<h4><a name="problem_properties">Dependent properties</a></h4>
<p>
Suppose there is a <code>Person</code> class that contains the properties <code>FirstName</code>, <code>LastName</code> and the calculated property <code>FullName</code>. When for instance the first name of a person changes, obviously the full name also changes. This class could be implemented like this using the <code>BindableObjectBase</code> from <a href="http://www.pochet.net/blog/2010/06/25/inotifypropertychanged-implementations-an-overview">this post</a>:
</p>

<div class="wp_syntax"><div class="code"><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> Person <span style="color: #008000;">:</span> BindableObjectBase
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> firstName<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> lastName<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> Person<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> firstName, <span style="color: #6666cc; font-weight: bold;">string</span> lastName<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;">FirstName</span> <span style="color: #008000;">=</span> firstName<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">LastName</span> <span style="color: #008000;">=</span> lastName<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;">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;">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;">SetValue</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">firstName</span>, value, <span style="color: #666666;">&quot;FirstName&quot;</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;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnPropertyChanged</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;FullName&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> LastName
    <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;">lastName</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;">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;">SetValue</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">lastName</span>, value, <span style="color: #666666;">&quot;LastName&quot;</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;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnPropertyChanged</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;FullName&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> FullName
    <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: #666666;">&quot; &quot;</span> <span style="color: #008000;">+</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">LastName</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></div></div>

<p>
The problem lies in the manual addition of the <code>OnPropertyChanged</code> calls. When classes get complex, it can be a nightmare to maintain.
</p>
<h4><a name="problem_objects">Nested objects</a></h4>
<p>
Now suppose there is also a <code>HelloMessage</class> that is defined as follows:
</p>

<div class="wp_syntax"><div class="code"><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> HelloMessage <span style="color: #008000;">:</span> BindableObjectBase
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> Person person<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> HelloMessage<span style="color: #008000;">&#40;</span>Person person<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;">Person</span> <span style="color: #008000;">=</span> person<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> Person Person
    <span style="color: #008000;">&#123;</span>
        get
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">person</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        set
        <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;">SetValue</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">person</span>, value, <span style="color: #666666;">&quot;Person&quot;</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;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnPropertyChanged</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Message&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Message
    <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: #666666;">&quot;Hello &quot;</span> <span style="color: #008000;">+</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Person</span><span style="color: #008000;">.</span><span style="color: #0000FF;">FullName</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></div></div>

<p>
Now this code
</p>
<p><a name="problem_objects_sample"></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Person person <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Person<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;E&quot;</span>, <span style="color: #666666;">&quot;Jongerius&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
HelloMessage message <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> HelloMessage<span style="color: #008000;">&#40;</span>person<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
message<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyChanged</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">&#40;</span>s, e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span>
<span style="color: #008000;">&#123;</span>
    Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;* Changed: &quot;</span> <span style="color: #008000;">+</span> e<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyName</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Display the message</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>message<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Change first name</span>
person<span style="color: #008000;">.</span><span style="color: #0000FF;">FirstName</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Emiel&quot;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Display the message again</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>message<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p></a></p>
<p>
results in
</p>

<div class="wp_syntax"><div class="code"><pre class="console" style="font-family:monospace;">Hello E Jongerius
Hello Emiel Jongerius</pre></div></div>

<p>
The <code>message.PropertyChanged</code> event is not raised, although the value of <code>message.Message</code> has been changed.
</p>
<p>
The usual way to approach this problem is to replace the definition of the <code>Person</code> property with the following:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> Person Person
<span style="color: #008000;">&#123;</span>
    get
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">person</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    set
    <span style="color: #008000;">&#123;</span>
        INotifyPropertyChanged oldValue <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">person</span> <span style="color: #0600FF; font-weight: bold;">as</span> INotifyPropertyChanged<span style="color: #008000;">;</span>
        INotifyPropertyChanged newValue <span style="color: #008000;">=</span> value <span style="color: #0600FF; font-weight: bold;">as</span> INotifyPropertyChanged<span style="color: #008000;">;</span>
&nbsp;
        <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;">SetValue</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">person</span>, value, <span style="color: #666666;">&quot;Person&quot;</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;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnPropertyChanged</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Message&quot;</span><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>oldValue <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>
                oldValue<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyChanged</span> <span style="color: #008000;">-=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnPersonChanged</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>newValue <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>
                newValue<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyChanged</span> <span style="color: #008000;">+=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnPersonChanged</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</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> OnPersonChanged<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> s, PropertyChangedEventArgs 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;">OnPropertyChanged</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Person&quot;</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;">OnPropertyChanged</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Message&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
Again, this can result in a maintenance nightmare when object hierarchies become complex.
</p>
<h3><a name="solution">Solution</a></h3>
<p>
The basic idea of the solution described in this blog is that the base class keeps track of the dependencies between the properties. This information is determined dynamically at runtime, during the calls to the property getters. Whenever the value of a property is changed, the previously determined information is used to raise change notifications for dependent properties.
</p>
<p>
Also the property values are checked whether they implement the <code>INotifyPropertyChanged</code> interface. If so, the appropriate event handlers are attached and detached during the setting of the property. A similar strategy is used by <a href="http://caliburn.codeplex.com/">Caliburn</a>.
</p>
<p>
There are two important things to notice about this approach:</p>
<ol>
<li>Dependencies are determined dynamically at runtime, so a performance hit is likely to be expected</li>
<li>Dependencies are updated only when property getters are called. This implies that when the value of a property has not been retrieved, the dependency information regarding this property has not been determined. Normally this would be no problem, because if the value of a property has not been used, the application is probably also not interested in the fact that the value of this property has been changed</li>
</ol>
<h3><a name="implementation">Implementation</a></h3>
<h4><a name="baseclass">Base class</a></h4>
<p>
The solution uses the <code>BindableObjectBase</code> class from <a href="http://www.pochet.net/blog/2010/06/25/inotifypropertychanged-implementations-an-overview">this post</a> as the base class with the following modifications:</p>
<ul>
<li>The function <code>OnPropertyChanged(string propertyName)</code> has been made <code>virtual</code></li>
<li>The static function <code>GetPropertyNameFromStackTrace()</code> has been made <code>protected</code></li>
<li>The static function <code>GetPropertyNameFromExpression&lt;T>(Expression&lt;Func&lt;T>> property)</code> has been made <code>protected</code>
<li>Refactored the functions <code>SetValue&lt;T>(ref T local, T newValue, Expression&lt;Func&lt;T>> property)</code> and <code>SetValue&lt;T>(ref T local, T newValue)</code> to make use of the function <code>SetValue&lt;T>(ref T local, T newValue, string propertyName)</code>
<li>The function <code>SetValue&lt;T>(ref T local, T newValue, string propertyName)</code> has been made <code>virtual</code></li>
</ul>
<h4><a name="storage">Storage of dependency information</a></h4>
<p>
The dependency information is stored as follows:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> IDictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span>, IDictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span>, ISet<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;&gt;&gt;</span> globalPropertyDependencyCollection <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Dictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span>, IDictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span>, ISet<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;&gt;&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">private</span> IDictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span>, ISet<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;&gt;</span> propertyDependencyCollection<span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">private</span> IDictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span>, ISet<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;&gt;</span> PropertyDependencyCollection
<span style="color: #008000;">&#123;</span>
    get
    <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;">propertyDependencyCollection</span> <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>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>BindableObjectBase2<span style="color: #008000;">.</span><span style="color: #0000FF;">globalPropertyDependencyCollection</span><span style="color: #008000;">.</span><span style="color: #0000FF;">TryGetValue</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;">GetType</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">FullName</span>, <span style="color: #0600FF; font-weight: bold;">out</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">propertyDependencyCollection</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;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">propertyDependencyCollection</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Dictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span>, ISet<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                BindableObjectBase2<span style="color: #008000;">.</span><span style="color: #0000FF;">globalPropertyDependencyCollection</span><span style="color: #008000;">&#91;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetType</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">FullName</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">propertyDependencyCollection</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</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;">propertyDependencyCollection</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
The private static variable <code>globalPropertyDependencyCollection</code> is used to reused dependency information between instances of the same type. Access to the global dependency information is cached using the <code>propertyDependencyCollection</code> instance variable.
</p>
<p>
Adding dependency information is relatively straight forward:
</p>

<div class="wp_syntax"><div class="code"><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> AddPropertyDependency<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> property, <span style="color: #6666cc; font-weight: bold;">string</span> dependsOn<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    ISet<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span> propertyDependencies <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;">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;">PropertyDependencyCollection</span><span style="color: #008000;">.</span><span style="color: #0000FF;">TryGetValue</span><span style="color: #008000;">&#40;</span>dependsOn, <span style="color: #0600FF; font-weight: bold;">out</span> propertyDependencies<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        propertyDependencies <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> HashSet<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span><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: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">else</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>propertyDependencies<span style="color: #008000;">.</span><span style="color: #0000FF;">Contains</span><span style="color: #008000;">&#40;</span>property<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;">return</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    propertyDependencies<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>property<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;">PropertyDependencyCollection</span><span style="color: #008000;">&#91;</span>dependsOn<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> propertyDependencies<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<h4><a name="notify">Notify changes</a></h4>
<p>
Based on the stored dependency information, change notifications regarding dependent properties can be automatically sent using this override to <code>OnPropertyChanged</code>:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><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> OnPropertyChanged<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> propertyName<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;">OnPropertyChanged</span><span style="color: #008000;">&#40;</span>propertyName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    ISet<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span> dependsOn <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;">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;">PropertyDependencyCollection</span><span style="color: #008000;">.</span><span style="color: #0000FF;">TryGetValue</span><span style="color: #008000;">&#40;</span>propertyName, <span style="color: #0600FF; font-weight: bold;">out</span> dependsOn<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;">return</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> item <span style="color: #0600FF; font-weight: bold;">in</span> dependsOn<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;">OnPropertyChanged</span><span style="color: #008000;">&#40;</span>item<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<h4><a name="callstack">Property Get call stack</a></h4>
<p>
Before the actually dependencies can be determined, an additional piece of infrastructure has to be put in place. Whenever a property getter is called from within another getter, a dependency between these two properties must be added. To keep track of property getter calls, a call stack is implemented:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> Stack<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span> getCallStack <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Stack<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>
Based on the contents of this call stack, the dependencies can be added using the following function:
</p>

<div class="wp_syntax"><div class="code"><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> PropertyGetCalled<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> propertyName<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> p <span style="color: #0600FF; font-weight: bold;">in</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">getCallStack</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;">AddPropertyDependency</span><span style="color: #008000;">&#40;</span>p, propertyName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<h4><a name="determining">Determining dependencies</a></h4>
<p>
In order to determine the dependencies the following private class is used:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">class</span> PropertyTracker <span style="color: #008000;">:</span> IDisposable
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">bool</span> isDisposed <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> BindableObjectBase2 containingObject<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> PropertyTracker<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> propertyName, BindableObjectBase2 objectBase<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;">containingObject</span> <span style="color: #008000;">=</span> objectBase<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">containingObject</span><span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyGetCalled</span><span style="color: #008000;">&#40;</span>propertyName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">containingObject</span><span style="color: #008000;">.</span><span style="color: #0000FF;">getCallStack</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Push</span><span style="color: #008000;">&#40;</span>propertyName<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> Dispose<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Dispose<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        GC<span style="color: #008000;">.</span><span style="color: #0000FF;">SuppressFinalize</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> Dispose<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span> disposing<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;">isDisposed</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">containingObject</span><span style="color: #008000;">.</span><span style="color: #0000FF;">getCallStack</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Pop</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;">containingObject</span> <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;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">isDisposed</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>
&nbsp;
    ~PropertyTracker<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Dispose<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
This class serves two functions:</p>
<ol>
<li>It calls the function <code>PropertyGetCalled</code> on the containing object, which adds the dependencies to dependent properties</li>
<li>It pushes the property name on the <a href="#callstack">call stack</a> and makes sure that the name is removed whenever the class gets disposed.
</ol>
<p>As long as for each property an instance of this class is created at the start of the getter and this instance is disposed at the end of the getter, the <code>getCallStack</code> will be filled correct and the correct references will be added using the call to <code>PropertyGetCalled</code>.
</p>
<p>
To facilitate the use of the class the following helper functions have been defined:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">protected</span> IDisposable GetPropertyTracker<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;">return</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetPropertyTracker</span><span style="color: #008000;">&#40;</span>GetPropertyNameFromStackTrace<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">protected</span> IDisposable GetPropertyTracker<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>Expression<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span> property<span style="color: #008000;">&#41;</span>
<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;">GetPropertyTracker</span><span style="color: #008000;">&#40;</span>GetPropertyNameFromExpression<span style="color: #008000;">&#40;</span>property<span style="color: #008000;">&#41;</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;">protected</span> IDisposable GetPropertyTracker<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> propertyName<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> PropertyTracker<span style="color: #008000;">&#40;</span>propertyName, <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<h4><a name="nested">Nested object support</a></h4>
<p>
With the following additions it is possible to extend this solution to support nested objects:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> Dictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span>, PropertyChangedEventHandler<span style="color: #008000;">&gt;</span> propertyChangedEventHandlers <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Dictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span>, PropertyChangedEventHandler<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">bool</span> SetValue<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> T local, T newValue, <span style="color: #6666cc; font-weight: bold;">string</span> propertyName<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    INotifyPropertyChanged localAsBindableObject <span style="color: #008000;">=</span> local <span style="color: #0600FF; font-weight: bold;">as</span> INotifyPropertyChanged<span style="color: #008000;">;</span>
    INotifyPropertyChanged newValueAsBindableObject <span style="color: #008000;">=</span> newValue <span style="color: #0600FF; font-weight: bold;">as</span> INotifyPropertyChanged<span style="color: #008000;">;</span>
&nbsp;
    <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;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</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;">ref</span> local, newValue, propertyName<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;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>localAsBindableObject <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>
        PropertyChangedEventHandler handler <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;">if</span> <span style="color: #008000;">&#40;</span>propertyChangedEventHandlers<span style="color: #008000;">.</span><span style="color: #0000FF;">TryGetValue</span><span style="color: #008000;">&#40;</span>propertyName, <span style="color: #0600FF; font-weight: bold;">out</span> handler<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;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">propertyChangedEventHandlers</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Remove</span><span style="color: #008000;">&#40;</span>propertyName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            localAsBindableObject<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyChanged</span> <span style="color: #008000;">-=</span> handler<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;">if</span> <span style="color: #008000;">&#40;</span>newValueAsBindableObject <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>
        PropertyChangedEventHandler handler <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> OnPropertyChanged<span style="color: #008000;">&#40;</span>propertyName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</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;">propertyChangedEventHandlers</span><span style="color: #008000;">&#91;</span>propertyName<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> handler<span style="color: #008000;">;</span>
        newValueAsBindableObject<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyChanged</span> <span style="color: #008000;">+=</span> handler<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
The basic idea behind this code is that whenever a value of a property raises the <code>PropertyChanged</code> event, we can assume that that property has changed, so we can raise the <code>PropertyChanged</code> event for the object containing the property too. This is done  by attaching a <code>PropertyChanged</code> handler to all objects that support the <code>INotifyPropertyChanged</code> interface whenever they are used as a value of a property and to remove the handler whenever the property gets another value.
</p>
<h4><a name="prevent_storm">Preventing a PopertyChanged event storm</a></h4>
<p>
Executing <a href="#problem_objects_sample">this example</a> with the base objects that implement the techniques described above, gives the following results:
</p>

<div class="wp_syntax"><div class="code"><pre class="console" style="font-family:monospace;">* Changed: Person
* Changed: Message
* Changed: Person
* Changed: Message</pre></div></div>

<p>
The <code>PropertyChanged</code> event gets fired twice for each property. This is caused by the following chaing of events:
</p>
<p>
The statement <code>peson.FirstName = "Emiel";</code> causes <code>person.PropertyChanged("FirstName")</code> to be raised, which causes</p>
<ol>
<li><code>message.PropertyChanged("Person")</code> to be raised, which causes <code>message.PropertyChanged("Message")</code> to be raised.</li>
<li><code>person.PropertyChanged("FullName")</code> to be raised, which causes <code>message.PropertyChanged("Person")</code> to be raised, which causes <code>message.PropertyChanged("Message")</code> to be raised.</li>
</ol>
<p>
The solution to this problem is to reorder the generated <code>PropertyChanged</code> events  and remove the duplicates during the reordering. This strategy is implemented in the following class:
</p>

<div class="wp_syntax"><div class="code"><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> BindableObjectBase3 <span style="color: #008000;">:</span> BindableObjectBase2
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> IDictionary<span style="color: #008000;">&lt;</span>BindableObjectBase3, ISet<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;&gt;</span> delayedNotifications <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;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> BindableObjectBase3 currentObject <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
&nbsp;
    <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> OnPropertyChanged<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> propertyName<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">bool</span> isFirst <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>delayedNotifications <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>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">try</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>isFirst<span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                delayedNotifications <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Dictionary<span style="color: #008000;">&lt;</span>BindableObjectBase3, ISet<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                currentObject <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnPropertyChanged</span><span style="color: #008000;">&#40;</span>propertyName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0600FF; font-weight: bold;">else</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> <span style="color: #008000;">==</span> currentObject<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;">OnPropertyChanged</span><span style="color: #008000;">&#40;</span>propertyName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
                <span style="color: #0600FF; font-weight: bold;">else</span>
                <span style="color: #008000;">&#123;</span>
                    <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnPropertyChangedDelayed</span><span style="color: #008000;">&#40;</span>propertyName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">finally</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>isFirst<span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">try</span>
                <span style="color: #008000;">&#123;</span>
                    ProcessDelayedNotifications<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;">finally</span>
                <span style="color: #008000;">&#123;</span>
                    delayedNotifications <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
                    currentObject <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>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</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> OnPropertyChangedDelayed<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> propertyName<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        ISet<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span> delayedNotificationsForThis <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>delayedNotifications<span style="color: #008000;">.</span><span style="color: #0000FF;">TryGetValue</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span>, <span style="color: #0600FF; font-weight: bold;">out</span> delayedNotificationsForThis<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            delayedNotificationsForThis <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> HashSet<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            delayedNotifications<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span>, delayedNotificationsForThis<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        delayedNotificationsForThis<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>propertyName<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> ProcessDelayedNotifications<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>delayedNotifications <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>
            <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span>delayedNotifications<span style="color: #008000;">.</span><span style="color: #0000FF;">Any</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            KeyValuePair<span style="color: #008000;">&lt;</span>BindableObjectBase3, ISet<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;&gt;</span> firstItem <span style="color: #008000;">=</span> delayedNotifications<span style="color: #008000;">.</span><span style="color: #0000FF;">ElementAt</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            delayedNotifications<span style="color: #008000;">.</span><span style="color: #0000FF;">Remove</span><span style="color: #008000;">&#40;</span>firstItem<span style="color: #008000;">.</span><span style="color: #0000FF;">Key</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            currentObject <span style="color: #008000;">=</span> firstItem<span style="color: #008000;">.</span><span style="color: #0000FF;">Key</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> propertyName <span style="color: #0600FF; font-weight: bold;">in</span> firstItem<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                currentObject<span style="color: #008000;">.</span><span style="color: #0000FF;">OnPropertyChanged</span><span style="color: #008000;">&#40;</span>propertyName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<h3><a name="usage">Usage</a></h3>
<p>
The solution presented in this post is easy to use:</p>
<ol>
<li>Derive all business objects from <code>BindableObjectBase3</code></li>
<li>Wrap all property getters with a <code>using</code> statement which contains a new instance of the <code>PropertyTracker</code> class created by one of the overloads of <code>GetPropertyTracker</code>
<li>Use one of the overloads of <code>SetValue</code> in the property setters</li>
</ol>
<p>That's it!
</p>
<h3><a name="example">Example</a></h3>
<p>
This sample in this paragraph can also be found in the <a href="#download">code download</a>.
</p>
<p>
Note: in this example the <a href="http://www.pochet.net/blog/2010/06/25/inotifypropertychanged-implementations-an-overview/#lambda">strategy using lambda expressions and expression trees</a> to determine the names of properties is used. All other strategies described <a href="http://www.pochet.net/blog/2010/06/25/inotifypropertychanged-implementations-an-overview/">here</a> could be used too.
</p>
<h4><a name="person">Class Person</a></h4>

<div class="wp_syntax"><div class="code"><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> Person <span style="color: #008000;">:</span> BindableObjectBase3
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> firstName<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> lastName<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> Person<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> firstName, <span style="color: #6666cc; font-weight: bold;">string</span> lastName<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;">FirstName</span> <span style="color: #008000;">=</span> firstName<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">LastName</span> <span style="color: #008000;">=</span> lastName<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;">string</span> FirstName
    <span style="color: #008000;">&#123;</span>
        get
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">using</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;">GetPropertyTracker</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">FirstName</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;">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>
        <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;">SetValue</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">firstName</span>, value, <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">FirstName</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> LastName
        <span style="color: #008000;">&#123;</span>
            get
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">using</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;">GetPropertyTracker</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">LastName</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;">return</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">lastName</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</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;">SetValue</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">lastName</span>, value, <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">LastName</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> FullName
    <span style="color: #008000;">&#123;</span>
        get
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">using</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;">GetPropertyTracker</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">FullName</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;">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: #666666;">&quot; &quot;</span> <span style="color: #008000;">+</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">LastName</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<h4><a name="hellomessage">Class HelloMessage</a></h4>

<div class="wp_syntax"><div class="code"><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> HelloMessage <span style="color: #008000;">:</span> BindableObjectBase3
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> Person person<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> HelloMessage<span style="color: #008000;">&#40;</span>Person person<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;">Person</span> <span style="color: #008000;">=</span> person<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> Person Person
    <span style="color: #008000;">&#123;</span>
        get
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">using</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;">GetPropertyTracker</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Person</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;">return</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">person</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</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;">SetValue</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">person</span>, value, <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Person</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Message
    <span style="color: #008000;">&#123;</span>
        get
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">using</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;">GetPropertyTracker</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Message</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;">return</span> <span style="color: #666666;">&quot;Hello &quot;</span> <span style="color: #008000;">+</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Person</span><span style="color: #008000;">.</span><span style="color: #0000FF;">FullName</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<h4><a name="test">Test</a></h4>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Person person1 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Person<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;E.&quot;</span>, <span style="color: #666666;">&quot;Jongerius&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
person1<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyChanged</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">&#40;</span>s, e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span>
<span style="color: #008000;">&#123;</span>
    Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;   // Raised: person1.PropertyChanged(<span style="color: #008080; font-weight: bold;">\&quot;</span>{0}<span style="color: #008080; font-weight: bold;">\&quot;</span>);&quot;</span>, e<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyName</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
HelloMessage message <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> HelloMessage<span style="color: #008000;">&#40;</span>person1<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
message<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyChanged</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">&#40;</span>s, e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span>
<span style="color: #008000;">&#123;</span>
    Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;   // Raised: message.PropertyChanged(<span style="color: #008080; font-weight: bold;">\&quot;</span>{0}<span style="color: #008080; font-weight: bold;">\&quot;</span>);&quot;</span>, e<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyName</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
person1<span style="color: #008000;">.</span><span style="color: #0000FF;">FirstName</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Emiel&quot;</span><span style="color: #008000;">;</span>
   <span style="color: #008080; font-style: italic;">// Raised: person1.PropertyChanged(&quot;FirstName&quot;);</span>
   <span style="color: #008080; font-style: italic;">// Raised: message.PropertyChanged(&quot;Person&quot;);</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// The following line causes the dependency information</span>
<span style="color: #008080; font-style: italic;">// to be updated</span>
<span style="color: #6666cc; font-weight: bold;">string</span> msg <span style="color: #008000;">=</span> message<span style="color: #008000;">.</span><span style="color: #0000FF;">Message</span><span style="color: #008000;">;</span>
&nbsp;
person1<span style="color: #008000;">.</span><span style="color: #0000FF;">FirstName</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;E.&quot;</span><span style="color: #008000;">;</span>
   <span style="color: #008080; font-style: italic;">// Raised: person1.PropertyChanged(&quot;FirstName&quot;);</span>
   <span style="color: #008080; font-style: italic;">// Raised: person1.PropertyChanged(&quot;FullName&quot;);</span>
   <span style="color: #008080; font-style: italic;">// Raised: message.PropertyChanged(&quot;Person&quot;);</span>
   <span style="color: #008080; font-style: italic;">// Raised: message.PropertyChanged(&quot;Message&quot;);</span>
&nbsp;
Person person2 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Person<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Someone&quot;</span>, <span style="color: #666666;">&quot;Else&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
person2<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyChanged</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">&#40;</span>s, e<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span>
<span style="color: #008000;">&#123;</span>
    Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;   // Raised: person2.PropertyChanged(<span style="color: #008080; font-weight: bold;">\&quot;</span>{0}<span style="color: #008080; font-weight: bold;">\&quot;</span>);&quot;</span>, e<span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyName</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
message<span style="color: #008000;">.</span><span style="color: #0000FF;">Person</span> <span style="color: #008000;">=</span> person2<span style="color: #008000;">;</span>
   <span style="color: #008080; font-style: italic;">// Raised: message.PropertyChanged(&quot;Person&quot;);</span>
   <span style="color: #008080; font-style: italic;">// Raised: message.PropertyChanged(&quot;Message&quot;);</span>
&nbsp;
person1<span style="color: #008000;">.</span><span style="color: #0000FF;">FirstName</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Emiel&quot;</span><span style="color: #008000;">;</span>
   <span style="color: #008080; font-style: italic;">// Raised: person1.PropertyChanged(&quot;FirstName&quot;);</span>
   <span style="color: #008080; font-style: italic;">// Raised: person1.PropertyChanged(&quot;FullName&quot;);</span>
&nbsp;
person2<span style="color: #008000;">.</span><span style="color: #0000FF;">FirstName</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;S.&quot;</span><span style="color: #008000;">;</span>
   <span style="color: #008080; font-style: italic;">// Raised: person2.PropertyChanged(&quot;FirstName&quot;);</span>
   <span style="color: #008080; font-style: italic;">// Raised: person2.PropertyChanged(&quot;FullName&quot;);</span>
   <span style="color: #008080; font-style: italic;">// Raised: message.PropertyChanged(&quot;Person&quot;);</span>
   <span style="color: #008080; font-style: italic;">// Raised: message.PropertyChanged(&quot;Message&quot;);</span></pre></div></div>

<h3><a name="download">Download</a></h3>
<p>
All sources can be downloaded <a href='http://www.pochet.net/blog/wp-content/uploads/2010/07/BindableObjectTest2.zip'>here</a>
</p>
<h3><a name="final">Final words</a></h3>
<p>
The implementation given above has some limitations:</p>
<ul>
<li>The implementation is not theadsafe</li>
<li>It is possible to create object hierarchies that cause endless loops during <code>PropertyChanged</code> events.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.pochet.net/blog/2010/07/02/inotifypropertychanged-automatic-dependent-property-and-nested-object-support/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>INotifyPropertyChanged implementations: An Overview</title>
		<link>http://www.pochet.net/blog/2010/06/25/inotifypropertychanged-implementations-an-overview/</link>
		<comments>http://www.pochet.net/blog/2010/06/25/inotifypropertychanged-implementations-an-overview/#comments</comments>
		<pubDate>Fri, 25 Jun 2010 14:58:07 +0000</pubDate>
		<dc:creator>Emiel</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[INotifyPropertyChanged]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.pochet.net/blog/?p=149</guid>
		<description><![CDATA[Summary One of the cornerstones of MVVM is the INotifyPropertyChanged interface. This post describes several implementations that have been used/proposed over time. Table of contents Introduction Defacto standard Using Lambda Expressions and Expression Trees Using MethodBase.GetCurrentMethod() Using StackTrace Performance comparison Other implementations MSIL weaving Using Proxy objects Additional reading Source download Introduction One of the [...]]]></description>
			<content:encoded><![CDATA[<h3><a name="summary">Summary</a></h3>
<p>
One of the cornerstones of MVVM is the <code>INotifyPropertyChanged</code> interface. This post describes several implementations that have been used/proposed over time.
</p>
<h3><a name="contents">Table of contents</a></h3>
<p>
<ul>
<li><a href="#introduction">Introduction</a></li>
<li><a href="#standard">Defacto standard</a></li>
<li><a href="#lambda">Using Lambda Expressions and Expression Trees</a></li>
<li><a href="#methodbase">Using MethodBase.GetCurrentMethod()</a></li>
<li><a href="#stacktrace">Using StackTrace</a></li>
<li><a href="#performance">Performance comparison</a></li>
<li><a href="#other">Other implementations</a>
<ul>
<li><a href="#msil">MSIL weaving</a></li>
<li><a href="#proxy">Using Proxy objects</a></li>
</ul>
</li>
<li><a href="#reading">Additional reading</a></li>
<li><a href="#download">Source download</a></li>
</ul>
<h3><a name="introduction">Introduction</a></h3>
<p> One of the cornerstones of MVVM is the <code>INotifyPropertyChanged</code> interface. This post describes several implementations that have been used/proposed over time. </p>
<p> The basic ingredient of each <code>INotifyPropertyChanged</code> implementation is the declaration of the <code>PropertyChanged</code> event, and a function to raise this event. We will use the following: </p>

<div class="wp_syntax"><div class="code"><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> BindableObject
<span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">event</span> PropertyChangedEventHandler PropertyChanged<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnPropertyChanged<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> propertyName<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            PropertyChangedEventHandler handler <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyChanged</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>handler <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;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsNullOrWhiteSpace</span><span style="color: #008000;">&#40;</span>propertyName<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;">return</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
&nbsp;
            handler<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span>, <span style="color: #008000;">new</span> PropertyChangedEventArgs<span style="color: #008000;">&#40;</span>propertyName<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
The challenge in implementing the interface is to get the proper value for the <code>propertyName</code> argument of the <code>OnPropertyChanged</code> in a re-factor-friendly way.
</p>
<h3><a name="standard">Defacto standard</a></h3>
<p>
The <a href="http://msdn.microsoft.com/en-us/library/ms743695.aspx">typical defacto standard</a> way of implementing a property is the following:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> useString<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> UseString
<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;">useString</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;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</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;">useString</span>, value<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;">return</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;">useString</span> <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
&nbsp;
		OnPropertyChanged<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;UseString&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></div></div>

<p>
The code in the setter is sometimes re-factored using a generic function like this:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">bool</span> SetValue<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> T property, T value, <span style="color: #6666cc; font-weight: bold;">string</span> propertyName<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: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>property, value<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;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	property <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
&nbsp;
	OnPropertyChanged<span style="color: #008000;">&#40;</span>propertyName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
to this:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> UseString
<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;">useString</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	set
	<span style="color: #008000;">&#123;</span>
		SetValue<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">useString</span>, value, <span style="color: #666666;">&quot;UseString&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></div></div>

<p>
This solution is not re-factor friendly: when you change the name of the property, you also have to change manually the string containing the name of the property. Failing to do this, does not lead to compiler errors, but will certainly lead to bugs that are difficult to track down. To tackle this problem the <code>propertyName</code> parameter of the function <code>OnPropertyChanged</code> <a href="http://joshsmithonwpf.wordpress.com">Josh Smith</a> proposed using the <a href="http://joshsmithonwpf.wordpress.com/2007/08/29/a-base-class-which-implements-inotifypropertychanged/">following function</a>:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Conditional<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;DEBUG&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> VerifyProperty<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> propertyName<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	Type type <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetType</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// Look for a public property with the specified name.</span>
	PropertyInfo propInfo <span style="color: #008000;">=</span> type<span style="color: #008000;">.</span><span style="color: #0000FF;">GetProperty</span><span style="color: #008000;">&#40;</span>propertyName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	Debug<span style="color: #008000;">.</span><span style="color: #0000FF;">Assert</span><span style="color: #008000;">&#40;</span>propInfo <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span>, <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span>CultureInfo<span style="color: #008000;">.</span><span style="color: #0000FF;">InvariantCulture</span>, <span style="color: #666666;">&quot;{0} is not a property of {1}&quot;</span>, propertyName, type<span style="color: #008000;">.</span><span style="color: #0000FF;">FullName</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
This leads to the following implementation of <code>OnPropertyChanged</code>:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnPropertyChanged<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> propertyName<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	VerifyProperty<span style="color: #008000;">&#40;</span>propertyName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	PropertyChangedEventHandler handler <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyChanged</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>handler <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;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsNullOrWhiteSpace</span><span style="color: #008000;">&#40;</span>propertyName<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;">return</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	handler<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span>, <span style="color: #008000;">new</span> PropertyChangedEventArgs<span style="color: #008000;">&#40;</span>propertyName<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
This approach will not lead to compile-time errors when the string containing the property name does not match the actual property name, but it will raise an assertion error in debug builds when <code>OnPropertyChanged</code> is called with non existent property names. However, this approach fails when the a wrong name is used of a property that does exist in the class. For example:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> property1<span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> property2<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Property1
<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;">property1</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
	set <span style="color: #008000;">&#123;</span> SetValue<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">property1</span>, value, <span style="color: #666666;">&quot;Property1&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Property2
<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;">property2</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
	set <span style="color: #008000;">&#123;</span> SetValue<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">property2</span>, value, <span style="color: #666666;">&quot;Property1&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></div></div>

<p>
When copy/paste programming is used, this is likely to happen.
</p>
<h3><a name="lambda">Using Lambda Expressions and Expression Trees</a></h3>
<p>
Refactor friendly <code>INotifyPropertyChanged</code> implementations like <a href="http://www.corvalius.com/blog/index.php/technology/refactoring-friendly-inotifypropertychanged-properties/">this</a> use <a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx">Lambda Expressions</a> and <a href="http://msdn.microsoft.com/en-us/library/bb397951.aspx">Expression Trees</a> to determine the property name using a function like this:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">string</span> GetPropertyNameFromExpression<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>Expression<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span> property<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	var lambda <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>LambdaExpression<span style="color: #008000;">&#41;</span>property<span style="color: #008000;">;</span>
	MemberExpression memberExpression<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>lambda<span style="color: #008000;">.</span><span style="color: #0000FF;">Body</span> <span style="color: #008000;">is</span> UnaryExpression<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		var unaryExpression <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>UnaryExpression<span style="color: #008000;">&#41;</span>lambda<span style="color: #008000;">.</span><span style="color: #0000FF;">Body</span><span style="color: #008000;">;</span>
                memberExpression <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>MemberExpression<span style="color: #008000;">&#41;</span>unaryExpression<span style="color: #008000;">.</span><span style="color: #0000FF;">Operand</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>
		memberExpression <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>MemberExpression<span style="color: #008000;">&#41;</span>lambda<span style="color: #008000;">.</span><span style="color: #0000FF;">Body</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">return</span> memberExpression<span style="color: #008000;">.</span><span style="color: #0000FF;">Member</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
with which the following implementation of <code>OnPropertyChanged</code> can be defined:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnPropertyChanged<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span>Expression<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span> property<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	OnPropertyChanged<span style="color: #008000;">&#40;</span>GetPropertyNameFromExpression<span style="color: #008000;">&#40;</span>property<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
Now we can implement a property as follows:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> useDelegate<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> UseDelegate
<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;">useDelegate</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;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</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;">useDelegate</span>, value<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;">return</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;">useDelegate</span> <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
&nbsp;
		OnPropertyChanged<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> UseDelegate<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
or with the following definition of <code>SetValue</code>
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">bool</span> SetValue<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> T property, T value, Expression<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span> propertyDelegate<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: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>property, value<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;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	property <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
&nbsp;
	OnPropertyChanged<span style="color: #008000;">&#40;</span>propertyDelegate<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
we can rewrite the property definition as follows:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> UseDelegate
<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;">useDelegate</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	set
	<span style="color: #008000;">&#123;</span>
		SetValue<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">useDelegate</span>, value, <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> UseDelegate<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
The upside of this approach is, that it is re-factor friendly: whenever the name of the property is re-factored using a re-factor-tool, the delegate is re-factored too and the correct property name is used in the <code>PropertyChanged</code> event. Another advantage is that misspellings lead to compiler errors, so there is no need for the <code>VerifyProperty</code> function. On the downside: due to the use of reflection, you can experience a performance hit. Furthermore: this approach is also vulnerable to bugs introduced by copy/paste programming like this:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> property1<span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> property2<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Property1
<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;">property1</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
	set <span style="color: #008000;">&#123;</span> SetValue<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">property1</span>, value, <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> Property1<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Property2
<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;">property2</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
	set <span style="color: #008000;">&#123;</span> SetValue<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">property2</span>, value, <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> Property1<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
On a personal note: I do not like the way this approach looks in code. It has some <a href="http://en.wikipedia.org/wiki/Code_smell">code smell</a> about it.
</p>
<h3><a name="methodbase">Using MethodBase.GetCurrentMethod()</a></h3>
<p>
<a href="http://karlshifflett.wordpress.com">Karl Shifflett</a> took another road in <a href="http://karlshifflett.wordpress.com/2009/08/02/inotifypropertychanged-how-to-remove-the-property-name-string-code-smell/">this article</a>: he proposed to get the property name from using <code>MethodBase.GetCurrentMethod()</code>. This leads to the following implementation of a property:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> UseMethodBase
<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;">useString</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	set
	<span style="color: #008000;">&#123;</span>
		SetValue<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">useString</span>, value, MethodBase<span style="color: #008000;">.</span><span style="color: #0000FF;">GetCurrentMethod</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Remove</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>,<span style="color: #FF0000;">4</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
This solution works quite well, although in his article Karl states:<br />
<cite>There could be issues with reliability of reflection for production environment where assemblies can be obfuscated, delayed signed etc.</cite><br />
So probably when using this method, it is wise to leave the <code>VerifyProperty</code> function in place.
</p>
<p>
Because this approach is based upon reflection too, a performance hit is to be expected. Furthermore it is impossible to re-factor the call to <code>MethodBase.GetCurrentMethod()</code> into a helper function, so when using this approach you are stuck using the call to <code>MethodBase.GetCurrentMethod().Name.Remove(0,4)</code> in every property setter.
</p>
<h3><a name="stacktrace">Using StackTrace</a></h3>
<p>
<strong>Important note:</strong> the following approach will not work in optimized release builds, so this approach is probably not suited for production code. This approach is included because it shows a very clean interface.
</p>
<p>
Using the <code>StackTrace</code> class we could determine the name of the property setter using the following function:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">string</span> GetPropertyNameFromStackTrace<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	StackTrace stackTrace <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StackTrace<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>           <span style="color: #008080; font-style: italic;">// get call stack</span>
        StackFrame<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> stackFrames <span style="color: #008000;">=</span> stackTrace<span style="color: #008000;">.</span><span style="color: #0000FF;">GetFrames</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>  <span style="color: #008080; font-style: italic;">// get method calls (frames)</span>
&nbsp;
	<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>StackFrame stackFrame <span style="color: #0600FF; font-weight: bold;">in</span> stackFrames<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
		<span style="color: #6666cc; font-weight: bold;">string</span> name <span style="color: #008000;">=</span> stackFrame<span style="color: #008000;">.</span><span style="color: #0000FF;">GetMethod</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>name<span style="color: #008000;">.</span><span style="color: #0000FF;">StartsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;set_&quot;</span>, StringComparison<span style="color: #008000;">.</span><span style="color: #0000FF;">Ordinal</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;">return</span> name<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">4</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #6666cc; font-weight: bold;">string</span> s <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>StackFrame stackFrame <span style="color: #0600FF; font-weight: bold;">in</span> stackFrames<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		s <span style="color: #008000;">+=</span> <span style="color: #666666;">&quot;   &quot;</span> <span style="color: #008000;">+</span> stackFrame<span style="color: #008000;">.</span><span style="color: #0000FF;">GetMethod</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Name</span> <span style="color: #008000;">+</span> Environment<span style="color: #008000;">.</span><span style="color: #0000FF;">NewLine</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Could not determine property name. StackTrace:<span style="color: #008080; font-weight: bold;">\n</span>&quot;</span> <span style="color: #008000;">+</span> s<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
With this we could create the following implementation of <code>OnPropertyChanged</code>:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnPropertyChanged<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	OnPropertyChanged<span style="color: #008000;">&#40;</span>GetPropertyNameFromStackTrace<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
The property implementation would become
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> useStackTrace<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> UseStackTrace
<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;">useStackTrace</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;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</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;">useStackTrace</span>, value<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;">return</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;">useStackTrace</span> <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
&nbsp;
		OnPropertyChanged<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></div></div>

<p>
or with the following definition of <code>SetValue</code>
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">bool</span> SetValue<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> T property, T value<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: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>property, value<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;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	property <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
&nbsp;
	OnPropertyChanged<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;">return</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
we can rewrite the property definition as follows:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> UseStackTrace
<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;">useStackTrace</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	set
	<span style="color: #008000;">&#123;</span>
		SetValue<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">useStackTrace</span>, value<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
From a development point of view, this is a very neat solution. But unfortunately the <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.stacktrace.aspx">documentation of <code>StackTrace</code></a> states that <cite>Stacktrace might not report as many method calls as expected, due to code transformations that occur during optimization.</cite> For this approach to work at least the following must be true:</p>
<ul>
<li>The name of the property setter must not be mangled for instance by by obfuscation</li>
<li>The setter must not be in-lined</li>
</ul>
<p>
According to <a href="http://blogs.msdn.com/b/davidnotario/archive/2004/11/01/250398.aspx">this article</a> written in 2004, we can prevent in-lining of the setter by applying the <a href="http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.methodimplattribute.aspx"><code>MethodImpl</code></a> attribute like this:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> UseStackTrace
<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;">useStackTrace</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#91;</span>MethodImpl<span style="color: #008000;">&#40;</span>MethodImplOptions<span style="color: #008000;">.</span><span style="color: #0000FF;">NoInlining</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
	set
	<span style="color: #008000;">&#123;</span>
		SetValue<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">ref</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">useStackTrace</span>, value<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>
Unfortunately having to apply this attribute to every setter takes away some of the neatness of this solution.
</p>
<h3><a name="performance">Performance comparison</a></h3>
<p>
Below is the output of a performance test run. The run is made with an optimized release build a release build, without any <code>.pdb</code> files, running from the command line running on Windows 7 in a virtual machine running on Windows XP. The <code>PropertyChanged</code> event handler contained some mathematical calculations to a simulate the actions that are usually performed in the event handler. The source code of the test can be found <a href='http://www.pochet.net/blog/wp-content/uploads/2010/06/BindableObjectTest.zip'>here</a>.
</p>

<div class="wp_syntax"><div class="code"><pre class="console" style="font-family:monospace;">1     times using                 UseString =&gt; 1,9531 ms
1     times using         UseStringVerified =&gt; 0 ms
1     times using             UseMethodBase =&gt; 0,9766 ms
1     times using     UseMethodBaseVerified =&gt; 0 ms
1     times using             UseExpression =&gt; 0 ms
1     times using     UseExpressionVerified =&gt; 0 ms
1     times using             UseStackTrace =&gt; 0 ms
1     times using     UseStackTraceVerified =&gt; 0 ms
&nbsp;
501   times using                 UseString =&gt; 83,9844 ms
501   times using         UseStringVerified =&gt; 88,8672 ms
501   times using             UseMethodBase =&gt; 92,7734 ms
501   times using     UseMethodBaseVerified =&gt; 85,9375 ms
501   times using             UseExpression =&gt; 80,0781 ms
501   times using     UseExpressionVerified =&gt; 78,125 ms
501   times using             UseStackTrace =&gt; 92,7735 ms
501   times using     UseStackTraceVerified =&gt; 88,8671 ms
&nbsp;
1001  times using                 UseString =&gt; 155,2734 ms
1001  times using         UseStringVerified =&gt; 153,3203 ms
1001  times using             UseMethodBase =&gt; 157,2265 ms
1001  times using     UseMethodBaseVerified =&gt; 157,2266 ms
1001  times using             UseExpression =&gt; 160,1563 ms
1001  times using     UseExpressionVerified =&gt; 164,0625 ms
1001  times using             UseStackTrace =&gt; 175,7812 ms
1001  times using     UseStackTraceVerified =&gt; 172,8515 ms
&nbsp;
1501  times using                 UseString =&gt; 226,5625 ms
1501  times using         UseStringVerified =&gt; 227,5391 ms
1501  times using             UseMethodBase =&gt; 246,0938 ms
1501  times using     UseMethodBaseVerified =&gt; 248,0469 ms
1501  times using             UseExpression =&gt; 264,6485 ms
1501  times using     UseExpressionVerified =&gt; 246,0937 ms
1501  times using             UseStackTrace =&gt; 258,7891 ms
1501  times using     UseStackTraceVerified =&gt; 262,6953 ms
&nbsp;
2001  times using                 UseString =&gt; 299,8047 ms
2001  times using         UseStringVerified =&gt; 296,875 ms
2001  times using             UseMethodBase =&gt; 339,8437 ms
2001  times using     UseMethodBaseVerified =&gt; 355,4688 ms
2001  times using             UseExpression =&gt; 300,7812 ms
2001  times using     UseExpressionVerified =&gt; 325,1953 ms
2001  times using             UseStackTrace =&gt; 343,75 ms
2001  times using     UseStackTraceVerified =&gt; 388,6718 ms</pre></div></div>

<p>
Although we can conclude that using reflection incurs a performance hit, this hit is not as dramatic as expected: much less than 0.1 ms per event. This difference will not be noticeable for normal, Silverlight (business) applications, in which normally two to ten property changed events are raised. On the other hand, I agree with <a href="http://karlshifflett.wordpress.com/2009/08/02/inotifypropertychanged-how-to-remove-the-property-name-string-code-smell/">Karl Shifflett</a>: <cite>If you have an application that has 100′s of events per second or more like real-time graphing applications, I would not use this method for performance reasons.  Instead, go with string constants for the property names or double quoted strings if you like that better.</cite>
</p>
<h3><a name="other">Other strategies</a></h3>
<h4><a name="msil">MSIL weaving</a></h4>
<p><a href="http://justinangel.net">Justin Angel</a> has created a <a href="http://justinangel.net/AutomagicallyImplementingINotifyPropertyChanged">solution</a> that implements <code>INotifyPropertyChanged</code> by injecting the necessary code in the property setters using <a href="http://www.mono-project.com/Cecil">Mono.Cecil</a> as well as <a href="http://www.sharpcrafters.com/postsharp">PostSharp 1.5</a>. <a href="http://www.sharpcrafters.com/blog/author/Gael-Fraiteur.aspx">Gael Fraiteur</a> has created a <a href="http://www.sharpcrafters.com/blog/post/introducing-postsharp-2-0-1-notifypropertychanged.aspx">PostSharp 2.0 version</a>. Simon Cropp created <a href="http://code.google.com/p/notifypropertyweaver/">NotifyPropertyWeaver</a> based on the Mono.Cecil implementation of Justin Angel.
</p>
<h4><a name="proxy">Using Proxy objects</a></h4>
<p>
<a href="http://www.ingebrigtsen.info">Einar Ingebrigtsen</a> has proposed <a href="http://www.ingebrigtsen.info/post/2010/01/10/INotifyPropertyChanged-Automagically-implemented.aspx">an implementation</a> based on proxy objects. The downside of using this approach is that the classes cannot be instantiated using the keyword <code>new</code> anymore.
</p>
<p>
<a href="http://www.lostechies.com/blogs/rhouston/default.aspx">Ray Houston</a> used a similar approach <a href="http://www.lostechies.com/blogs/rhouston/archive/2009/06/02/fluent-silverlight-auto-wiring-inotifypropertychanged.aspx">here</a> and <a href="http://jonas.follesoe.no">Jonas Follesø</a> did <a href="http://jonas.follesoe.no/AutomaticChangeNotificationForDependentProperties.aspx">something similar</a> too.
</p>
<h4><a name='aspect'>Using Aspect Oriented Programming</a></h4>
<p>
<a href='http://sachabarber.net'>Sacha Barber</a> has implemented the <code>INotifyPropertyChanged</code> interface using 4 different Aspect Oriented Programming techniques which can be found <a href='http://www.codeproject.com/KB/miscctrl/Aspects.aspx'>in this well written article</a>.
</p>
<h3><a name="reading">Additional reading</a></h3>
<p>
The following links may be helpful:</p>
<ul>
<li><a href="http://joshsmithonwpf.wordpress.com/2007/08/29/a-base-class-which-implements-inotifypropertychanged/">A base class which implements INotifyPropertyChanged</a></li>
<li><a href="http://karlshifflett.wordpress.com/2009/08/02/inotifypropertychanged-how-to-remove-the-property-name-string-code-smell/">Updated – INotifyPropertyChanged – How to remove the Property Name String Code Smell</a></li>
<li><a href="http://www.pochet.net/blog/2010/07/02/inotifypropertychanged-automatic-dependent-property-and-nested-object-support/">INotifyPropertyChanged: automatic dependent property and nested object support</a></li>
</ul>
<h3><a name="download">Source download</a></h3>
<p>The sources for this article can be downloaded <a href='http://www.pochet.net/blog/wp-content/uploads/2010/06/BindableObjectTest.zip'>here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pochet.net/blog/2010/06/25/inotifypropertychanged-implementations-an-overview/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Silverlight DataContext Changed Event and Trigger</title>
		<link>http://www.pochet.net/blog/2010/06/16/silverlight-datacontext-changed-event-and-trigger/</link>
		<comments>http://www.pochet.net/blog/2010/06/16/silverlight-datacontext-changed-event-and-trigger/#comments</comments>
		<pubDate>Wed, 16 Jun 2010 11:38:02 +0000</pubDate>
		<dc:creator>Emiel</dc:creator>
				<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.pochet.net/blog/?p=139</guid>
		<description><![CDATA[Silverlight does not provide a DataContextChanged event. Several solutions have been created for this problem. The solution presented here is based on two attached properties instead of one. The advantage of this solution is that multiple event handlers can be attached and this solution does not force the developer to implement a specific interface like [...]]]></description>
			<content:encoded><![CDATA[<p>Silverlight does not provide a <code>DataContextChanged</code> event. Several solutions have been created for this problem. The solution presented here is based on two attached properties instead of one. The advantage of this solution is that multiple event handlers can be attached and this solution does not force the developer to implement a specific interface like the solution presented <a href="http://www.codeproject.com/Articles/38559/Silverlight-DataContext-Changed-Event.aspx">here</a>.</p>
<p>The heart of the solution consists of the static class <code>DataContextChangedHelper</code>. This class uses the attached property named <code>InheritedDataContext</code> which is bound to the <code>DataContext</code> of the <code>FrameworkElement</code>. Whenever the <code>DataContext</code> is changed, the value of <code>InheritedDataContext</code> (due to the binding) and as a result of that the function <code>OnInheritedDataContextChanged</code> is called. This function call the eventhandlers that are stored in the attached property <code>DataContextChangedHandler</code>.</p>
<p>The public interface of the <code>DataContextChangedHelper</code> consists of the following extension methods to the class <code>FrameworkElement</code>:</p>
<ul>
<li><code>AddDataContextChangedHandler</code></li>
<li><code>RemoveDataContextChangedHandler</code></li>
</ul>
<p>These methods take care of creating and destroying the attached properties.</p>
<p>The class <code>DataContextChangedHelper</code> is defined as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> DataContextChangedHelper
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Identifies the InheritedDataContext DependencyProperty.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> DependencyProperty InheritedDataContextProperty <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;InheritedDataContext&quot;</span>,
            <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#41;</span>,
            <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>DataContextChangedHelper<span style="color: #008000;">&#41;</span>,
            <span style="color: #008000;">new</span> PropertyMetadata<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">null</span>, OnInheritedDataContextChanged<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Handles changes to the InheritedDataContext DependencyProperty.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;d&quot;&gt;Instance with property change.&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;e&quot;&gt;Property change details.&lt;/param&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnInheritedDataContextChanged<span style="color: #008000;">&#40;</span>DependencyObject d, DependencyPropertyChangedEventArgs e<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        EventHandler handler <span style="color: #008000;">=</span> d<span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&#40;</span>DataContextChangedHandlerProperty<span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">as</span> EventHandler<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>handler<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>
            handler<span style="color: #008000;">&#40;</span>d,EventArgs<span style="color: #008000;">.</span><span style="color: #0000FF;">Empty</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Identifies the DataContextChangedHandler DependencyProperty.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> DependencyProperty DataContextChangedHandlerProperty <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;DataContextChangedHandler&quot;</span>,
            <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>EventHandler<span style="color: #008000;">&#41;</span>,
            <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>DataContextChangedHelper<span style="color: #008000;">&#41;</span>,
            <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Adds the data context changed handler.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;element&quot;&gt;Element to which the handler is added&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;handler&quot;&gt;The handler to add&lt;/param&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> AddDataContextChangedHandler<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> FrameworkElement element, EventHandler handler<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>element<span style="color: #008000;">==</span><span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">||</span> handler<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>
            <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>element<span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&#40;</span>InheritedDataContextProperty<span style="color: #008000;">&#41;</span> <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>
            element<span style="color: #008000;">.</span><span style="color: #0000FF;">SetBinding</span><span style="color: #008000;">&#40;</span>InheritedDataContextProperty, <span style="color: #008000;">new</span> Binding<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        EventHandler currentHandler <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>EventHandler<span style="color: #008000;">&#41;</span>element<span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&#40;</span>DataContextChangedHandlerProperty<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        currentHandler <span style="color: #008000;">+=</span> handler<span style="color: #008000;">;</span>
        element<span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span>DataContextChangedHandlerProperty, currentHandler<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;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Removes the data context changed handler.</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;element&quot;&gt;The element from which the handler has to be removed&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;handler&quot;&gt;The handler to remove&lt;/param&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> RemoveDataContextChangedHandler<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> FrameworkElement element, EventHandler handler<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>element <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">||</span> handler <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>
            <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        EventHandler currentHandler <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>EventHandler<span style="color: #008000;">&#41;</span>element<span style="color: #008000;">.</span><span style="color: #0000FF;">GetValue</span><span style="color: #008000;">&#40;</span>DataContextChangedHandlerProperty<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        currentHandler <span style="color: #008000;">-=</span> handler<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>currentHandler <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>
            element<span style="color: #008000;">.</span><span style="color: #0000FF;">ClearValue</span><span style="color: #008000;">&#40;</span>DataContextChangedHandlerProperty<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            element<span style="color: #008000;">.</span><span style="color: #0000FF;">ClearValue</span><span style="color: #008000;">&#40;</span>InheritedDataContextProperty<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>
            element<span style="color: #008000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;">&#40;</span>DataContextChangedHandlerProperty, currentHandler<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Based on the class <code>DataContextChangedHelper</code> a trigger for use in Expression Blend can be defined as follows:</p>

<div class="wp_syntax"><div class="code"><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> DataContextChangedTrigger <span style="color: #008000;">:</span> TriggerBase<span style="color: #008000;">&lt;</span>FrameworkElement<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Called after the trigger is attached to an &lt;see cref=&quot;AssociatedObject&quot;/&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</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;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnAttached</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AssociatedObject</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AddDataContextChangedHandler</span><span style="color: #008000;">&#40;</span>OnDataContextChanged<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;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Called when the trigger is being detached from its &lt;see cref=&quot;AssociatedObject&quot;/&gt;, but before it has actually occurred</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</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> OnDetaching<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;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnDetaching</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AssociatedObject</span><span style="color: #008000;">.</span><span style="color: #0000FF;">RemoveDataContextChangedHandler</span><span style="color: #008000;">&#40;</span>OnDataContextChanged<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;">/// &lt;summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// Called when the DataContext of the &lt;see cref=&quot;AssociatedObject&quot;/&gt; is changed</span>
    <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;s&quot;&gt;&lt;/param&gt;</span>
    <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;e&quot;&gt;&lt;/param&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnDataContextChanged<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> s, 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;">InvokeActions</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;">.</span><span style="color: #0000FF;">DataContext</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></div></div>

<p>This trigger can be used as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;UserControl</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;">xmlns:i</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/expression/2010/interactivity&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:my</span>=<span style="color: #ff0000;">&quot;clr-namespace:TriggerTest&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;i:Interaction.Triggers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;my:DataContextChangedTrigger<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #808080; font-style: italic;">&lt;!-- Actions to perform on DataContextChanged --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/my:DataContextChangedTrigger<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/i:Interaction.Triggers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    ...
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/UserControl<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The code for this post including a sample project can be found <a href='http://www.pochet.net/blog/wp-content/uploads/2010/06/TriggerTest.zip'>here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pochet.net/blog/2010/06/16/silverlight-datacontext-changed-event-and-trigger/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Localization of XAML files in Silverlight</title>
		<link>http://www.pochet.net/blog/2010/06/10/localization-of-xaml-files-in-silverlight/</link>
		<comments>http://www.pochet.net/blog/2010/06/10/localization-of-xaml-files-in-silverlight/#comments</comments>
		<pubDate>Thu, 10 Jun 2010 13:49:19 +0000</pubDate>
		<dc:creator>Emiel</dc:creator>
				<category><![CDATA[Localization]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://www.pochet.net/blog/?p=95</guid>
		<description><![CDATA[This post describes a walk-through of the steps to perform in order to localize XAML files in Silverlight. This post is based on Silverlight 4, but all the steps do also apply to Silverlight 3. The full source code of the resulting project can be downloaded here. The language C# is used throughout this post, [...]]]></description>
			<content:encoded><![CDATA[<p>This post describes a walk-through of the steps to perform in order to localize XAML files in Silverlight. This post is based on Silverlight 4, but all the steps do also apply to Silverlight 3. The full source code of the resulting project can be downloaded <a href='http://www.pochet.net/blog/wp-content/uploads/2010/06/LocalizationWalkThrough.zip'>here</a>. The language C# is used throughout this post, but the steps are easy translatable to VB.NET.</p>
<p>First create a Silverlight application solution using Visual Studio 2010 named <code>LocalizationWalkThrough</code> and close the solution after it has been created. Now open the project file <code>LocalizationWalkThrough\LocalizationWalkThrough.csproj</code> with your favorite text editor, and locate the tag <code>SupportedCultures</code>. Add a comma separated list of the cultures that the application has to support to this tag:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-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;Project</span> ...<span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;PropertyGroup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        ...
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;SupportedCultures<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>en,nl<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/SupportedCultures<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        ...
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/PropertyGroup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    ...
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Project<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>In this example we will use <code>en</code> as the default culture and <code>nl</code> as an additional culture. Next add a resource file to the project for the default culture and one for each additional culture:</p>
<p><a href="http://www.pochet.net/blog/wp-content/uploads/2010/06/AddResources.jpg"><img src="http://www.pochet.net/blog/wp-content/uploads/2010/06/AddResources.jpg" alt="Add resources" width="298" height="193" class="align-center size-full wp-image-97" /></a></p>
<p>and populate the resource files:</p>
<p><a href="http://www.pochet.net/blog/wp-content/uploads/2010/06/FillResources.jpg"><img src="http://www.pochet.net/blog/wp-content/uploads/2010/06/FillResources-300x50.jpg" alt="Fill Resources" title="Fill Resources" width="300" height="50" class="align-center size-medium wp-image-122" /></a></p>
<p>Set the custom tool for the resource of the default culture to <code>PublicResourceCodeGenerator</code>:</p>
<p><a href="http://www.pochet.net/blog/wp-content/uploads/2010/06/ChangeCustomTool.jpg"><img src="http://www.pochet.net/blog/wp-content/uploads/2010/06/ChangeCustomTool-300x143.jpg" alt="Change Custom tool" title="Change Custom Tool" width="300" height="143" class="align-center size-full wp-image-97"/></a></p>
<p>The custom tool <code>PublicResourceCodeGenerator</code> is part of the <a href="http://www.guysmithferrier.com/post/2008/09/Silverlight-And-Strongly-Typed-Resource-Classes.aspx">code generators</a> by <a href="http://www.guysmithferrier.com">Guy Smith Ferrier</a>. A VS2010 installer can be found <a href="http://www.pochet.net/blog/2010/06/07/code-generators-for-generating-strongly-typed-resource-classes-for-visual-studio-2010/">here</a>.</p>
<p>Changing the custom tool to <code>PublicResourceCodeGenerator</code> has the following effects:
<ul>
<li>The generated class for the resources will be public instead of internal</li>
<li>The constructor for the generated class will be public instead of internal</li>
</ul>
<p>The next step is to add the resources to the application resources:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Application</span> <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;">x:Class</span>=<span style="color: #ff0000;">&quot;LocalizationWalkThrough.App&quot;</span></span>
<span style="color: #009900;">             <span style="color: #000066;">xmlns:res</span>=<span style="color: #ff0000;">&quot;clr-namespace:LocalizationWalkThrough.Resources&quot;</span></span>
<span style="color: #009900;">             <span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Application.Resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;res:Strings</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;Strings&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Application.Resources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Application<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>We can now use the resources in XAML:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;UserControl</span> <span style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;LocalizationWalkThrough.MainPage&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;">xmlns:d</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/expression/blend/2008&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:mc</span>=<span style="color: #ff0000;">&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">mc:Ignorable</span>=<span style="color: #ff0000;">&quot;d&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">d:DesignHeight</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">d:DesignWidth</span>=<span style="color: #ff0000;">&quot;400&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;Grid</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;LayoutRoot&quot;</span> <span style="color: #000066;">Background</span>=<span style="color: #ff0000;">&quot;White&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">Text</span>=<span style="color: #ff0000;">&quot;{Binding HelloWorld,Source={StaticResource Strings}}&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;/UserControl<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The final thing left to do is to add a feature for changing the culture. There a numerous possibilities to do this. Here the method of specifying the culture in the querystring of the containing page is used as described <a href="http://www.pochet.net/blog/2010/06/09/passing-parameters-to-a-silverlight-xap-application/">here</a>:</p>

<div class="wp_syntax"><div class="code"><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> Application_Startup<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, StartupEventArgs e<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// Get the ui culture</span>
    <span style="color: #6666cc; font-weight: bold;">string</span> culture<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>HtmlPage<span style="color: #008000;">.</span><span style="color: #0000FF;">Document</span><span style="color: #008000;">.</span><span style="color: #0000FF;">QueryString</span><span style="color: #008000;">.</span><span style="color: #0000FF;">TryGetValue</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;culture&quot;</span>, <span style="color: #0600FF; font-weight: bold;">out</span> culture<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;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsNullOrWhiteSpace</span><span style="color: #008000;">&#40;</span>culture<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;">try</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">// Set the correct UI culture</span>
                Thread<span style="color: #008000;">.</span><span style="color: #0000FF;">CurrentThread</span><span style="color: #008000;">.</span><span style="color: #0000FF;">CurrentUICulture</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> CultureInfo<span style="color: #008000;">&#40;</span>culture<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0600FF; font-weight: bold;">catch</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">// Ignore any errors. We will just continue with the default culture</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">RootVisual</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MainPage<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>And here is the result:</p>
<p><a href="http://www.pochet.net/blog/wp-content/uploads/2010/06/LocalizationResult.jpg"><img src="http://www.pochet.net/blog/wp-content/uploads/2010/06/LocalizationResult-300x230.jpg" alt="Localization Result" title="Localization Result" width="300" height="230" class="align-center size-medium wp-image-127" /></a></p>
<p>As a final optimization the following line could be added to the <code>AssemblyInfo.cs</code> file in the <code>Properties</code> directory:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>assembly<span style="color: #008000;">:</span> NeutralResourcesLanguageAttribute<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;en&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span></pre></div></div>

<p>This line informs the ResourceManager of the neutral culture of an assembly. Look <a href="http://msdn.microsoft.com/en-us/library/system.resources.neutralresourceslanguageattribute.aspx">here</a> for more details.</p>
<p>The full source code of this walk through can be downloaded <a href='http://www.pochet.net/blog/wp-content/uploads/2010/06/LocalizationWalkThrough.zip'>here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pochet.net/blog/2010/06/10/localization-of-xaml-files-in-silverlight/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
