<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Andrew Smith</title>
	<atom:link href="http://agsmith.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://agsmith.wordpress.com</link>
	<description>Random Thoughts about C#, VS, WinForms, WPF and .NET in General</description>
	<lastBuildDate>Mon, 13 May 2013 10:35:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='agsmith.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Andrew Smith</title>
		<link>http://agsmith.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://agsmith.wordpress.com/osd.xml" title="Andrew Smith" />
	<atom:link rel='hub' href='http://agsmith.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Path Data, Prefixes and Known Colors</title>
		<link>http://agsmith.wordpress.com/2008/09/24/path-data-prefixes-and-known-colors/</link>
		<comments>http://agsmith.wordpress.com/2008/09/24/path-data-prefixes-and-known-colors/#comments</comments>
		<pubDate>Thu, 25 Sep 2008 03:01:51 +0000</pubDate>
		<dc:creator>agsmith</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://agsmith.wordpress.com/2008/09/24/path-data-prefixes-and-known-colors/</guid>
		<description><![CDATA[I previously wrote about some fixes I made to the Baml Viewer addin for Reflector. As always happens with these kinds of things, one thing led to another and I ended up finding a few more issues and felt compelled to debug/fix them. Tonight I checked in a few more changes: Xml Namespace Mapping Prefixes [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=agsmith.wordpress.com&#038;blog=2288202&#038;post=40&#038;subd=agsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I <a href="http://agsmith.wordpress.com/2008/09/23/baml-viewer-fixes" target="_blank">previously wrote</a> about some fixes I made to the <a href="http://www.codeplex.com/reflectoraddins/Wiki/View.aspx?title=BamlViewer&amp;referringTitle=Home" target="_blank">Baml Viewer</a> addin for <a href="http://www.red-gate.com/products/reflector/" target="_blank">Reflector</a>. As always happens with these kinds of things, one thing led to another and I ended up finding a few more issues and felt compelled to debug/fix them. Tonight I checked in a few more changes:</p>
<ul>
<li>Xml Namespace Mapping Prefixes &#8211; While debugging the first set of issues I noticed that the xml namespace mappings weren&#8217;t being included. So when a type name was displayed it excluded the prefix &#8211; e.g. ButtonChrome instead of theme:ButtonChrome.
<li>PathData &#8211; A reader posted a comment mentioning that path data was always displayed as &#8220;???&#8221;. I guess this was a placeholder but the functionality was never implemented. This was a bit involved but the baml viewer will now display the <a href="http://msdn.microsoft.com/en-us/library/ms752293.aspx" target="_blank">path markup notation</a> for the path data geometry. Note, the original value won&#8217;t necessarily round trip to the exact original markup but the resulting path should be the same. So if you used m (for a relative move) instead of M (for an absolute move), you may notice that M is output instead. The absolute points were likely calculated when the original path markup was parsed so the output will contain M (i.e. absolute offsets) and the points will be absolute instead of relative.
<li>Known Colors &#8211; Solid color brush values are handled specially by the baml compiler. Previously the only known color that was handled was transparent. All other known colors were output using their ARGB values &#8211; incidentally without the leading # so if you tried to use the xaml it would give you an error. The baml viewer will now map these known colors back to the known color name.</li>
</ul>
<p>I hope I don&#8217;t find any more issues <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  but if you find something interesting I may try to look into it as time allows.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/agsmith.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/agsmith.wordpress.com/40/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=agsmith.wordpress.com&#038;blog=2288202&#038;post=40&#038;subd=agsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://agsmith.wordpress.com/2008/09/24/path-data-prefixes-and-known-colors/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/5a5815e085f302f992834c8429e41166?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">agsmith</media:title>
		</media:content>
	</item>
		<item>
		<title>Baml Viewer Fixes</title>
		<link>http://agsmith.wordpress.com/2008/09/23/baml-viewer-fixes/</link>
		<comments>http://agsmith.wordpress.com/2008/09/23/baml-viewer-fixes/#comments</comments>
		<pubDate>Tue, 23 Sep 2008 13:39:14 +0000</pubDate>
		<dc:creator>agsmith</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://agsmith.wordpress.com/2008/09/23/baml-viewer-fixes/</guid>
		<description><![CDATA[I use Reflector everyday and I&#8217;ve always wanted to give something back to Lutz for making this great utility. One of the things I use within Reflector is the BamlViewer addin but it had some problems correctly displaying all the baml I&#8217;ve tried to view. In some cases it even crashed while parsing the resource. [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=agsmith.wordpress.com&#038;blog=2288202&#038;post=39&#038;subd=agsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I use <a href="http://www.red-gate.com/products/reflector/" target="_blank">Reflector</a> everyday and I&#8217;ve always wanted to give something back to <a href="http://blog.lutzroeder.com/" target="_blank">Lutz</a> for making this great utility. One of the things I use within Reflector is the <a href="http://www.codeplex.com/reflectoraddins/Wiki/View.aspx?title=BamlViewer&amp;referringTitle=Home" target="_blank">BamlViewer addin</a> but it had some problems correctly displaying all the baml I&#8217;ve tried to view. In some cases it even crashed while parsing the resource.</p>
<p>Since the code was in <a href="http://www.codeplex.com/reflectoraddins" target="_blank">CodePlex</a>, I decided to try and fix these issues myself. Having done so I wanted to get these incorporated into the addin so someone else encountering the same issues could get the fixes. In order to check in these changes though you have to be a developer on the project so I contacted Lutz to see if he wanted to check them in. Reflector has <a href="http://blog.lutzroeder.com/2008/08/future-of-net-reflector.html" target="_blank">recently been handed over</a> to <a href="http://www.red-gate.com/" target="_blank">RedGate Software</a> so he referred me to <a href="http://www.red-gate.com/careers/people_profiles/general_manager.htm" target="_blank">James Moore</a> who aside from being a coordinator on that project also heads up the .Net developer tools division at RedGate. He kindly agreed to add me on as a developer to that project so this morning I checked in my fixes.</p>
<p>One more change that I&#8217;d like to make is that I&#8217;d like to get it to support displaying the xml namespace prefix. I have made some changes locally to address this so when I get a chance I&#8217;ll test it out more thoroughly, clean it up and get that checked in as well.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/agsmith.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/agsmith.wordpress.com/39/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=agsmith.wordpress.com&#038;blog=2288202&#038;post=39&#038;subd=agsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://agsmith.wordpress.com/2008/09/23/baml-viewer-fixes/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/5a5815e085f302f992834c8429e41166?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">agsmith</media:title>
		</media:content>
	</item>
		<item>
		<title>Accessing Enum members in Xaml</title>
		<link>http://agsmith.wordpress.com/2008/09/19/accessing-enum-members-in-xaml/</link>
		<comments>http://agsmith.wordpress.com/2008/09/19/accessing-enum-members-in-xaml/#comments</comments>
		<pubDate>Fri, 19 Sep 2008 18:01:51 +0000</pubDate>
		<dc:creator>agsmith</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://agsmith.wordpress.com/2008/09/19/accessing-enum-members-in-xaml/</guid>
		<description><![CDATA[Sacha posted a nice article on how to get friendly names for enums. I&#8217;ve seen this kind of approach before using the Description attribute. One of the things that he does though is create a static method to obtain the list of enum members. You can however actually get the list with pure xaml as [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=agsmith.wordpress.com&#038;blog=2288202&#038;post=35&#038;subd=agsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><a href="http://sachabarber.net/" target="_blank">Sacha</a> posted a <a href="http://www.codeproject.com/KB/WPF/FriendlyEnums.aspx" target="_blank">nice article</a> on how to get friendly names for enums. I&#8217;ve seen this kind of approach before using the Description attribute. One of the things that he does though is create a static method to obtain the list of enum members. You can however actually get the list with pure xaml as follows:</p>
<div style="font-family:monospace;">
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">ObjectDataProvider</span><span style="color:#ff0000;"> MethodName</span><span style="color:#0000ff;">=&#8221;GetValues&#8221;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#ff0000;">ObjectType</span><span style="color:#0000ff;">=&#8221;{</span><span style="color:#a31515;">x</span><span style="color:#0000ff;">:</span><span style="color:#a31515;">Type</span><span style="color:#ff0000;"> sys</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">Enum</span><span style="color:#0000ff;">}&#8221;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#ff0000;">x</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">Key</span><span style="color:#0000ff;">=&#8221;DayOfWeekValues&#8221;&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">&nbsp;&nbsp;&nbsp; </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">ObjectDataProvider.MethodParameters</span><span style="color:#0000ff;">&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">x</span><span style="color:#0000ff;">:</span><span style="color:#a31515;">Type</span><span style="color:#ff0000;"> TypeName</span><span style="color:#0000ff;">=&#8221;sys:DayOfWeek&#8221; /&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">&nbsp;&nbsp;&nbsp; </span><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">ObjectDataProvider.MethodParameters</span><span style="color:#0000ff;">&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">ObjectDataProvider</span><span style="color:#0000ff;">&gt;</span></div>
</div>
<p>And then you can wire it up to a combobox:</p>
<div style="font-family:monospace;">
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">ComboBox</span><span style="color:#ff0000;"> x</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">Name</span><span style="color:#0000ff;">=&#8221;cboDayOfWeek&#8221;</span><span style="color:#ff0000;">&nbsp;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#ff0000;">&nbsp; ItemsSource</span><span style="color:#0000ff;">=&#8221;{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> Source</span><span style="color:#0000ff;">={</span><span style="color:#a31515;">StaticResource</span><span style="color:#ff0000;"> DayOfWeekValues</span><span style="color:#0000ff;">}}&#8221;</span>&nbsp;<span style="color:#0000ff;">/&gt;</span></div>
</div>
<p>The only problem though is that its verbose (as well as a little heavy for something that is just meant to return a static list). When I write a test app for a control I&#8217;m writing I tend to end up having a lot of these and it ends up cluttering the xaml. I was thinking about it and an alternative approach which combines the above as well as takes Sacha&#8217;s issue into account would be to write a markup extension that combines all this together.</p>
<div style="font-family:monospace;">
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp; <span style="color:#808080;">///</span><span style="color:#008000;">&nbsp;</span><span style="color:#808080;">&lt;summary&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp; <span style="color:#808080;">///</span><span style="color:#008000;"> Markup extension that provides a list of the members of a given enum.</span></div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp; <span style="color:#808080;">///</span><span style="color:#008000;">&nbsp;</span><span style="color:#808080;">&lt;/summary&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">public</span>&nbsp;<span style="color:#0000ff;">class</span>&nbsp;<span style="color:#2b91af;">EnumListExtension</span> : <span style="color:#2b91af;">MarkupExtension</span></div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp; {</div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#0000ff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #region</span> Member Variables</div>
<p>&nbsp;
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">private</span>&nbsp;<span style="color:#2b91af;">Type</span> _enumType;</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">private</span>&nbsp;<span style="color:#0000ff;">bool</span> _asString;</div>
<p>&nbsp;
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#0000ff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #endregion</span>&nbsp;<span style="color:#008000;">//Member Variables</span></div>
<p>&nbsp;
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#0000ff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #region</span> Constructor</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#808080;">///</span><span style="color:#008000;">&nbsp;</span><span style="color:#808080;">&lt;summary&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#808080;">///</span><span style="color:#008000;"> Initializes a new </span><span style="color:#808080;">&lt;see cref=&#8221;EnumListExtension&#8221;/&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#808080;">///</span><span style="color:#008000;">&nbsp;</span><span style="color:#808080;">&lt;/summary&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">public</span> EnumListExtension()</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</div>
<p>&nbsp;
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#808080;">///</span><span style="color:#008000;">&nbsp;</span><span style="color:#808080;">&lt;summary&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#808080;">///</span><span style="color:#008000;"> Initializes a new </span><span style="color:#808080;">&lt;see cref=&#8221;EnumListExtension&#8221;/&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#808080;">///</span><span style="color:#008000;">&nbsp;</span><span style="color:#808080;">&lt;/summary&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#808080;">///</span><span style="color:#008000;">&nbsp;</span><span style="color:#808080;">&lt;param name=&#8221;enumType&#8221;&gt;</span><span style="color:#008000;">The type of enum whose members are to be returned.</span><span style="color:#808080;">&lt;/param&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">public</span> EnumListExtension(<span style="color:#2b91af;">Type</span> enumType)</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">this</span>.EnumType = enumType;</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } </div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#0000ff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #endregion</span>&nbsp;<span style="color:#008000;">//Constructor</span></div>
<p>&nbsp;
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#0000ff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #region</span> Properties</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#808080;">///</span><span style="color:#008000;">&nbsp;</span><span style="color:#808080;">&lt;summary&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#808080;">///</span><span style="color:#008000;"> Gets/sets the type of enumeration to return </span></div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#808080;">///</span><span style="color:#008000;">&nbsp;</span><span style="color:#808080;">&lt;/summary&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">public</span>&nbsp;<span style="color:#2b91af;">Type</span> EnumType</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">get</span> { <span style="color:#0000ff;">return</span>&nbsp;<span style="color:#0000ff;">this</span>._enumType; }</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">set</span></div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">if</span> (<span style="color:#0000ff;">value</span> != <span style="color:#0000ff;">this</span>._enumType)</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">if</span> (<span style="color:#0000ff;">null</span> != <span style="color:#0000ff;">value</span>)</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">Type</span> enumType = <span style="color:#2b91af;">Nullable</span>.GetUnderlyingType(<span style="color:#0000ff;">value</span>) ?? <span style="color:#0000ff;">value</span>;</div>
<p>&nbsp;
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">if</span> (enumType.IsEnum == <span style="color:#0000ff;">false</span>)</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">throw</span>&nbsp;<span style="color:#0000ff;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentException</span>(<span style="color:#a31515;">&#8220;Type must be for an Enum.&#8221;</span>);</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</div>
<p>&nbsp;
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">this</span>._enumType = <span style="color:#0000ff;">value</span>;</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</div>
<p>&nbsp;
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#808080;">///</span><span style="color:#008000;">&nbsp;</span><span style="color:#808080;">&lt;summary&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#808080;">///</span><span style="color:#008000;"> Gets/sets a value indicating whether to display the enumeration members as strings using the Description on the member if available.</span></div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#808080;">///</span><span style="color:#008000;">&nbsp;</span><span style="color:#808080;">&lt;/summary&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">public</span>&nbsp;<span style="color:#0000ff;">bool</span> AsString</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">get</span> { <span style="color:#0000ff;">return</span>&nbsp;<span style="color:#0000ff;">this</span>._asString; }</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">set</span> { <span style="color:#0000ff;">this</span>._asString = <span style="color:#0000ff;">value</span>; }</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#0000ff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #endregion</span>&nbsp;<span style="color:#008000;">//Properties</span></div>
<p>&nbsp;
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#0000ff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #region</span> Base class overrides</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#808080;">///</span><span style="color:#008000;">&nbsp;</span><span style="color:#808080;">&lt;summary&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#808080;">///</span><span style="color:#008000;"> Returns a list of items for the specified </span><span style="color:#808080;">&lt;see cref=&#8221;EnumType&#8221;/&gt;</span><span style="color:#008000;">. Depending on the </span><span style="color:#808080;">&lt;see cref=&#8221;AsString&#8221;/&gt;</span><span style="color:#008000;"> property, the </span></div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#808080;">///</span><span style="color:#008000;"> items will be returned as the enum member value or as strings.</span></div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#808080;">///</span><span style="color:#008000;">&nbsp;</span><span style="color:#808080;">&lt;/summary&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#808080;">///</span><span style="color:#008000;">&nbsp;</span><span style="color:#808080;">&lt;param name=&#8221;serviceProvider&#8221;&gt;</span><span style="color:#008000;">An object that provides services for the markup extension.</span><span style="color:#808080;">&lt;/param&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#808080;">///</span><span style="color:#008000;">&nbsp;</span><span style="color:#808080;">&lt;returns&gt;&lt;/returns&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">public</span>&nbsp;<span style="color:#0000ff;">override</span>&nbsp;<span style="color:#0000ff;">object</span> ProvideValue(<span style="color:#2b91af;">IServiceProvider</span> serviceProvider)</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">if</span> (<span style="color:#0000ff;">null</span> == <span style="color:#0000ff;">this</span>._enumType)</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">throw</span>&nbsp;<span style="color:#0000ff;">new</span>&nbsp;<span style="color:#2b91af;">InvalidOperationException</span>(<span style="color:#a31515;">&#8220;The EnumType must be specified.&#8221;</span>);</div>
<p>&nbsp;
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">Type</span> actualEnumType = <span style="color:#2b91af;">Nullable</span>.GetUnderlyingType(<span style="color:#0000ff;">this</span>._enumType) ?? <span style="color:#0000ff;">this</span>._enumType;</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">Array</span> enumValues = <span style="color:#2b91af;">Enum</span>.GetValues(actualEnumType);</div>
<p>&nbsp;
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#008000;">// if the object itself is to be returned then just use GetValues</span></div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#008000;">// </span></div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">if</span> (<span style="color:#0000ff;">this</span>._asString == <span style="color:#0000ff;">false</span>)</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">if</span> (actualEnumType == <span style="color:#0000ff;">this</span>._enumType)</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">return</span> enumValues;</div>
<p>&nbsp;
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">Array</span> tempArray = <span style="color:#2b91af;">Array</span>.CreateInstance(actualEnumType, enumValues.Length + 1);</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; enumValues.CopyTo(tempArray, 1);</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">return</span> tempArray;</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</div>
<p>&nbsp;
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">List</span>&lt;<span style="color:#0000ff;">string</span>&gt; items = <span style="color:#0000ff;">new</span>&nbsp;<span style="color:#2b91af;">List</span>&lt;<span style="color:#0000ff;">string</span>&gt;();</div>
<p>&nbsp;
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">if</span> (actualEnumType != <span style="color:#0000ff;">this</span>._enumType)</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; items.Add(<span style="color:#0000ff;">null</span>);</div>
<p>&nbsp;
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#008000;">// otherwise we must process the list</span></div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">foreach</span> (<span style="color:#0000ff;">object</span> item <span style="color:#0000ff;">in</span>&nbsp;<span style="color:#2b91af;">Enum</span>.GetValues(<span style="color:#0000ff;">this</span>._enumType))</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">string</span> itemString = item.ToString();</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">FieldInfo</span> field = <span style="color:#0000ff;">this</span>._enumType.GetField(itemString);</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">object</span>[] attribs = field.GetCustomAttributes(<span style="color:#0000ff;">typeof</span>(<span style="color:#2b91af;">DescriptionAttribute</span>), <span style="color:#0000ff;">false</span>);</div>
<p>&nbsp;
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">if</span> (<span style="color:#0000ff;">null</span> != attribs &amp;&amp; attribs.Length &gt; 0)</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; itemString = ((<span style="color:#2b91af;">DescriptionAttribute</span>)attribs[0]).Description;</div>
<p>&nbsp;
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; items.Add(itemString);</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</div>
<p>&nbsp;
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">return</span> items.ToArray();</div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#0000ff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #endregion</span>&nbsp;<span style="color:#008000;">//Base class overrides</span></div>
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp; }</div>
</div>
<p>The markup extension has a Type property that can be used to indicate the enum type for which it should obtain the list. If you want it to return strings as opposed to the actual enum values and take the Description attribute into account, you just set the AsString property to true. So now the hookup is much simpler:</p>
<div style="font-family:monospace;">
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">ComboBox</span><span style="color:#ff0000;"> x</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">Name</span><span style="color:#0000ff;">=&#8221;cboDayOfWeek&#8221;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#ff0000;">&nbsp; ItemsSource</span><span style="color:#0000ff;">=&#8221;{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> Source</span><span style="color:#0000ff;">={</span><span style="color:#a31515;">local</span><span style="color:#0000ff;">:</span><span style="color:#a31515;">EnumList</span><span style="color:#0000ff;"> {</span><span style="color:#a31515;">x</span><span style="color:#0000ff;">:</span><span style="color:#a31515;">Type</span><span style="color:#ff0000;"> sys</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">DayOfWeek</span><span style="color:#0000ff;">}}}&#8221;</span>&nbsp;<span style="color:#0000ff;">/&gt;</span></div>
<p>&nbsp; </p></div>
<p><b>Note:</b> I updated this code to use the DisplayName instead of the DescriptionAttribute after <a href="http://mbrownchicago.spaces.live.com/" target="_blank">Mike Brown</a> correctly pointed out that DisplayName is a more appropriate attribute for this scenario.</p>
<p><b>Updated:</b> I reverted back to using the Description attribute since the DisplayNameAttribute&#8217;s usage does not allow its use on Fields. Thanks to John for pointing this out.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/agsmith.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/agsmith.wordpress.com/35/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=agsmith.wordpress.com&#038;blog=2288202&#038;post=35&#038;subd=agsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://agsmith.wordpress.com/2008/09/19/accessing-enum-members-in-xaml/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/5a5815e085f302f992834c8429e41166?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">agsmith</media:title>
		</media:content>
	</item>
		<item>
		<title>Hit Testing in WPF</title>
		<link>http://agsmith.wordpress.com/2008/09/16/hit-testing-in-wpf/</link>
		<comments>http://agsmith.wordpress.com/2008/09/16/hit-testing-in-wpf/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 03:00:20 +0000</pubDate>
		<dc:creator>agsmith</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://agsmith.wordpress.com/2008/09/16/hit-testing-in-wpf/</guid>
		<description><![CDATA[There&#8217;s a lot that can be written about hit testing in WPF so I won&#8217;t try to cover everything but there are some subtleties that bear mentioning. A common issue that I see people encounter is that fact that the default background for many elements is null and null (at least in terms of hit [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=agsmith.wordpress.com&#038;blog=2288202&#038;post=34&#038;subd=agsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>There&#8217;s a lot that can be written about hit testing in WPF so I won&#8217;t try to cover everything but there are some subtleties that bear mentioning. A common issue that I see people encounter is that fact that the default background for many elements is null and null (at least in terms of hit testing) is treated separately from Transparent. Take the following snippet for example.</p>
<div style="font-family:monospace;">
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#8b008b;">&lt;Page</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#8b008b;">&nbsp; </span><span style="color:#ff0000;">xmlns</span><span style="color:#0000ff;">=&#8221;<a href="http://schemas.microsoft.com/winfx/2006/xaml/presentation&#038;#8221" rel="nofollow">http://schemas.microsoft.com/winfx/2006/xaml/presentation&#038;#8221</a>;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#0000ff;">&nbsp; </span><span style="color:#ff0000;">xmlns:x</span><span style="color:#0000ff;">=&#8221;<a href="http://schemas.microsoft.com/winfx/2006/xaml&#038;#8221" rel="nofollow">http://schemas.microsoft.com/winfx/2006/xaml&#038;#8221</a>;</span><span style="color:#8b008b;">&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#8b008b;">&nbsp; &lt;Page.Resources&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#8b008b;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Style </span><span style="color:#ff0000;">TargetType</span><span style="color:#0000ff;">=&#8221;{x:Type Grid}&#8221;</span><span style="color:#8b008b;">&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#8b008b;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Style.Triggers&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#8b008b;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Trigger </span><span style="color:#ff0000;">Property</span><span style="color:#0000ff;">=&#8221;IsMouseOver&#8221; </span><span style="color:#ff0000;">Value</span><span style="color:#0000ff;">=&#8221;True&#8221;</span><span style="color:#8b008b;">&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#8b008b;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Setter </span><span style="color:#ff0000;">Property</span><span style="color:#0000ff;">=&#8221;Background&#8221; </span><span style="color:#ff0000;">Value</span><span style="color:#0000ff;">=&#8221;Yellow&#8221; </span><span style="color:#8b008b;">/&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#8b008b;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Trigger&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#8b008b;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Style.Triggers&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#8b008b;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Style&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#8b008b;">&nbsp; &lt;/Page.Resources&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#8b008b;">&nbsp; &lt;Grid&gt;&nbsp; </span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#8b008b;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;TextBlock </span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#8b008b;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:#ff0000;">Background</span><span style="color:#0000ff;">=&#8221;Red&#8221;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#0000ff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:#ff0000;">Text</span><span style="color:#0000ff;">=&#8221;Test&#8221; </span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#0000ff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:#ff0000;">TextAlignment</span><span style="color:#0000ff;">=&#8221;Center&#8221; </span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#0000ff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:#ff0000;">HorizontalAlignment</span><span style="color:#0000ff;">=&#8221;Center&#8221;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#0000ff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:#ff0000;">VerticalAlignment</span><span style="color:#0000ff;">=&#8221;Center&#8221;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#0000ff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:#ff0000;">Width</span><span style="color:#0000ff;">=&#8221;100&#8243; </span><span style="color:#8b008b;">/&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#8b008b;">&nbsp; &lt;/Grid&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#8b008b;">&lt;/Page&gt;</span></div>
</div>
<div style="font-family:monospace;">&nbsp;</div>
<div style="font-family:monospace;">There is a trigger for the Grid that changes its Background to yellow when IsMouseOver is true. If you run this and move the mouse over the Grid without going over the TextBlock, the background of the Grid will remain the same. However, once you move the mouse over the TextBlock, the background of the Grid will change. Then something interesting happens &#8211; once the mouse leaves the TextBlock buts remains within the Grid the background of the Grid will remain Yellow. This basically comes back to the fact that the default background of the Grid is null. If you were to add a setter to the Style set the Background to Transparent (don&#8217;t set it explicitly or the mouse over won&#8217;t work since you&#8217;ll be providing a local value which will take predence over the style trigger) then the background would have changed to yellow as soon as the mouse entered the Grid.</div>
<div style="font-family:monospace;">&nbsp;</div>
<div style="font-family:monospace;">
<div style="margin-left:4ex;text-indent:-4ex;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#8b008b;">&lt;Style </span><span style="color:#ff0000;">TargetType</span><span style="color:#0000ff;">=&#8221;{x:Type Grid}&#8221;</span><span style="color:#8b008b;">&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#8b008b;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Setter </span><span style="color:#ff0000;">Property</span><span style="color:#0000ff;">=&#8221;Background&#8221; </span><span style="color:#ff0000;">Value</span><span style="color:#0000ff;">=&#8221;Transparent&#8221; </span><span style="color:#8b008b;">/&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#8b008b;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Style.Triggers&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#8b008b;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Trigger </span><span style="color:#ff0000;">Property</span><span style="color:#0000ff;">=&#8221;IsMouseOver&#8221; </span><span style="color:#ff0000;">Value</span><span style="color:#0000ff;">=&#8221;True&#8221;</span><span style="color:#8b008b;">&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#8b008b;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Setter </span><span style="color:#ff0000;">Property</span><span style="color:#0000ff;">=&#8221;Background&#8221; </span><span style="color:#ff0000;">Value</span><span style="color:#0000ff;">=&#8221;Yellow&#8221; </span><span style="color:#8b008b;">/&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#8b008b;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Trigger&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#8b008b;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Style.Triggers&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#8b008b;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/Style&gt;</span></div>
</div>
<p>An interesting thing to note here is that the TextBlock doesn&#8217;t have this issue. So if you were to modify the original source and explicitly set the TextBlock&#8217;s Background to {x:Null} or not set it (since it defaults to null) and rerun the original test, you will find that the background of the Grid still changes to yellow when you go over the TextBlock (and not just when you go over the actual rendered text). The reason that this occurs is because the TextBlock explicitly overrides <a href="http://msdn.microsoft.com/en-us/library/system.windows.uielement.hittestcore.aspx" target="_blank">UIElement.HitTestCore</a> and returns true if the specified point is within its rect. There are actually a few other elements that do this as well including ScrollViewer and InkCanvas. For TextBlock I think the main reason is that you probably don&#8217;t want the IsMouseOver changing as you move across the text in between characters but this is just a guess.</p>
<p>Another property that affects hit testing in WPF is the <a href="http://msdn.microsoft.com/en-us/library/system.windows.uielement.ishittestvisible.aspx" target="_blank">IsHitTestVisible</a>. This property determines if the element and its descendants should be hidden from hit testing. I phrased it in this way because if you set IsHitTestVisible to false on the Grid in the sample above, it wouldn&#8217;t matter what the IsHitTestVisible state of the descendants is set to since the hit test that is performed to evaluate the IsMouseOver state is going to skip the Grid element and not traverse into its children.</p>
<p>Next time I&#8217;ll get to the real reason I started writing about hit testing today &#8211; to discuss the methods available for performing hit testing in code.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/agsmith.wordpress.com/34/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/agsmith.wordpress.com/34/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/agsmith.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/agsmith.wordpress.com/34/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=agsmith.wordpress.com&#038;blog=2288202&#038;post=34&#038;subd=agsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://agsmith.wordpress.com/2008/09/16/hit-testing-in-wpf/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/5a5815e085f302f992834c8429e41166?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">agsmith</media:title>
		</media:content>
	</item>
		<item>
		<title>Getting Started with iTunes</title>
		<link>http://agsmith.wordpress.com/2008/07/19/getting-started-with-itunes/</link>
		<comments>http://agsmith.wordpress.com/2008/07/19/getting-started-with-itunes/#comments</comments>
		<pubDate>Sun, 20 Jul 2008 01:59:44 +0000</pubDate>
		<dc:creator>agsmith</dc:creator>
				<category><![CDATA[Personal]]></category>

		<guid isPermaLink="false">http://agsmith.wordpress.com/2008/07/19/getting-started-with-itunes/</guid>
		<description><![CDATA[I&#8217;ve been thinking about getting an iPod for a while but I&#8217;ve held off for one reason or another. Well my wife got me an iPod Nano for my birthday recently so I&#8217;ve been digging through my cd&#8217;s &#8211; many of which still haven&#8217;t been unpacked since we moved into the house a couple of [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=agsmith.wordpress.com&#038;blog=2288202&#038;post=33&#038;subd=agsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve been thinking about getting an iPod for a while but I&#8217;ve held off for one reason or another. Well my wife got me an iPod Nano for my birthday recently so I&#8217;ve been digging through my cd&#8217;s &#8211; many of which still haven&#8217;t been unpacked since we moved into the house a couple of years ago. </p>
<p>One of the first things I had to do was to install iTunes. I try to be careful about the applications I install on my system. I didn&#8217;t have any worries about malware or anything like that but one of the requirements for iTunes is to install QuickTime. I had gotten a bad taste from previous experiences using QuickTime so I was looking to avoid installing that if possible. Luckily I found some posts like <a href="http://www.mydigitallife.info/2007/07/19/download-and-install-itunes-without-quicktime/" target="_blank">this one</a> that talk about using a QuickTime alternative with iTunes.</p>
<p>One of the things I really liked about the iPod was the cover flow display &#8211; both within iTunes but also within the iPod itself. The problem that I&#8217;ve found though (and apparantly others have as well) is that iTunes sometimes gets the wrong album artwork or fails to find the artwork. I searched around to try and find a solution and thought I had found one named iTunes Art Importer but most of the links I found were broken. When I had finally found the application and installed it, it didn&#8217;t work &#8211; every search I tried returned right away with a message that no matches were found. I did some more searching to try and find another utility when I came across another page where Garett Harnish <a href="http://hyperion.cc.uregina.ca/~harnishg/" target="_blank">modified iTunes Art Importer</a> to work again &#8211; apparantly the original version was written against an older version of the Amazon web service.</p>
<p>I&#8217;m sure there are lots of utilities out there for iTunes but this is all I&#8217;ve needed so far. There&#8217;s even an <a href="http://developer.apple.com/sdk/itunescomsdk.html" target="_blank">SDK</a> for it.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/agsmith.wordpress.com/33/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/agsmith.wordpress.com/33/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/agsmith.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/agsmith.wordpress.com/33/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=agsmith.wordpress.com&#038;blog=2288202&#038;post=33&#038;subd=agsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://agsmith.wordpress.com/2008/07/19/getting-started-with-itunes/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/5a5815e085f302f992834c8429e41166?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">agsmith</media:title>
		</media:content>
	</item>
		<item>
		<title>ElementName Binding In ToolTips (Borrowing a NameScope)</title>
		<link>http://agsmith.wordpress.com/2008/07/17/elementname-binding-in-tooltips-borrowing-a-namescope/</link>
		<comments>http://agsmith.wordpress.com/2008/07/17/elementname-binding-in-tooltips-borrowing-a-namescope/#comments</comments>
		<pubDate>Fri, 18 Jul 2008 03:49:14 +0000</pubDate>
		<dc:creator>agsmith</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://agsmith.wordpress.com/?p=25</guid>
		<description><![CDATA[I had previously seen mention of the limitation that you cannot use ElementName binding within a ToolTip but I never bothered to investigate it since I didn&#8217;t have need to use it. Yesterday Josh asked me about it so I decided to look into it further. I&#8217;m going to deal with ToolTip here since that [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=agsmith.wordpress.com&#038;blog=2288202&#038;post=25&#038;subd=agsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I had previously seen mention of the limitation that you cannot use ElementName binding within a ToolTip but I never bothered to investigate it since I didn&#8217;t have need to use it. Yesterday <a href="http://joshsmithonwpf.wordpress.com/" target="_blank">Josh</a> asked me about it so I decided to look into it further. I&#8217;m going to deal with ToolTip here since that was the one he asked me about but I believe the same technique could apply to ContextMenu.</p>
<p>First let&#8217;s take a look at the various ways you can define a tooltip that uses ElementName in a binding and see which work.</p>
<div style="font-family:monospace;">
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Window.Resources</span><span style="color:#0000ff;">&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">    </span><span style="color:#008000;">&lt;!&#8211; Shared tooltip &#8211;&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">    </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">ToolTip</span><span style="color:#ff0000;"> x</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">Key</span><span style="color:#0000ff;">=&#8221;sharedTT&#8221;&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">        </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">TextBlock</span><span style="color:#ff0000;"> Text</span><span style="color:#0000ff;">=&#8221;{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> ElementName</span><span style="color:#0000ff;">=txt,</span> </div>
<div style="margin-left:4ex;text-indent:-4ex;">           <span style="color:#ff0000;">Path</span><span style="color:#0000ff;">=Text}&#8221; /&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">    </span><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">ToolTip</span><span style="color:#0000ff;">&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Window.Resources</span><span style="color:#0000ff;">&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">DockPanel</span><span style="color:#0000ff;">&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">    </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">TextBox</span><span style="color:#ff0000;"> Text</span><span style="color:#0000ff;">=&#8221;This is the tooltip text&#8221;</span> </div>
<div style="margin-left:4ex;text-indent:-4ex;">            <span style="color:#ff0000;">x</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">Name</span><span style="color:#0000ff;">=&#8221;txt&#8221;</span><span style="color:#ff0000;"> DockPanel.Dock</span><span style="color:#0000ff;">=&#8221;Top&#8221; /&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">    </span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">    </span><span style="color:#008000;">&lt;!&#8211; 1 &#8211; explicitly provide a tooltip instance where </span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#008000;">        the content is bound &#8211;&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">    </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Button</span><span style="color:#ff0000;"> Content</span><span style="color:#0000ff;">=&#8221;Explicit ToolTip&#8221;</span><span style="color:#ff0000;"> DockPanel.Dock</span><span style="color:#0000ff;">=&#8221;Top&#8221; &gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">        </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">ToolTipService.ToolTip</span><span style="color:#0000ff;">&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">            </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">ToolTip</span><span style="color:#ff0000;"> Content</span><span style="color:#0000ff;">=&#8221;{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> ElementName</span><span style="color:#0000ff;">=txt,</span> </div>
<div style="margin-left:4ex;text-indent:-4ex;">               <span style="color:#ff0000;">Path</span><span style="color:#0000ff;">=Text}&#8221; /&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">        </span><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">ToolTipService.ToolTip</span><span style="color:#0000ff;">&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">    </span><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Button</span><span style="color:#0000ff;">&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">    </span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">    </span><span style="color:#008000;">&lt;!&#8211; 2 &#8211; Set the tooltip to a binding &#8211;&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">    </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Button</span><span style="color:#ff0000;"> DockPanel.Dock</span><span style="color:#0000ff;">=&#8221;Top&#8221;</span><span style="color:#ff0000;"> Content</span><span style="color:#0000ff;">=&#8221;Binding&#8221;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;">           <span style="color:#ff0000;">ToolTipService.ToolTip</span><span style="color:#0000ff;">=&#8221;{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> ElementName</span><span style="color:#0000ff;">=txt,</span> </div>
<div style="margin-left:4ex;text-indent:-4ex;">           <span style="color:#ff0000;">Path</span><span style="color:#0000ff;">=Text}&#8221;/&gt;</span></div>
<p> </p>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">    </span><span style="color:#008000;">&lt;!&#8211; 3- Set the tooltip to an object that tries to </span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#008000;">        bind to an element outside the tooltip &#8211;&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">    </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Button</span><span style="color:#ff0000;"> DockPanel.Dock</span><span style="color:#0000ff;">=&#8221;Top&#8221;</span><span style="color:#ff0000;"> Content</span><span style="color:#0000ff;">=&#8221;Element ToolTip&#8221;&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">        </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">ToolTipService.ToolTip</span><span style="color:#0000ff;">&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">            </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">TextBlock</span><span style="color:#ff0000;"> Text</span><span style="color:#0000ff;">=&#8221;{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> ElementName</span><span style="color:#0000ff;">=txt,</span> </div>
<div style="margin-left:4ex;text-indent:-4ex;">               <span style="color:#ff0000;">Path</span><span style="color:#0000ff;">=Text}&#8221; /&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">        </span><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">ToolTipService.ToolTip</span><span style="color:#0000ff;">&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">    </span><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Button</span><span style="color:#0000ff;">&gt;</span></div>
<p> </p>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">    </span><span style="color:#008000;">&lt;!&#8211; 4- Set the tooltip to an element that tries to bind</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#008000;">        to the value of another element within the tooltip &#8211;&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">    </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Button</span><span style="color:#ff0000;"> DockPanel.Dock</span><span style="color:#0000ff;">=&#8221;Top&#8221; &gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">        </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">ToolTipService.ToolTip</span><span style="color:#0000ff;">&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">            </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">StackPanel</span><span style="color:#0000ff;">&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">                </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">TextBlock</span><span style="color:#ff0000;"> x</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">Name</span><span style="color:#0000ff;">=&#8221;ttText&#8221;</span><span style="color:#ff0000;"> Text</span><span style="color:#0000ff;">=&#8221;Foo&#8221; /&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">                </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">TextBlock</span><span style="color:#ff0000;"> Text</span><span style="color:#0000ff;">=&#8221;{</span><span style="color:#a31515;">Binding</span><span style="color:#ff0000;"> ElementName</span><span style="color:#0000ff;">=ttText,</span> </div>
<div style="margin-left:4ex;text-indent:-4ex;">                   <span style="color:#ff0000;">Path</span><span style="color:#0000ff;">=Text}&#8221; /&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">            </span><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">StackPanel</span><span style="color:#0000ff;">&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">        </span><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">ToolTipService.ToolTip</span><span style="color:#0000ff;">&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">        Normal ToolTip</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">    </span><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Button</span><span style="color:#0000ff;">&gt;</span></div>
<p> </p>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">    </span><span style="color:#008000;">&lt;!&#8211; 5 &#8211; Bind to a ToolTip in Resources &#8211;&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">    </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Button</span><span style="color:#ff0000;"> DockPanel.Dock</span><span style="color:#0000ff;">=&#8221;Top&#8221;</span> </div>
<div style="margin-left:4ex;text-indent:-4ex;">           <span style="color:#ff0000;">Content</span><span style="color:#0000ff;">=&#8221;SharedResource ToolTip&#8221;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;">           <span style="color:#ff0000;">ToolTipService.ToolTip</span><span style="color:#0000ff;">=&#8221;{</span><span style="color:#a31515;">StaticResource</span><span style="color:#ff0000;"> sharedTT</span><span style="color:#0000ff;">}&#8221;/&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">DockPanel</span><span style="color:#0000ff;">&gt;</span></div>
</div>
<p>Of all of these the only one that actually works is #2 where the ToolTip property is set to a Binding instance. Suprisingly, at least to me, is that even #4 where we are trying to bind to another element within the tooltip itself does not work.</p>
<p>In order to understand why these aren&#8217;t working, we need to understand how <a href="http://msdn.microsoft.com/en-us/library/system.windows.data.binding.elementname(VS.85).aspx" target="_blank">ElementName</a> binding works. Basically, when an ElementName is used in a binding, the <a href="http://msdn.microsoft.com/en-us/library/ms746659(VS.85).aspx" target="_blank">NameScope</a> of the target object is used to locate the element with the specified name. If that element doesn&#8217;t have a NameScope specifically on it, the FrameworkElement.FindScope method continues up the logical tree and falls back to the <a href="http://blogs.msdn.com/nickkramer/archive/2006/08/18/705116.aspx" target="_blank">inheritance context</a> if there is no logical parent. The name scope is the object in which all named objects have been registered. So in this example, there is a NameScope created for the Window itself implicitly. Other objects also provide a namescope to prevent conflicts between names &#8211; e.g. ControlTemplate and Style. Actually in those cases, the objects themselves implement INameScope.</p>
<p>The ToolTip however is not part of the logical nor visual tree of the element on which it is being set. Instead, it is just the value of a property and as such it doesn&#8217;t have a way to reach the NameScope of the Window in which it was created. The reason that #2 worked is because we just set the value of the ToolTip property to a binding. Since that is a property set directly on the element, that binding has access to the namescope just as you can use in a binding for any other dependency property on the element.</p>
<p>So the issue we have to overcome is how to provide a way for the ToolTip to get to the NameScope of the Window. My solution was to set the NameScope of the ToolTip to a custom INameScope implementation that would get to the NameScope of the element for which the ToolTip was being used. To accomplish this, I defined a new attached property named <strong>BindableToolTip</strong>. When set, it would set the NameScope property of the tooltip (and create a tooltip if the value wasn&#8217;t a tooltip instance such as the case where you are just providing the elements that make up the content of the tooltip) to my custom INameScope implementation that would &#8220;borrow&#8221; the namescope of the element on which the tooltip was being set. I then set the real ToolTipService.ToolTip property to that tooltip instance.</p>
<p>I did hit one glitch along the way. The BamlRecordReader class which is used to process the compiled baml uses a stack to manage the namescopes it encounters. By the time that the BindableToolTip property change is invoked, the BamlRecordReader has already processed the ToolTip instance (through its PushContext method). So when it gets to the point where it wants to clean up its namescope stack (in its PopContext method), it finds that the tooltip now has a namescope and tries to pop an item off its stack which results in an exception because it tries to pop off more items then it pushed. To get around this, I remove the namescope from the tooltip in the Initialized event of the tooltip.</p>
<p>To use the new functionality, you would just replace any place you are setting the ToolTip property with the BindableToolTip property. After doing so with the sample above, every case now works. I&#8217;ve <a href='http://agsmith.files.wordpress.com/2008/07/bindabletooltipzip.doc' title="This is a zip file. Remove the .doc extension when saving the file.">attached the sample project</a> that defines and uses this new attached property.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/agsmith.wordpress.com/25/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/agsmith.wordpress.com/25/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/agsmith.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/agsmith.wordpress.com/25/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=agsmith.wordpress.com&#038;blog=2288202&#038;post=25&#038;subd=agsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://agsmith.wordpress.com/2008/07/17/elementname-binding-in-tooltips-borrowing-a-namescope/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/5a5815e085f302f992834c8429e41166?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">agsmith</media:title>
		</media:content>
	</item>
		<item>
		<title>Who set the DataContext?</title>
		<link>http://agsmith.wordpress.com/2008/07/14/who-set-the-datacontext/</link>
		<comments>http://agsmith.wordpress.com/2008/07/14/who-set-the-datacontext/#comments</comments>
		<pubDate>Mon, 14 Jul 2008 18:25:13 +0000</pubDate>
		<dc:creator>agsmith</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://agsmith.wordpress.com/2008/07/14/who-set-the-datacontext/</guid>
		<description><![CDATA[In the course of the last month or so, several people have asked why the DataContext that they set on the form level wasn&#8217;t carried down deeper into their element tree. Since I haven&#8217;t seen any documentation going into this I&#8217;d like to go over when/why the DataContext may be explicitly set in the WPF [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=agsmith.wordpress.com&#038;blog=2288202&#038;post=24&#038;subd=agsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In the course of the last month or so, several people have asked why the DataContext that they set on the form level wasn&#8217;t carried down deeper into their element tree. Since I haven&#8217;t seen any documentation going into this I&#8217;d like to go over when/why the DataContext may be explicitly set in the WPF framework.</p>
<p>The DataContext is an inherited property defined on FrameworkElement and on FrameworkContextElement (the latter is actually an AddOwner of the former but I&#8217;ll discuss AddOwner another day). Suffice it to say that if you set the DataContext on an element, it should/will get inherited by its descendant elements. So naturally some people will assume that if they set it on their Window/Page, that all elements within that Window/Page will get that DataContext value. Afterall they are not setting it anywhere else. Well, that is the problem. While they may not be setting it, the WPF framework does in certain situations and if you look at it you can understand why. So if anything, including within the WPF framework itself, sets the DataContext on one of the descandants of that Window/Page, then all its descandants will get its locally set DataContext instead of the one set on one of its ancestors.</p>
<p>Ok so when will the WPF framework actually set the DataContext? There are actually two situations that I know of in which a class in the WPF framework will set the DataContext and both relate to when it automatically generates an element for you. One is in the ItemsControl &#8211; or more accurately by the ItemContainerGenerator of an ItemsControl when a container element is generated for an element in the list. The other is in the ContentPresenter when an element is generated for its Content (e.g. based on a DataTemplate). This makes perfect sense since after all the element generated for the ItemsControl and the elements within the DataTemplate need to get access to the thing that that element is supposed to represent.</p>
<p>The reason this may not be noticed in normal usage is because inherited properties prefer the logical tree and therefore can &#8220;skip&#8221; over the elements that have had their DataContext set further up the visual tree between it and its logical parent. So for example, if you were to put a TextBlock into a ListBox:</p>
<div style="font-family:monospace;">
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">    </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Grid</span><span style="color:#ff0000;"> DataContext</span><span style="color:#0000ff;">=&#8221;Foo&#8221;&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">        </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">ListBox</span><span style="color:#0000ff;">&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">            </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">TextBlock</span><span style="color:#ff0000;"> Text</span><span style="color:#0000ff;">=&#8221;{Binding}&#8221; /&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">        </span><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">ListBox</span><span style="color:#0000ff;">&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">    </span><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Grid</span><span style="color:#0000ff;">&gt;</span> </div>
</div>
<p>At runtime, a ListBoxItem would be created to contain the TextBlock within the ListBox. The DataContext of that ListBoxItem is the TextBlock (i.e. the object it represents within the listbox). However, the DataContext of the TextBlock ends up being <em>Foo</em> since that is the DataContext of its logical parent (i.e. the ListBox) and would show &#8220;Foo&#8221; as its Text.</p>
<p>If the TextBlock were not a logical child of the listbox (e.g. if it were added to the listbox via its ItemsSource) then it would inherit the DataContext of its visual parent &#8211; which ultimately would come from the ListBoxItem.</p>
<div style="font-family:monospace;">
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">    </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Grid</span><span style="color:#ff0000;"> DataContext</span><span style="color:#0000ff;">=&#8221;Foo&#8221;&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">        </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">Grid.Resources</span><span style="color:#0000ff;">&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">            </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">x</span><span style="color:#0000ff;">:</span><span style="color:#a31515;">Array</span><span style="color:#ff0000;"> x</span><span style="color:#0000ff;">:</span><span style="color:#ff0000;">Key</span><span style="color:#0000ff;">=&#8221;arr&#8221;</span><span style="color:#ff0000;"> Type</span><span style="color:#0000ff;">=&#8221;sys:Object&#8221;&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">                </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">TextBlock</span><span style="color:#ff0000;"> Text</span><span style="color:#0000ff;">=&#8221;{</span><span style="color:#a31515;">Binding</span><span style="color:#0000ff;">}&#8221; /&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">            </span><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">x</span><span style="color:#0000ff;">:</span><span style="color:#a31515;">Array</span><span style="color:#0000ff;">&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">        </span><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Grid.Resources</span><span style="color:#0000ff;">&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">        </span><span style="color:#0000ff;">&lt;</span><span style="color:#a31515;">ListBox</span><span style="color:#ff0000;"> ItemsSource</span><span style="color:#0000ff;">=&#8221;{</span><span style="color:#a31515;">StaticResource</span><span style="color:#ff0000;"> arr</span><span style="color:#0000ff;">}&#8221; /&gt;</span></div>
<div style="margin-left:4ex;text-indent:-4ex;"><span style="color:#a31515;">    </span><span style="color:#0000ff;">&lt;/</span><span style="color:#a31515;">Grid</span><span style="color:#0000ff;">&gt;</span> </div>
</div>
<p>In this case, the TextBlock would have a Text value of &#8220;System.Windows.Control.TextBlock&#8221; &#8211; the ToString of the TextBlock itself since it is the DataContext of the ListBoxItem and therefore that is its DataContext.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/agsmith.wordpress.com/24/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/agsmith.wordpress.com/24/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/agsmith.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/agsmith.wordpress.com/24/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=agsmith.wordpress.com&#038;blog=2288202&#038;post=24&#038;subd=agsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://agsmith.wordpress.com/2008/07/14/who-set-the-datacontext/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/5a5815e085f302f992834c8429e41166?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">agsmith</media:title>
		</media:content>
	</item>
		<item>
		<title>PropertyDescriptor AddValueChanged Alternative</title>
		<link>http://agsmith.wordpress.com/2008/04/07/propertydescriptor-addvaluechanged-alternative/</link>
		<comments>http://agsmith.wordpress.com/2008/04/07/propertydescriptor-addvaluechanged-alternative/#comments</comments>
		<pubDate>Tue, 08 Apr 2008 02:26:33 +0000</pubDate>
		<dc:creator>agsmith</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://agsmith.wordpress.com/2008/04/07/propertydescriptor-addvaluechanged-alternative/</guid>
		<description><![CDATA[I&#8217;ve been meaning to write about this for a while since I&#8217;ve seen this approach mentioned lots of times on the newsgroups but have seen no mentions about the caveats. Just today I saw the same problem in some code from one of the disciples. The scenario is that you want to know when the [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=agsmith.wordpress.com&#038;blog=2288202&#038;post=23&#038;subd=agsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve been meaning to write about this for a while since I&#8217;ve seen this approach mentioned lots of times on the newsgroups but have seen no mentions about the caveats. Just today I saw the same problem in some code from one of the <a href="http://wpfdisciples.wordpress.com/" target="_blank">disciples</a>. The scenario is that you want to know when the value of a dependency property changes but you don&#8217;t have a one to one relationship with the object. For example, you have a list of items and you want to know when the <strong>IsSelected</strong> property of an item has changed. </p>
<p>The solution that I have seen given for this is to get to the <a href="http://msdn2.microsoft.com/en-us/library/system.componentmodel.propertydescriptor.aspx" target="_blank">PropertyDescriptor</a> and use its <a href="http://msdn2.microsoft.com/en-us/library/system.componentmodel.propertydescriptor.addvaluechanged.aspx" target="_blank">AddValueChanged</a> method to provide an EventHandler to receive a notification when the property has changed. Sometimes, the reply will mention/use <a href="http://msdn2.microsoft.com/en-us/library/system.componentmodel.dependencypropertydescriptor.aspx" target="_blank">DependencyPropertyDescriptor</a> directly but its the same thing since that is just a derived PropertyDescriptor that provides additional information about the underlying DependencyProperty it represents. You&nbsp; can get to this property descriptor in a few ways but the most common are to get it from the <a href="http://msdn2.microsoft.com/en-us/library/system.componentmodel.typedescriptor.getproperties(VS.85).aspx" target="_blank">TypeDescriptor.GetProperties</a> method or using the <a href="http://msdn2.microsoft.com/en-us/library/system.componentmodel.dependencypropertydescriptor.fromproperty.aspx" target="_blank">DependencyPropertyDescriptor.FromProperty</a>. </p>
<p>The issue with this approach is that it will root your object so it will never get collected by the GC. There have been plenty of discussions about how hooking events (particularly static events) can root your object so I won&#8217;t go into great detail there. While it does not seem that you are hooking a static event in this case, in essence you are. When you add a handler to a property descriptor, that property descriptor stores the delegate in a hashtable keyed by the object whose property you are hooking. A delegate/handler is basically a pointer to a method on an object (or no object if its for a static method) so that means the property descriptor has a reference to your object as well as the object whose value you are watching (since that is the key into the hashtable). The property descriptors themselves are cached statically so the hashtable is kept around and therefore your object and the one you are watching are as well.</p>
<p>I personally like to use the <a href="http://memprofiler.com/" target="_blank">Scitech memory profiler</a> (or you can use the <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=A362781C-3870-43BE-8926-862B40AA0CD0&amp;displaylang=en" target="_blank">Microsoft CLR Profiler</a>) when debugging memory leak issues but you can see the issue manifest itself pretty easily in this case. First we&#8217;ll do a benchmark to make sure that we can check whether the object was collected.</p>
<div style="font-family:monospace;">
<div style="margin-left:8ex;text-indent:-4ex;"><span style="color:#2b91af;">ListBoxItem</span> i = <span style="color:#0000ff;">new</span>&nbsp;<span style="color:#2b91af;">ListBoxItem</span>();</div>
<div style="margin-left:8ex;text-indent:-4ex;"><span style="color:#2b91af;">WeakReference</span> wr = <span style="color:#0000ff;">new</span>&nbsp;<span style="color:#2b91af;">WeakReference</span>(i);</div>
<div style="margin-left:8ex;text-indent:-4ex;">i = <span style="color:#0000ff;">null</span>;</div>
<div style="margin-left:8ex;text-indent:-4ex;"><span style="color:#2b91af;">GC</span>.Collect();</div>
<div style="margin-left:8ex;text-indent:-4ex;"><span style="color:#0000ff;">bool</span> isAlive = wr.IsAlive;</div>
</div>
<p>If you run this code and check isAlive, you will see that it returns false indicating that the ListBoxItem was not referenced and was able to be collected. Now let&#8217;s try using the AddValueChanged method.</p>
<div style="font-family:monospace;">
<div style="margin-left:8ex;text-indent:-4ex;"><span style="color:#2b91af;">ListBoxItem</span> i = <span style="color:#0000ff;">new</span>&nbsp;<span style="color:#2b91af;">ListBoxItem</span>();</div>
<div style="margin-left:8ex;text-indent:-4ex;"><span style="color:#2b91af;">WeakReference</span> wr = <span style="color:#0000ff;">new</span>&nbsp;<span style="color:#2b91af;">WeakReference</span>(i);</div>
<div style="margin-left:8ex;text-indent:-4ex;"><span style="color:#2b91af;">PropertyDescriptor</span> prop = <span style="color:#2b91af;">TypeDescriptor</span>.GetProperties(<span style="color:#0000ff;">typeof</span>(<span style="color:#2b91af;">ListBoxItem</span>))[<span style="color:#a31515;">"IsSelected"</span>];</div>
<div style="margin-left:8ex;text-indent:-4ex;"><span style="color:#008000;">// the following yields the same pd</span></div>
<div style="margin-left:8ex;text-indent:-4ex;"><span style="color:#008000;">//PropertyDescriptor prop = DependencyPropertyDescriptor.FromProperty(ListBoxItem.IsSelectedProperty, typeof(ListBoxItem));</span></div>
<div style="margin-left:8ex;text-indent:-4ex;">prop.AddValueChanged(i, <span style="color:#0000ff;">new</span>&nbsp;<span style="color:#2b91af;">EventHandler</span>(<span style="color:#0000ff;">this</span>.OnValueChanged));</div>
<div style="margin-left:8ex;text-indent:-4ex;">i = <span style="color:#0000ff;">null</span>;</div>
<div style="margin-left:8ex;text-indent:-4ex;"><span style="color:#2b91af;">GC</span>.Collect();</div>
<div style="margin-left:8ex;text-indent:-4ex;"><span style="color:#0000ff;">bool</span> isAlive = wr.IsAlive;</div>
</div>
<p>This time isAlive returns true because the property descriptor is maintaining a reference to the ListBoxItem. Now, let&#8217;s try an alternative approach that involves creating a helper class to listen for the property change.</p>
<div style="font-family:monospace;">
<div style="margin-left:8ex;text-indent:-4ex;"><span style="color:#2b91af;">ListBoxItem</span> i = <span style="color:#0000ff;">new</span>&nbsp;<span style="color:#2b91af;">ListBoxItem</span>();</div>
<div style="margin-left:8ex;text-indent:-4ex;"><span style="color:#2b91af;">WeakReference</span> wr = <span style="color:#0000ff;">new</span>&nbsp;<span style="color:#2b91af;">WeakReference</span>(i);</div>
<div style="margin-left:8ex;text-indent:-4ex;"><span style="color:#2b91af;">PropertyChangeNotifier</span> notifier = <span style="color:#0000ff;">new</span>&nbsp;<span style="color:#2b91af;">PropertyChangeNotifier</span>(i, <span style="color:#a31515;">&#8220;IsSelected&#8221;</span>);</div>
<div style="margin-left:8ex;text-indent:-4ex;">notifier.ValueChanged += <span style="color:#0000ff;">new</span>&nbsp;<span style="color:#2b91af;">EventHandler</span>(OnValueChanged);</div>
<div style="margin-left:8ex;text-indent:-4ex;">i = <span style="color:#0000ff;">null</span>;</div>
<div style="margin-left:8ex;text-indent:-4ex;"><span style="color:#2b91af;">GC</span>.Collect();</div>
<div style="margin-left:8ex;text-indent:-4ex;"><span style="color:#0000ff;">bool</span> isAlive = wr.IsAlive;</div>
</div>
<p>In this case isAlive is false indicating that the object can be collected even though we&#8217;re still maintaining an explicit reference to the <em>notifier</em>. The implementation for the class &#8211; <strong>PropertyChangeNotifier</strong> &#8211; is listed below. The class is basically a simple DependencyObject that exposes 2 properties &#8211; <strong>Value</strong> returns the value of the property of the object that it is watching and <strong>PropertySource</strong> returns the object whose property it is watching. The constructor for the object takes the object whose property is to be watched for changes and the property that should be watched. This class takes advantage of the fact that bindings use weak references to manage associations so the class will not root the object who property changes it is watching. It also uses a <a href="http://msdn2.microsoft.com/en-us/library/system.weakreference.aspx" target="_blank">WeakReference</a> to maintain a reference to the object whose property it is watching without rooting that object. In this way, you can maintain a collection of these objects so that you can unhook the property change later without worrying about that collection rooting the object whose values you are watching.</p>
<div style="font-family:monospace;">
<div style="margin-left:8ex;text-indent:-4ex;"><span style="color:#0000ff;">public</span>&nbsp;<span style="color:#0000ff;">sealed</span>&nbsp;<span style="color:#0000ff;">class</span>&nbsp;<span style="color:#2b91af;">PropertyChangeNotifier</span> : </div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#2b91af;">DependencyObject</span>, </div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#2b91af;">IDisposable</span></div>
<div style="margin-left:8ex;text-indent:-4ex;">{</div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#0000ff;">#region</span> Member Variables</div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#0000ff;">private</span>&nbsp;<span style="color:#2b91af;">WeakReference</span> _propertySource;</div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#0000ff;">#endregion</span>&nbsp;<span style="color:#008000;">// Member Variables</span></div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#008000;"></span>&nbsp;</div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#0000ff;">#region</span> Constructor</div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#0000ff;">public</span> PropertyChangeNotifier(<span style="color:#2b91af;">DependencyObject</span> propertySource, <span style="color:#0000ff;">string</span> path)</div>
<div style="margin-left:16ex;text-indent:-4ex;">: <span style="color:#0000ff;">this</span>(propertySource, <span style="color:#0000ff;">new</span>&nbsp;<span style="color:#2b91af;">PropertyPath</span>(path))</div>
<div style="margin-left:12ex;text-indent:-4ex;">{</div>
<div style="margin-left:12ex;text-indent:-4ex;">}</div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#0000ff;">public</span> PropertyChangeNotifier(<span style="color:#2b91af;">DependencyObject</span> propertySource, <span style="color:#2b91af;">DependencyProperty</span> property)</div>
<div style="margin-left:16ex;text-indent:-4ex;">: <span style="color:#0000ff;">this</span>(propertySource, <span style="color:#0000ff;">new</span>&nbsp;<span style="color:#2b91af;">PropertyPath</span>(property))</div>
<div style="margin-left:12ex;text-indent:-4ex;">{</div>
<div style="margin-left:12ex;text-indent:-4ex;">}</div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#0000ff;">public</span> PropertyChangeNotifier(<span style="color:#2b91af;">DependencyObject</span> propertySource, <span style="color:#2b91af;">PropertyPath</span> property)</div>
<div style="margin-left:12ex;text-indent:-4ex;">{</div>
<div style="margin-left:16ex;text-indent:-4ex;"><span style="color:#0000ff;">if</span> (<span style="color:#0000ff;">null</span> == propertySource)</div>
<div style="margin-left:20ex;text-indent:-4ex;"><span style="color:#0000ff;">throw</span>&nbsp;<span style="color:#0000ff;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&#8220;propertySource&#8221;</span>);</div>
<div style="margin-left:16ex;text-indent:-4ex;"><span style="color:#0000ff;">if</span> (<span style="color:#0000ff;">null</span> == property)</div>
<div style="margin-left:20ex;text-indent:-4ex;"><span style="color:#0000ff;">throw</span>&nbsp;<span style="color:#0000ff;">new</span>&nbsp;<span style="color:#2b91af;">ArgumentNullException</span>(<span style="color:#a31515;">&#8220;property&#8221;</span>);</div>
<div style="margin-left:16ex;text-indent:-4ex;"><span style="color:#0000ff;">this</span>._propertySource = <span style="color:#0000ff;">new</span>&nbsp;<span style="color:#2b91af;">WeakReference</span>(propertySource);</div>
<div style="margin-left:16ex;text-indent:-4ex;"><span style="color:#2b91af;">Binding</span> binding = <span style="color:#0000ff;">new</span>&nbsp;<span style="color:#2b91af;">Binding</span>();</div>
<div style="margin-left:16ex;text-indent:-4ex;">binding.Path = property;</div>
<div style="margin-left:16ex;text-indent:-4ex;">binding.Mode = <span style="color:#2b91af;">BindingMode</span>.OneWay;</div>
<div style="margin-left:16ex;text-indent:-4ex;">binding.Source = propertySource;</div>
<div style="margin-left:16ex;text-indent:-4ex;"><span style="color:#2b91af;">BindingOperations</span>.SetBinding(<span style="color:#0000ff;">this</span>, ValueProperty, binding);</div>
<div style="margin-left:12ex;text-indent:-4ex;">} </div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#0000ff;">#endregion</span>&nbsp;<span style="color:#008000;">// Constructor</span></div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#008000;"></span>&nbsp;</div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#0000ff;">#region</span> PropertySource</div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#0000ff;">public</span>&nbsp;<span style="color:#2b91af;">DependencyObject</span> PropertySource</div>
<div style="margin-left:12ex;text-indent:-4ex;">{</div>
<div style="margin-left:16ex;text-indent:-4ex;"><span style="color:#0000ff;">get</span>&nbsp;</div>
<div style="margin-left:16ex;text-indent:-4ex;">{</div>
<div style="margin-left:20ex;text-indent:-4ex;"><span style="color:#0000ff;">try</span></div>
<div style="margin-left:20ex;text-indent:-4ex;">{</div>
<div style="margin-left:24ex;text-indent:-4ex;"><span style="color:#008000;">// note, it is possible that accessing the target property</span></div>
<div style="margin-left:24ex;text-indent:-4ex;"><span style="color:#008000;">// will result in an exception so i&#8217;ve wrapped this check</span></div>
<div style="margin-left:24ex;text-indent:-4ex;"><span style="color:#008000;">// in a try catch</span></div>
<div style="margin-left:24ex;text-indent:-4ex;"><span style="color:#0000ff;">return</span>&nbsp;<span style="color:#0000ff;">this</span>._propertySource.IsAlive</div>
<div style="margin-left:28ex;text-indent:-4ex;">? <span style="color:#0000ff;">this</span>._propertySource.Target <span style="color:#0000ff;">as</span>&nbsp;<span style="color:#2b91af;">DependencyObject</span></div>
<div style="margin-left:28ex;text-indent:-4ex;">: <span style="color:#0000ff;">null</span>;</div>
<div style="margin-left:20ex;text-indent:-4ex;">}</div>
<div style="margin-left:20ex;text-indent:-4ex;"><span style="color:#0000ff;">catch</span></div>
<div style="margin-left:20ex;text-indent:-4ex;">{</div>
<div style="margin-left:24ex;text-indent:-4ex;"><span style="color:#0000ff;">return</span>&nbsp;<span style="color:#0000ff;">null</span>;</div>
<div style="margin-left:20ex;text-indent:-4ex;">}</div>
<div style="margin-left:16ex;text-indent:-4ex;">}</div>
<div style="margin-left:12ex;text-indent:-4ex;">} </div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#0000ff;">#endregion</span>&nbsp;<span style="color:#008000;">// PropertySource</span></div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#008000;"></span>&nbsp;</div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#0000ff;">#region</span> Value</div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#808080;">///</span><span style="color:#008000;">&nbsp;</span><span style="color:#808080;">&lt;summary&gt;</span></div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#808080;">///</span><span style="color:#008000;"> Identifies the </span><span style="color:#808080;">&lt;see cref=&#8221;Value&#8221;/&gt;</span><span style="color:#008000;"> dependency property</span></div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#808080;">///</span><span style="color:#008000;">&nbsp;</span><span style="color:#808080;">&lt;/summary&gt;</span></div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#0000ff;">public</span>&nbsp;<span style="color:#0000ff;">static</span>&nbsp;<span style="color:#0000ff;">readonly</span>&nbsp;<span style="color:#2b91af;">DependencyProperty</span> ValueProperty = <span style="color:#2b91af;">DependencyProperty</span>.Register(<span style="color:#a31515;">&#8220;Value&#8221;</span>,</div>
<div style="margin-left:16ex;text-indent:-4ex;"><span style="color:#0000ff;">typeof</span>(<span style="color:#0000ff;">object</span>), <span style="color:#0000ff;">typeof</span>(<span style="color:#2b91af;">PropertyChangeNotifier</span>), <span style="color:#0000ff;">new</span>&nbsp;<span style="color:#2b91af;">FrameworkPropertyMetadata</span>(<span style="color:#0000ff;">null</span>, <span style="color:#0000ff;">new</span>&nbsp;<span style="color:#2b91af;">PropertyChangedCallback</span>(OnPropertyChanged)));</div>
<div style="margin-left:16ex;text-indent:-4ex;">&nbsp;</div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#0000ff;">private</span>&nbsp;<span style="color:#0000ff;">static</span>&nbsp;<span style="color:#0000ff;">void</span> OnPropertyChanged(<span style="color:#2b91af;">DependencyObject</span> d, <span style="color:#2b91af;">DependencyPropertyChangedEventArgs</span> e)</div>
<div style="margin-left:12ex;text-indent:-4ex;">{</div>
<div style="margin-left:16ex;text-indent:-4ex;"><span style="color:#2b91af;">PropertyChangeNotifier</span> notifier = (<span style="color:#2b91af;">PropertyChangeNotifier</span>)d;</div>
<div style="margin-left:16ex;text-indent:-4ex;"><span style="color:#0000ff;">if</span> (<span style="color:#0000ff;">null</span> != notifier.ValueChanged)</div>
<div style="margin-left:20ex;text-indent:-4ex;">notifier.ValueChanged(notifier, <span style="color:#2b91af;">EventArgs</span>.Empty);</div>
<div style="margin-left:12ex;text-indent:-4ex;">}</div>
<div style="margin-left:12ex;text-indent:-4ex;">&nbsp;</div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#808080;">///</span><span style="color:#008000;">&nbsp;</span><span style="color:#808080;">&lt;summary&gt;</span></div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#808080;">///</span><span style="color:#008000;"> Returns/sets the value of the property</span></div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#808080;">///</span><span style="color:#008000;">&nbsp;</span><span style="color:#808080;">&lt;/summary&gt;</span></div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#808080;">///</span><span style="color:#008000;">&nbsp;</span><span style="color:#808080;">&lt;seealso cref=&#8221;ValueProperty&#8221;/&gt;</span></div>
<div style="margin-left:12ex;text-indent:-4ex;">[<span style="color:#2b91af;">Description</span>(<span style="color:#a31515;">"Returns/sets the value of the property"</span>)]</div>
<div style="margin-left:12ex;text-indent:-4ex;">[<span style="color:#2b91af;">Category</span>(<span style="color:#a31515;">"Behavior"</span>)]</div>
<div style="margin-left:12ex;text-indent:-4ex;">[<span style="color:#2b91af;">Bindable</span>(<span style="color:#0000ff;">true</span>)]</div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#0000ff;">public</span>&nbsp;<span style="color:#0000ff;">object</span> Value</div>
<div style="margin-left:12ex;text-indent:-4ex;">{</div>
<div style="margin-left:16ex;text-indent:-4ex;"><span style="color:#0000ff;">get</span></div>
<div style="margin-left:16ex;text-indent:-4ex;">{</div>
<div style="margin-left:20ex;text-indent:-4ex;"><span style="color:#0000ff;">return</span> (<span style="color:#0000ff;">object</span>)<span style="color:#0000ff;">this</span>.GetValue(<span style="color:#2b91af;">PropertyChangeNotifier</span>.ValueProperty);</div>
<div style="margin-left:16ex;text-indent:-4ex;">}</div>
<div style="margin-left:16ex;text-indent:-4ex;"><span style="color:#0000ff;">set</span></div>
<div style="margin-left:16ex;text-indent:-4ex;">{</div>
<div style="margin-left:20ex;text-indent:-4ex;"><span style="color:#0000ff;">this</span>.SetValue(<span style="color:#2b91af;">PropertyChangeNotifier</span>.ValueProperty, <span style="color:#0000ff;">value</span>);</div>
<div style="margin-left:16ex;text-indent:-4ex;">}</div>
<div style="margin-left:12ex;text-indent:-4ex;">}</div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#0000ff;">#endregion</span>&nbsp;<span style="color:#008000;">//Value</span></div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#008000;"></span>&nbsp;</div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#0000ff;">#region</span> Events</div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#0000ff;">public</span>&nbsp;<span style="color:#0000ff;">event</span>&nbsp;<span style="color:#2b91af;">EventHandler</span> ValueChanged;</div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#0000ff;">#endregion</span>&nbsp;<span style="color:#008000;">// Events</span></div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#008000;"></span>&nbsp;</div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#0000ff;">#region</span> IDisposable Members</div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#0000ff;">public</span>&nbsp;<span style="color:#0000ff;">void</span> Dispose()</div>
<div style="margin-left:12ex;text-indent:-4ex;">{</div>
<div style="margin-left:16ex;text-indent:-4ex;"><span style="color:#2b91af;">BindingOperations</span>.ClearBinding(<span style="color:#0000ff;">this</span>, ValueProperty);</div>
<div style="margin-left:12ex;text-indent:-4ex;">}</div>
<div style="margin-left:12ex;text-indent:-4ex;"><span style="color:#0000ff;">#endregion</span></div>
<div style="margin-left:8ex;text-indent:-4ex;">}</div>
</div>
<p>Another possible approach would be to implement your own derived <a href="http://msdn2.microsoft.com/en-us/library/system.windows.weakeventmanager.aspx" target="_blank">WeakEventManager</a> but I&#8217;ll leave that as an exercise for the reader. For those that choose to go this route, there is such a class &#8211; except its internal &#8211; in the PresentationFramework assembly.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/agsmith.wordpress.com/23/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/agsmith.wordpress.com/23/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/agsmith.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/agsmith.wordpress.com/23/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=agsmith.wordpress.com&#038;blog=2288202&#038;post=23&#038;subd=agsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://agsmith.wordpress.com/2008/04/07/propertydescriptor-addvaluechanged-alternative/feed/</wfw:commentRss>
		<slash:comments>31</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/5a5815e085f302f992834c8429e41166?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">agsmith</media:title>
		</media:content>
	</item>
		<item>
		<title>SyncHashtable is not enumerable&#8230;</title>
		<link>http://agsmith.wordpress.com/2008/01/19/synchashtable-is-not-enumerable/</link>
		<comments>http://agsmith.wordpress.com/2008/01/19/synchashtable-is-not-enumerable/#comments</comments>
		<pubDate>Sat, 19 Jan 2008 05:34:37 +0000</pubDate>
		<dc:creator>agsmith</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Debugging]]></category>

		<guid isPermaLink="false">http://agsmith.wordpress.com/2008/01/19/synchashtable-is-not-enumerable/</guid>
		<description><![CDATA[One of the things I really like about working on Mole is that I get to debug some obscure issues that I haven&#8217;t come across before. Today, Karl mentioned that there was an issue when dealing with collections &#8211; specifically with a synchronized hashtable. Mole was dealing with collections using one of the base collection interfaces [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=agsmith.wordpress.com&#038;blog=2288202&#038;post=17&#038;subd=agsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>One of the things I really like about working on <a target="_blank" href="http://www.codeproject.com/KB/macros/MoleForVisualStudioEdit.aspx">Mole</a> is that I get to debug some obscure issues that I haven&#8217;t come across before. Today, <a target="_blank" href="http://karlshifflett.wordpress.com/">Karl</a> mentioned that there was an issue when dealing with collections &#8211; specifically with a synchronized hashtable. Mole was dealing with collections using one of the base collection interfaces &#8211; ICollection. He fixed the issue correctly by iterating the IDictionaryEnumerator you get from the IDictionary implementations of a hashtable but I wanted to see what was go on so I decided to look into this further.</p>
<p>The problem can be reproduced with the following snippet:</p>
<div style="font-size:10pt;background:white;color:black;font-family:Courier New;text-align:left;">
<p style="margin:0;"><span style="color:#2b91af;">Hashtable</span> ht = <span style="color:blue;">new</span> <span style="color:#2b91af;">Hashtable</span>();</p>
<p style="margin:0;"><span style="color:#2b91af;">Hashtable</span> syncedTable = <span style="color:#2b91af;">Hashtable</span>.Synchronized(ht);</p>
<p style="margin:0;"><span style="color:#2b91af;">IEnumerator</span> enumerator = ((<span style="color:#2b91af;">ICollection</span>)syncedTable).GetEnumerator();</p>
</div>
<p><!--EndFragment-->Or more likely it would be something like this:</p>
<div style="font-size:10pt;background:white;color:black;font-family:Courier New;text-align:left;">
<p style="margin:0;"><span style="color:#2b91af;">Hashtable</span> ht = <span style="color:blue;">new</span> <span style="color:#2b91af;">Hashtable</span>();</p>
<p style="margin:0;"><span style="color:#2b91af;">Hashtable</span> syncedTable = <span style="color:#2b91af;">Hashtable</span>.Synchronized(ht);</p>
<p style="margin:0;">syncedTable.Add(<span style="color:#a31515;">&#8220;Foo&#8221;</span>, <span style="color:#a31515;">&#8220;Bar&#8221;</span>);</p>
<p style="margin:0;">EnumerateCollection(syncedTable);</p>
</div>
<p><!--EndFragment-->Where EnumerateCollection was something like the following:</p>
<div style="font-size:10pt;background:white;color:black;font-family:Courier New;text-align:left;">
<p style="margin:0;"><span style="color:blue;">private</span> <span style="color:blue;">void</span> EnumerateCollection(<span style="color:#2b91af;">IEnumerable</span> enumerable)</p>
<p style="margin:0;">{</p>
<p style="margin:0;">    <span style="color:blue;">foreach</span> (<span style="color:blue;">object</span> value <span style="color:blue;">in</span> enumerable)</p>
<p style="margin:0;">    {</p>
<p style="margin:0;">        <span style="color:green;">// do something</span></p>
<p style="margin:0;">    }</p>
<p style="margin:0;">}</p>
</div>
<p><!--EndFragment-->I took at a look at MS&#8217; code for the Hashtable class using <a target="_blank" href="http://www.aisto.com/roeder/dotnet/">Reflector</a> and there is an obvious bug in their implementation. I did a quick internet search and it seems like people have been bitten by this same issue for a while so its hard to believe that it still exists within the framework today. In case anyone encounters the issue, I&#8217;ll explain what&#8217;s going on.</p>
<p>Hashtable implements the IDictionary interface which derives from ICollection and therefore IEnumerable. Both IDictionary and IEnumerable define a GetEnumerator method. For IDictionary this returns an IDictionaryEnumerator and for IEnumerable this returns an IEnumerator. Hashtable&#8217;s implementation of the IDictionary method is a public virtual GetEnumerator method. Its implementation of the IEnumerable method is an explicit implementation of the interface method.</p>
<p>e.g. <font color="#006018">IEnumerator</font> <font color="#006018">IEnumerable</font>.<b><font color="#000000">GetEnumerator</font></b>();</p>
<p>The Hashtable class exposes a public static method named <strong>Synchronize</strong> that is supposed to take a hashtable and return a thread safe hashtable. There are actually flaws in the thread safety aspect of the implementation but that is another matter outside the issue we were dealing with. The Synchronize methods returns an instance of a private nested class named SyncHashtable which derives from Hashtable and is basically a thin wrapper around the Hashtable you pass to the Synchronize method. SyncHashtable overrides the virtual methods on the base Hashtable class and attempts to make dealing with the hashtable thread safe by using locks around edits.</p>
<p>SyncHashtable correctly overrides the public virtual GetEnumerator method and returns the IDictionaryEnumerator of the hashtable its wrapping. However, and herein lies the problem, it does not reimplement the IEnumerable.GetEnumerator method. Therefore, you get the base Hashtable&#8217;s implementation which returns an enumerator (HashtableEnumerator) for the SyncHashtable itself. This enumerator class assumes that the <em>buckets</em> member variable of the Hashtable it is to enumerate is non-null but the SyncHashtable specifically uses an overload of the Hashtable constructor that does not initialize this member (and others normally initialized for a Hashtable). It makes sense that they would not want to initialize these values since the SyncHashtable doesn&#8217;t store its own values &#8211; remember its just a thin wrapper around the Hashtable you provided to the Synchronize method &#8211; but then they should have reimplemented the IEnumerable.GetEnumerator method.</p>
<p>So if you&#8217;re writing any code that enumerates collections using the IEnumerable interface (directly or indirectly) with an object where you&#8217;re not in control of the source, you may want to consider dealing with the IDictionary&#8217;s enumerator first.</p>
<p>So you could fix the EnumeratorCollection method above as follows:</p>
<div style="font-size:10pt;background:white;color:black;font-family:Courier New;text-align:left;">
<p style="margin:0;"><span style="color:blue;">private</span> <span style="color:blue;">void</span> EnumerateCollection(<span style="color:#2b91af;">IEnumerable</span> enumerable)</p>
<p style="margin:0;">{</p>
<p style="margin:0;">    <span style="color:#2b91af;">IEnumerator</span> enumerator;</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">    <span style="color:green;">// SyncHashtable has a bug where its IEnumerable </span></p>
<p style="margin:0;">    <span style="color:green;">// returns an enumerator whose ctor causes a crash. </span></p>
<p style="margin:0;">    <span style="color:green;">// instead, </span><span style="color:green;">we can get around the problem using its </span></p>
<p style="margin:0;">    <span style="color:green;">// IDictionary.GetEnumerator impl</span></p>
<p style="margin:0;">    <span style="color:#2b91af;">Hashtable</span> table = enumerable <span style="color:blue;">as</span> <span style="color:#2b91af;">Hashtable</span>;</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">    <span style="color:blue;">if</span> (<span style="color:blue;">null</span> != table)</p>
<p style="margin:0;">        enumerator = table.GetEnumerator();</p>
<p style="margin:0;">    <span style="color:blue;">else</span></p>
<p style="margin:0;">        enumerator = enumerable.GetEnumerator();</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">    <span style="color:blue;">while</span> (enumerator.MoveNext())</p>
<p style="margin:0;">    {</p>
<p style="margin:0;">        <span style="color:blue;">object</span> value = enumerator.Current;</p>
<p style="margin:0;">&nbsp;</p>
<p style="margin:0;">        <span style="color:green;">// do something</span></p>
<p style="margin:0;">    }</p>
<p style="margin:0;">}</p>
</div>
<p><!--EndFragment--></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/agsmith.wordpress.com/17/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/agsmith.wordpress.com/17/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/agsmith.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/agsmith.wordpress.com/17/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=agsmith.wordpress.com&#038;blog=2288202&#038;post=17&#038;subd=agsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://agsmith.wordpress.com/2008/01/19/synchashtable-is-not-enumerable/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/5a5815e085f302f992834c8429e41166?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">agsmith</media:title>
		</media:content>
	</item>
		<item>
		<title>Compare Property &amp; Field Values with Mole</title>
		<link>http://agsmith.wordpress.com/2008/01/15/compare-property-field-values-with-mole/</link>
		<comments>http://agsmith.wordpress.com/2008/01/15/compare-property-field-values-with-mole/#comments</comments>
		<pubDate>Wed, 16 Jan 2008 02:29:37 +0000</pubDate>
		<dc:creator>agsmith</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[WinForms]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://agsmith.wordpress.com/2008/01/15/compare-property-field-values-with-mole/</guid>
		<description><![CDATA[As seems to happen with Mole, a suggestion quickly snowballed into a feature implementation. Mole now has the ability to save the members (properties/fields) of the selected element to an xml file and allow you to load that xml file back in to allow property comparison. This will be extremely helpful when you&#8217;re trying to debug a problem [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=agsmith.wordpress.com&#038;blog=2288202&#038;post=16&#038;subd=agsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>As seems to happen with Mole, a suggestion quickly snowballed into a feature implementation. Mole now has the ability to save the members (properties/fields) of the selected element to an xml file and allow you to load that xml file back in to allow property comparison. This will be extremely helpful when you&#8217;re trying to debug a problem where something works for one element but doesn&#8217;t for another instance. There&#8217;s even an option to filter out members with the same value so you can see just the properties that differ.</p>
<p> You can download the latest version <a href="http://www.codeproject.com/KB/macros/MoleForVisualStudioEdit.aspx">here</a>.</p>
<p>In addition, Karl has been posting some videos on YouTube that show how to use Mole. You can view those videos <a href="http://www.youtube.com/profile_videos?user=LittleRichieRules">here</a>.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/agsmith.wordpress.com/16/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/agsmith.wordpress.com/16/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/agsmith.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/agsmith.wordpress.com/16/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=agsmith.wordpress.com&#038;blog=2288202&#038;post=16&#038;subd=agsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://agsmith.wordpress.com/2008/01/15/compare-property-field-values-with-mole/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/5a5815e085f302f992834c8429e41166?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">agsmith</media:title>
		</media:content>
	</item>
	</channel>
</rss>
