<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dr. WPF &#187; Markup Extensions</title>
	<atom:link href="http://drwpf.com/blog/category/markup-extensions/feed/" rel="self" type="application/rss+xml" />
	<link>http://drwpf.com/blog</link>
	<description>Drinking (and serving) the WPF Kool-Aid since 2002</description>
	<lastBuildDate>Sun, 09 May 2010 06:59:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Tips and Tricks: Making Value Converters More Accessible in Markup</title>
		<link>http://drwpf.com/blog/2009/03/17/tips-and-tricks-making-value-converters-more-accessible-in-markup/</link>
		<comments>http://drwpf.com/blog/2009/03/17/tips-and-tricks-making-value-converters-more-accessible-in-markup/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 19:29:00 +0000</pubDate>
		<dc:creator>Dr. WPF</dc:creator>
				<category><![CDATA[Markup]]></category>
		<category><![CDATA[Markup Extensions]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[Value Converters]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://drwpf.com/blog/?p=48</guid>
		<description><![CDATA[
I recently had the pleasure of presenting some rather advanced WPF concepts (along with some cool tips and tricks) to a group of smart folks.  Unfortunately, I ran up against time constraints and was forced to drop the data binding portion of my talk.  I&#8217;m a little bummed about not being able to show one cool [...]]]></description>
			<content:encoded><![CDATA[<div class="jbr">
<p>I recently had the pleasure of presenting some rather advanced WPF concepts (along with some cool tips and tricks) to a group of smart folks.  Unfortunately, I ran up against time constraints and was forced to drop the data binding portion of my talk.  I&#8217;m a little bummed about not being able to show one cool trick that I use a lot, so here it is&#8230;</p>
<p>Call me lazy (really, I&#8217;m okay with it), but I&#8217;ve never been wild about having to declare my value converters as resources before using them.  It&#8217;s just one extra step:</p>
<pre><span class="kwrd">&lt;</span><span class="html"><span style="color: #993300;">Window.Resources</span></span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html"><span style="color: #993300;">src:MyConverter</span></span> <span class="attr"><span style="color: #ff0000;">x:Key</span></span><span class="kwrd">="MyConverter"</span> <span class="kwrd">/&gt;</span>
<span class="kwrd">&lt;/</span><span class="html"><span style="color: #993300;">Window.Resources</span></span><span class="kwrd">&gt;</span></pre>
<p>And then later the converter must be specified using a StaticResource reference, which incurs some overhead (albeit small overhead, in the grand scheme of things) for the resource resolution:</p>
<pre><span class="kwrd">&lt;</span><span style="color: #993300;"><span class="html">TextBlock</span> </span><span class="attr"><span style="color: #ff0000;">Text</span></span><span class="kwrd">="{<span style="color: #993300;">Binding</span> <span style="color: #ff0000;">SomePath</span>, <span style="color: #ff0000;">Converter</span>={<span style="color: #993300;">StaticResource</span> <span style="color: #ff0000;">MyConverter</span>}}"</span> <span class="kwrd">/&gt;</span></pre>
<p>I often choose to skip the middleman.  Instead of using the StaticResource markup extension to look up a converter, I simply derive my value converter, itself, from MarkupExtension.  Then I return an instance of it in the ProvideValue() override.</p>
<p>I have to assume there are plenty of others using this approach too.  (It&#8217;s clever, afterall, but that&#8217;s about the extent of the hyperbole I&#8217;d grant it.)  If you&#8217;re not one of them, I simply wanted to put it out there as a potential trick for your arsenal.</p>
<p>Below is a super simple example of a dummy converter (typically used to debug bindings) that does this:</p>
<pre><span style="color: #0000ff;"><span class="kwrd">public</span> <span class="kwrd">class</span> </span>DummyConverter : <span style="color: #339966;">MarkupExtension</span>, <span style="color: #339966;">IValueConverter</span>
{
    <span style="color: #0000ff;"><span class="kwrd">private</span> <span class="kwrd">static</span> </span><span style="color: #339966;">DummyConverter</span> _converter = <span class="kwrd"><span style="color: #0000ff;">null</span></span>;
    <span style="color: #0000ff;"><span class="kwrd">public</span> <span class="kwrd">override</span> <span class="kwrd">object</span></span> ProvideValue(<span style="color: #339966;">IServiceProvider</span> serviceProvider)
    {
        <span class="kwrd">if</span> (_converter == <span class="kwrd"><span style="color: #0000ff;">null</span></span>)
        {
            _converter = <span class="kwrd"><span style="color: #0000ff;">new</span></span> DummyConverter();
        }
        <span class="kwrd"><span style="color: #0000ff;">return</span></span> _converter;
    }
    <span class="codeproc">#<span style="color: #0000ff;">region</span></span> IValueConverter Members
    <span style="color: #0000ff;"><span class="kwrd">public</span> <span class="kwrd">object</span></span> Convert(<span class="kwrd"><span style="color: #0000ff;">object</span></span> <span class="kwrd">value</span>, <span style="color: #339966;">Type</span> targetType, <span class="kwrd"><span style="color: #0000ff;">object</span></span> parameter,
        <span style="color: #339966;">CultureInfo</span> culture)
    {
        <span style="color: #0000ff;"><span class="kwrd">return</span> </span><span class="kwrd">value</span>; <span style="color: #00ff00;"><span class="rem"><span style="color: #009933;">// set breakpoint here to debug your binding</span></span>
</span>    }

    <span style="color: #0000ff;"><span class="kwrd">public</span> <span class="kwrd">object</span></span> ConvertBack(<span class="kwrd"><span style="color: #0000ff;">object</span></span> <span class="kwrd">value</span>, <span style="color: #339966;">Type</span> targetType, <span class="kwrd"><span style="color: #0000ff;">object</span></span> parameter,
        <span style="color: #339966;">CultureInfo</span> culture)
    {
        <span class="kwrd"><span style="color: #0000ff;">return</span></span> <span class="kwrd">value</span>;
    }
    <span class="codeproc">#<span style="color: #0000ff;">endregion</span></span>
}</pre>
<p>The above class creates a singleton dummy converter that can be used across my app without any resource lookup.  The XAML usage is now simply this:</p>
<pre><span class="kwrd">&lt;</span><span style="color: #993300;"><span class="html">TextBlock</span> </span><span class="attr"><span style="color: #ff0000;">Text</span></span><span class="kwrd">="{<span style="color: #993300;">Binding</span> <span style="color: #ff0000;">SomePath</span>, <span style="color: #ff0000;">Converter</span>={<span style="color: #993300;">src:DummyConverter</span>}}"</span> <span class="kwrd">/&gt;</span></pre>
<p>This is much better, to my lazy way of thinking.</p></div>
]]></content:encoded>
			<wfw:commentRss>http://drwpf.com/blog/2009/03/17/tips-and-tricks-making-value-converters-more-accessible-in-markup/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Supplying an Object Reference in the ConstructorParameters Collection of an ObjectDataProvider</title>
		<link>http://drwpf.com/blog/2007/09/02/supplying-an-object-reference-in-the-constructorparameters-collection-of-an-objectdataprovider/</link>
		<comments>http://drwpf.com/blog/2007/09/02/supplying-an-object-reference-in-the-constructorparameters-collection-of-an-objectdataprovider/#comments</comments>
		<pubDate>Sun, 02 Sep 2007 22:29:58 +0000</pubDate>
		<dc:creator>Dr. WPF</dc:creator>
				<category><![CDATA[Markup Extensions]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://drwpf.com/blog/?p=5</guid>
		<description><![CDATA[
Dear Dr. WPF,
I have a .NET 2.0 data collection object that aggregates RSS feeds and maintains the incoming posts in an in-memory collection (as well as saving them to a data store). The class has no associated UI… it just provides the data as well as a few events that can be used to filter [...]]]></description>
			<content:encoded><![CDATA[<div class="jbr">
<p class="MsoNormal"><em style="mso-bidi-font-style: normal">Dear Dr. WPF,</em></p>
<p class="MsoNormal"><em style="mso-bidi-font-style: normal">I have a .NET 2.0 data collection object that aggregates RSS feeds and maintains the incoming posts in an in-memory collection (as well as saving them to a data store). The class has no associated UI… it just provides the data as well as a few events that can be used to filter (accept/reject) posts as they arrive.<span style="mso-spacerun: yes">  </span>I have used it quite successfully in several different applications.</em></p>
<p class="MsoNormal"><em style="mso-bidi-font-style: normal">I recently decided to enhance the class to better support .NET 3.0 scenarios.<span style="mso-spacerun: yes">  </span>As such, the posts are now maintained in an observable collection, which works great for databinding.<span style="mso-spacerun: yes">  </span>But I would also like to support the routed event model and rather than requiring consumers of my object to attach event handlers, like in the 2.0 version, I’d like to have the object raise bubbled events.</em></p>
<p class="MsoNormal"><em style="mso-bidi-font-style: normal">This is where the trouble comes…<span style="mso-spacerun: yes">  </span>As mentioned earlier, the control has no knowledge of the UI.<span style="mso-spacerun: yes">  </span>It just knows about the data.<span style="mso-spacerun: yes">  </span>In order to raise routed events, it needs a target element.<span style="mso-spacerun: yes">  </span>To support this, I’ve created a constructor that accepts a UIElement, and if present, I will raise events on that element.<span style="mso-spacerun: yes">  </span>This works fine when I create my data object in code, but I would like to support creating it in XAML using an ObjectDataProvider.</em></p>
<p class="MsoNormal"><em style="mso-bidi-font-style: normal">Here is what I would like to do:</em></p>
<pre class="MsoNormal" style="mso-layout-grid-align: none"><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">&lt;</span><span style="color: #a31515; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">Grid</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin"> </span><span style="color: red; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">Name</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">=</span><span style="font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">"<span style="color: blue">RssGrid</span>"<span style="color: blue">&gt;
</span></span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin"><span style="mso-spacerun: yes">  </span>&lt;</span><span style="color: #a31515; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">Grid.Resources</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">&gt;
</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin"><span style="mso-spacerun: yes">    </span>&lt;</span><span style="color: #a31515; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">ObjectDataProvider</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin"> </span><span style="color: red; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">x:Key</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">=</span><span style="font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">"<span style="color: blue">RssData</span>"<span style="color: blue"> 
</span></span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin"><span style="mso-spacerun: yes">        </span></span><span style="color: red; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">ObjectType</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">=</span><span style="font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">"<span style="color: blue">{x:Type rss:RssFeeder}</span>"<span style="color: blue">&gt;
</span></span><strong style="mso-bidi-font-weight: normal"><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin"><span style="mso-spacerun: yes">      </span>&lt;</span></strong><strong style="mso-bidi-font-weight: normal"><span style="color: #a31515; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">ObjectDataProvider.ConstructorParameters</span></strong><strong style="mso-bidi-font-weight: normal"><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">&gt;
</span></strong><strong style="mso-bidi-font-weight: normal"><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin"><span style="mso-spacerun: yes">        </span>&lt;</span></strong><strong style="mso-bidi-font-weight: normal"><span style="color: #a31515; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">Binding</span></strong><strong style="mso-bidi-font-weight: normal"><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin"> </span></strong><strong style="mso-bidi-font-weight: normal"><span style="color: red; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">ElementName</span></strong><strong style="mso-bidi-font-weight: normal"><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">=</span></strong><strong style="mso-bidi-font-weight: normal"><span style="font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">"<span style="color: blue">RssGrid</span>"<span style="color: blue"> /&gt;
</span></span></strong><strong style="mso-bidi-font-weight: normal"><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin"><span style="mso-spacerun: yes">     </span><span style="mso-spacerun: yes"> </span>&lt;/</span></strong><strong style="mso-bidi-font-weight: normal"><span style="color: #a31515; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">ObjectDataProvider.ConstructorParameters</span></strong><strong style="mso-bidi-font-weight: normal"><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">&gt;
</span></strong><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin"><span style="mso-spacerun: yes">    </span>&lt;/</span><span style="color: #a31515; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">ObjectDataProvider</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">&gt;
</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin"><span style="mso-spacerun: yes">  </span>&lt;/</span><span style="color: #a31515; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">Grid.Resources</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">&gt;
</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin"><span style="mso-spacerun: yes">  </span>&lt;</span><span style="color: #a31515; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">ListBox</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin"> </span><span style="color: red; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">DataSource</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">=</span><span style="font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">"<span style="color: blue">{StaticResource RssData}</span>"
</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin"><span style="mso-spacerun: yes">      </span></span><span style="color: red; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">ItemsSource</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">=</span><span style="font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">"<span style="color: blue">{Binding Path=RecentPosts}</span>"<span style="color: blue"> </span><span style="color: red">... </span><span style="color: blue">/&gt;
</span></span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">&lt;/</span><span style="color: #a31515; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">Grid</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">&gt;</span></pre>
<p class="MsoNormal"><em style="mso-bidi-font-style: normal">But this doesn’t work because bindings can only target dependency properties.<span style="mso-spacerun: yes">  </span>The ConstructorParameters collection is a CLR property.</em></p>
<p class="MsoNormal"><em style="mso-bidi-font-style: normal">Is there any way to do this in XAML?</em></p>
<p class="MsoNormal"><em style="mso-bidi-font-style: normal">Sincerely,<br />
Marie</em><em style="mso-bidi-font-style: normal"> </em></p>
<div class="MsoNormal" style="text-align: center">
<hr size="2" /></div>
<p class="MsoNormal">Hi Marie,</p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">It’s funny how the same question will pop up all at once from several different sources.<span style="mso-spacerun: yes">  </span>Yours is one such example.<span style="mso-spacerun: yes">  </span>I have received variations of this same question from no less than 4 different people within the last month.<span style="mso-spacerun: yes">  </span>It typically takes one of the following forms:</span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">In markup, how do I pass an instance of an object …</span></p>
<p class="MsoNormal"><span style="line-height: 150%; mso-fareast-font-family: Arial; mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt"><span style="mso-list: Ignore">1.<span style="font-family: Times New Roman;">  </span></span></span><span style="line-height: 150%; mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">in the ConstructorParameters of an ObjectDataProvider?</span></p>
<p class="MsoNormal"><span style="line-height: 150%; mso-fareast-font-family: Arial; mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt"><span style="mso-list: Ignore">2.  </span></span><span style="line-height: 150%; mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">as the ConverterParameter of a Binding?</span></p>
<p class="MsoNormal"><span style="line-height: 150%; mso-fareast-font-family: Arial; mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt"><span style="mso-list: Ignore">3.<span style="font: 7pt &quot;Times New Roman&quot;">  </span></span></span><span style="line-height: 150%; mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">as the value of a CLR property (that is not backed by a dependency property)?</span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">And just last week, I saw <a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2071241&amp;SiteID=1">this version</a> of the same question (very similar to your scenario) in the WPF forum on the MSDN site.<span style="mso-spacerun: yes">  </span>In my response to that post, I said I would blog about an approach that I sometimes use to do this.<span style="mso-spacerun: yes">  </span>I think this solution will work nicely for you also.<span style="mso-spacerun: yes">  </span>You’ll find the promised details below.</span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">Best regards,<br />
</span>Dr. WPF</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">Introducing the ObjectReference Markup Extension</span></strong></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">First, let me say that although this solution can be used as an answer to all 3 of the scenarios described above, it’s not always the most appropriate solution.<span style="mso-spacerun: yes">  </span>In <a href="http://www.drwpf.com/blog/?p=2">this earlier post</a>, I provided a MultiBinding approach that I believe is often a more dynamic answer for question 2.<span style="mso-spacerun: yes">  </span>And question 3 can sometimes be better accommodated by using a one-way-to-source (or two-way) binding on another property wherein the source (the CLR property) essentially becomes the target.<span style="mso-spacerun: yes">  </span>I will likely blog about that approach in a future post.</span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">Enough blather.</span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">So how <em style="mso-bidi-font-style: normal">do</em> you pass an object instance to the ConstructorParameters collection of the ObjectDataProvider?<span style="mso-spacerun: yes">  </span>The short answer is…</span></p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">It Can’t Be Done </span></strong></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">… using the native XAML support in 3.0 or 3.5.<span style="mso-spacerun: yes">  </span>More specifically, unless the object is a resource, there is no way to <em style="mso-bidi-font-style: normal">directly</em> reference it in markup.<span style="mso-spacerun: yes">  </span>If the target property is a dependency property on a dependency object within the visual tree, then a binding can be used to create an <em style="mso-bidi-font-style: normal">indirect</em> reference to the object.<span style="mso-spacerun: yes">  </span>But in all of the cases above, the target is not a dependency property, so this won’t work.</span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">One strength of WPF is that it’s more than just a framework… it’s an extensible <em style="mso-bidi-font-style: normal">“platform”</em>.<span style="mso-spacerun: yes">  </span>So what can we do?<span style="mso-spacerun: yes">  </span>We can extend markup!</span></p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">A New Markup Extension</span></strong></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">To solve the problem at hand, I introduce to you a markup extension I’ve created called <em style="mso-bidi-font-style: normal">ObjectReference</em>.<span style="mso-spacerun: yes">  </span>You can use this extension in your projects by adding <a href="http://www.drwpf.com/blog/Portals/0/Code/ObjectReference.cs.txt">this small code file</a> to the project.<span style="mso-spacerun: yes">  </span></span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">You are certainly already familiar with a number of native WPF markup extensions:<span style="mso-spacerun: yes">  </span>{Binding}, {TemplateBinding}, {StaticResource}, {DynamicResource}, {x:Null}, {x:Static}, {x:Type}, {x:Array}, etc.<span style="mso-spacerun: yes">  </span>Just like all of these classes, the ObjectReference class derives from the MarkupExtension base class and provides extended functionality for XAML-based scenarios.<span style="mso-spacerun: yes">  </span></span></p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">Usage in Markup</span></strong></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">The ObjectReference extension can be used in markup in two ways:<span style="mso-spacerun: yes">  </span>first as a <em style="mso-bidi-font-style: normal">“declaration”</em> on an object and second as a <em style="mso-bidi-font-style: normal">“reference”</em> to an earlier declaration.<span style="mso-spacerun: yes">  </span>Both of these uses can be seen in the following example:</span></p>
<pre class="MsoNormal" style="mso-layout-grid-align: none"><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin">&lt;</span><span style="color: #a31515; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin">Grid</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"> </span><strong style="mso-bidi-font-weight: normal"><span style="color: red; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin">xmlns:dw</span></strong><strong style="mso-bidi-font-weight: normal"><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin">=</span></strong><strong style="mso-bidi-font-weight: normal"><span style="font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin">"<span style="color: blue">clr-namespace:DrWPF.Markup</span>"
</span></strong><strong style="mso-bidi-font-weight: normal"><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"><span style="mso-spacerun: yes">    </span></span></strong><strong style="mso-bidi-font-weight: normal"><span style="color: red; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin">dw:ObjectReference.Declaration</span></strong><strong style="mso-bidi-font-weight: normal"><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin">=</span></strong><strong style="mso-bidi-font-weight: normal"><span style="font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin">"<span style="color: blue">{dw:ObjectReference RssGrid}</span>"</span></strong><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"> &gt;
</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin"><span style="mso-spacerun: yes">  </span>&lt;</span><span style="color: #a31515; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">Grid.Resources</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">&gt;
</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin"><span style="mso-spacerun: yes">    </span>&lt;</span><span style="color: #a31515; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">ObjectDataProvider</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin"> </span><span style="color: red; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">x:Key</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">=</span><span style="font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">"<span style="color: blue">RssData</span>"<span style="color: blue"> 
</span></span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin"><span style="mso-spacerun: yes">        </span></span><span style="color: red; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">ObjectType</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">=</span><span style="font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">"<span style="color: blue">{x:Type rss:RssFeeder}</span>"<span style="color: blue">&gt;
</span></span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin"><span style="mso-spacerun: yes">      </span>&lt;</span><span style="color: #a31515; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">ObjectDataProvider.ConstructorParameters</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">&gt;
</span><strong style="mso-bidi-font-weight: normal"><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin"><span style="mso-spacerun: yes">        </span>&lt;</span></strong><strong style="mso-bidi-font-weight: normal"><span style="color: #a31515; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">dw:ObjectReference</span></strong><strong style="mso-bidi-font-weight: normal"><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin"> </span></strong><strong style="mso-bidi-font-weight: normal"><span style="color: red; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">Key</span></strong><strong style="mso-bidi-font-weight: normal"><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">=</span></strong><strong style="mso-bidi-font-weight: normal"><span style="font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">"<span style="color: blue">RssGrid</span>"<span style="color: blue"> /&gt;
</span></span></strong><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin"><span style="mso-spacerun: yes">     </span><span style="mso-spacerun: yes"> </span>&lt;/</span><span style="color: #a31515; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">ObjectDataProvider.ConstructorParameters</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">&gt;
</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin"><span style="mso-spacerun: yes">    </span>&lt;/</span><span style="color: #a31515; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">ObjectDataProvider</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">&gt;
</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin"><span style="mso-spacerun: yes">  </span>&lt;/</span><span style="color: #a31515; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">Grid.Resources</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">&gt;
</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin"><span style="mso-spacerun: yes">  </span>&lt;</span><span style="color: #a31515; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">ListBox</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin"> </span><span style="color: red; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">DataSource</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">=</span><span style="font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">"<span style="color: blue">{StaticResource RssData}</span>"
</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin"><span style="mso-spacerun: yes">      </span></span><span style="color: red; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">ItemsSource</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">=</span><span style="font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">"<span style="color: blue">{Binding Path=RecentPosts}</span>"<span style="color: blue"> </span><span style="color: red">... </span><span style="color: blue">/&gt;
</span></span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">&lt;/</span><span style="color: #a31515; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">Grid</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 9.0pt; mso-fareast-theme-font: minor-latin">&gt;</span></pre>
<p class="MsoNormal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">Note the use of an attached Declaration property to declare the object reference.<span style="mso-spacerun: yes">  </span>This is one method of declaring a reference on a dependency object.<span style="mso-spacerun: yes">  </span>In this case, a reference is created for the Grid object and it is assigned a Key value using the string “RssGrid”.</span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">Later, the object is referenced in the ConstructorParameters collection by using a second ObjectReference with the same Key value.</span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">If the target object is not a dependency object, you can declare a reference by setting an existing property using the ObjectReference extension.<span style="mso-spacerun: yes">  </span>In this case, you must set the IsDeclaration property to <strong style="mso-bidi-font-weight: normal">true </strong>when creating the ObjectReference, as shown here:</span></p>
<pre class="MsoNormal" style="mso-layout-grid-align: none"><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin">&lt;</span><span style="color: #a31515; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin">winforms:MonthCalendar</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"> 
</span><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"><span style="mso-spacerun: yes">    </span></span><strong style="mso-bidi-font-weight: normal"><span style="color: red; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin">Name</span></strong><strong style="mso-bidi-font-weight: normal"><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin">=</span></strong><strong style="mso-bidi-font-weight: normal"><span style="font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin">"<span style="color: blue">{dw:ObjectReference mc, IsDeclaration=True}</span>"</span></strong><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin"> &gt;</span></pre>
<p class="MsoNormal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">Because IsDeclaration is true, the reference is treated as a declaration for the MonthCalendar object.<span style="mso-spacerun: yes">  </span>In this case, the supplied key value of “mc” is passed straight through to the Name property.<span style="mso-spacerun: yes">  </span>It is essentially the same as saying </span><strong style="mso-bidi-font-weight: normal"><span style="color: red; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin">Name</span></strong><strong style="mso-bidi-font-weight: normal"><span style="color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin">=</span></strong><strong style="mso-bidi-font-weight: normal"><span style="font-family: &quot;Courier New&quot;; mso-no-proof: yes; mso-fareast-font-family: Calibri; mso-bidi-font-size: 10.0pt; mso-fareast-theme-font: minor-latin">&#8220;<span style="color: blue">mc</span>&#8220;</span></strong><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">, except that it also creates a reference to the object.</span></p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">Download a Sample</span></strong></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">If you’ve examined the code in the sample I posted earlier in my forum response, note that it is slightly different that the version described herein. <span style="mso-spacerun: yes"> </span>I have updated the extension to make it fit for wide consumption and more applicable to other scenarios, such as referencing non-dependency objects.<span style="mso-spacerun: yes">  </span>You can <a href="http://www.drwpf.com/blog/Portals/0/Samples/ObjectReferenceSample.zip">download the updated ObjectReference sample here</a>.<strong style="mso-bidi-font-weight: normal"></strong></span></p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">How it works</span></strong></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">Let’s take a look at how this ObjectReference markup extension works…</span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">Like any markup extension, our class derives from MarkupExtension and overrides the ProvideValue() method to return a resolved value.<span style="mso-spacerun: yes">  </span>The IServiceProvider parameter supplied to this method provides access to the parser’s context and allows us to know things such as which property the extension is targeting and the exact instance of the object that owns the property.<span style="mso-spacerun: yes">  </span>This is very helpful for our purposes.</span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">If you look at the code in the ProvideValue() function, you’ll see that it first checks to see if we’re creating a declaration or a reference.<span style="mso-spacerun: yes">  </span>If it’s a declaration, then a reference to the target object is added to a static dictionary of weak references using the Key property as the dictionary key for the reference.<span style="mso-spacerun: yes">  </span>In this case, the Key value is returned as the result of the ProvideValue() function. <span style="mso-spacerun: yes"> </span>If the ObjectReference is being used as a reference (rather than a declaration), the ProvideValue() function looks up the supplied Key in the dictionary and returns the object stored earlier.</span></p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">Sidenote:</span></strong><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt"><span style="mso-spacerun: yes">  </span>Since we are maintaining references in a static dictionary, it is important that we only keep weak references to the objects.<span style="mso-spacerun: yes">  </span>Otherwise, our references would keep the objects alive indefinitely.<span style="mso-spacerun: yes">  </span>We certainly don’t want to leak objects.</span></p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">Limitations</span></strong></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">There are definitely some limitations to be aware of with this approach.<span style="mso-spacerun: yes">  </span></span></p>
<p class="MsoNormal"><em style="mso-bidi-font-style: normal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">Parse order matters</span></em></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">First and foremost, this solution is very dependent on the order in which the markup is parsed.<span style="mso-spacerun: yes">  </span>The declaration must be parsed before the reference.<span style="mso-spacerun: yes">  </span>Keep in mind that XAML is parsed from top to bottom.<span style="mso-spacerun: yes">  </span>This is similar to the way static resource references work, but not exactly.<span style="mso-spacerun: yes">  </span>Static resources are resolved based on the tree of elements, whereas, with our extension, it is purely based on parse order.<span style="mso-spacerun: yes">  </span>The reference can appear in another tree entirely and, as long as that tree is parsed first, the reference will resolve.</span></p>
<p class="MsoNormal"><em style="mso-bidi-font-style: normal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">It’s not a binding</span></em></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">Second, note that the reference is <em style="mso-bidi-font-style: normal">not</em> a binding.<span style="mso-spacerun: yes">  </span>It is resolved exactly once at parse time and it resolves based on the values in the static dictionary at that point in time.<span style="mso-spacerun: yes">  </span>If you change a reference later, it will have no effect on the earlier returned object.</span></p>
<p class="MsoNormal"><em style="mso-bidi-font-style: normal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">References will be overwritten</span></em></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">Also, note that a reference will be overwritten if another declaration is parsed having the same Key value.<span style="mso-spacerun: yes">  </span>Most of the time, this will be the desired behavior.<span style="mso-spacerun: yes">  </span>For example, if you parse a XAML page that contains a declaration on its root element and references in the same page, you’ll want the references to refer to the declaration for that instance of the page.<span style="mso-spacerun: yes">  </span>There is no way to get at the earlier references after they’ve been replaced.</span></p>
<p class="MsoNormal"><em style="mso-bidi-font-style: normal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">The ObjectReference class must be in the project</span></em></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">You must include the ObjectReference.cs file in the project.<span style="mso-spacerun: yes">  </span>If you use ObjectReference extensions in loose XAML files, other parsers may not be able to parse the files.<span style="mso-spacerun: yes">  </span>To work around this limitation, you could compile a separate assembly including the extension and include it alongside the loose XAML.<span style="mso-spacerun: yes">  </span>The xmlns namespace mapping in the XAML files would need to reference the assembly containing the ObjectReference class.</span></p>
<p class="MsoNormal"><em style="mso-bidi-font-style: normal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">The reference dictionary is never compacted</span></em></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">To clean up this extension, I really should periodically walk the dictionary of weak references and remove entries for objects that no longer exist.<span style="mso-spacerun: yes">  </span>So far, I’ve just been too lazy to do that.<span style="mso-spacerun: yes">  </span>You’ll note that I’m providing the ObjectReference.cs file under a BSD open source license, so feel free to make any such improvements that you see fit.<span style="mso-spacerun: yes">  </span></span><span style="font-family: Wingdings; mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-char-type: symbol; mso-symbol-font-family: Wingdings"><span style="mso-char-type: symbol; mso-symbol-font-family: Wingdings">J</span></span></p>
<p class="MsoNormal"><strong style="mso-bidi-font-weight: normal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">Let me know how it works for you!</span></strong></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">Please let me know in what creative ways you are able to use this markup extension.</span></p>
<p class="MsoNormal"><span style="mso-bidi-font-family: Arial; mso-bidi-font-size: 9.0pt">Cheers,<br />
Dr. WPF</span></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://drwpf.com/blog/2007/09/02/supplying-an-object-reference-in-the-constructorparameters-collection-of-an-objectdataprovider/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

