<?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; Value Converters</title>
	<atom:link href="http://drwpf.com/blog/category/value-converters/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>Can my value converter access the target of the binding?</title>
		<link>http://drwpf.com/blog/2007/08/18/can-my-value-converter-access-the-target-of-the-binding/</link>
		<comments>http://drwpf.com/blog/2007/08/18/can-my-value-converter-access-the-target-of-the-binding/#comments</comments>
		<pubDate>Sat, 18 Aug 2007 17:02:51 +0000</pubDate>
		<dc:creator>Dr. WPF</dc:creator>
				<category><![CDATA[Value Converters]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://drwpf.com/blog/?p=2</guid>
		<description><![CDATA[Dear Dr. WPF,
In my application, I’m using a value converter to convert a GUID to a static resource.  The GUID is accessible in my DataTemplate through a binding like this:
&#60;DataTemplate&#62;
  &#60;Grid Height="Auto" Width="Auto"&#62;
    &#60;TextBlock Text="{Binding Path=ID}" /&#62;
  &#60;/Grid&#62;
&#60;/DataTemplate&#62;
I would really like to use the GUID in a more dynamic way as a key into a resource [...]]]></description>
			<content:encoded><![CDATA[<p><em><span style="font-size: 9pt; font-family: &quot;Arial&quot;,&quot;sans-serif&quot;; mso-fareast-font-family: 'Times New Roman'">Dear Dr. WPF,</span></em></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal"><em><span style="font-size: 9pt; font-family: &quot;Arial&quot;,&quot;sans-serif&quot;; mso-fareast-font-family: 'Times New Roman'">In my application, I’m using a value converter to convert a GUID to a static resource.  The GUID is accessible in my DataTemplate through a binding like this:</span></em></p>
<pre class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal"><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">&lt;</span><span style="font-size: 9pt; color: #a31515; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">DataTemplate</span><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">&gt;
  &lt;</span><span style="font-size: 9pt; color: #a31515; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">Grid </span><span style="font-size: 9pt; color: red; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">Height</span><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">=</span><span style="font-size: 9pt; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">"<span style="color: blue">Auto</span>" <span style="color: red">Width</span><span style="color: blue">=</span>"<span style="color: blue">Auto</span>"<span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">TextBlock </span><span style="color: red">Text</span><span style="color: blue">=</span>"<span style="color: blue">{Binding Path=ID}</span>"<span style="color: blue"> /&gt;
  &lt;/</span><span style="color: #a31515">Grid</span><span style="color: blue">&gt;</span></span><span style="font-size: 9pt; color: black; font-family: &quot;Arial&quot;,&quot;sans-serif&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">
</span><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">&lt;/</span><span style="font-size: 9pt; color: #a31515; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">DataTemplate</span><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">&gt;</span></pre>
<p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal"><em><span style="font-size: 9pt; color: black; font-family: &quot;Arial&quot;,&quot;sans-serif&quot;; mso-fareast-font-family: 'Times New Roman'">I would really like to use the GUID in a more dynamic way as a key into a resource dictionary. I imagine it would look something like this (although obviously this won’t work):</span></em><span style="font-size: 9pt; font-family: &quot;Arial&quot;,&quot;sans-serif&quot;; mso-fareast-font-family: 'Times New Roman'"> </span></p>
<pre class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal"><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">&lt;</span><span style="font-size: 9pt; color: #a31515; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">DataTemplate</span><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">&gt;
  &lt;</span><span style="font-size: 9pt; color: #a31515; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">Grid </span><span style="font-size: 9pt; color: red; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">Height</span><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">=</span><span style="font-size: 9pt; color: black; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">"</span><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">Auto</span><span style="font-size: 9pt; color: black; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">" </span><span style="font-size: 9pt; color: red; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">Width</span><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">=</span><span style="font-size: 9pt; color: black; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">"</span><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">Auto</span><span style="font-size: 9pt; color: black; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">"</span><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">&gt;
    &lt;</span><span style="font-size: 9pt; color: #a31515; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">ContentControl</span><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic"> 
</span><span style="font-size: 9pt; color: red; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">        Content</span><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">=</span><span style="font-size: 9pt; color: black; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">"</span><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">{StaticResource {Binding Path=ID}}</span><span style="font-size: 9pt; color: black; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">"</span><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic"> /&gt;
  &lt;/</span><span style="font-size: 9pt; color: #a31515; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">Grid</span><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">&gt;
&lt;/</span><span style="font-size: 9pt; color: #a31515; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">DataTemplate</span><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-style: italic">&gt;</span></pre>
<p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal"><em><span style="font-size: 9pt; color: black; font-family: &quot;Arial&quot;,&quot;sans-serif&quot;; mso-fareast-font-family: 'Times New Roman'">I thought I might be able to use a binding along with a value converter that uses FindResource() to look up the static resource. Unfortunately, my value converter does not receive a reference to the object on which the binding is set. It only gets the bound value (the GUID) and the type of the target dependency property. In order to call FindResource(), I need a reference to the actual target dependency object.</span></em></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal"><em><span style="font-size: 9pt; color: black; font-family: &quot;Arial&quot;,&quot;sans-serif&quot;; mso-fareast-font-family: 'Times New Roman'">Is there an easy way to do this in WPF?</span></em></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal"><em><span style="font-size: 9pt; color: black; font-family: &quot;Arial&quot;,&quot;sans-serif&quot;; mso-fareast-font-family: 'Times New Roman'">Thanks for any help you can provide!</span></em></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal"><em><span style="font-size: 9pt; color: black; font-family: &quot;Arial&quot;,&quot;sans-serif&quot;; mso-fareast-font-family: 'Times New Roman'">Sincerely,<br />
Jim<br />
 </span></em></p>
<hr size="2" /><span style="font-size: 9pt; font-family: &quot;Arial&quot;,&quot;sans-serif&quot;; mso-fareast-font-family: 'Times New Roman'">Hi Jim,</span></p>
<p><span style="font-size: 9pt; font-family: &quot;Arial&quot;,&quot;sans-serif&quot;; mso-fareast-font-family: 'Times New Roman'">Yes, it can be frustrating that your value converter does not receive a reference to the target dependency object for the binding. Value converters were designed to be more generic in nature, allowing them to be used in scenarios that might not involve bindings. As such, they have no knowledge of the binding itself or the object on which it is set. That said, there are actually many cases where a value converter might desire this kind of contextual information. </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal"><span style="font-size: 9pt; font-family: &quot;Arial&quot;,&quot;sans-serif&quot;; mso-fareast-font-family: 'Times New Roman'">Your scenario is a prime candidate for a multibinding and multivalue converter. With a multibinding, you can actually supply your own reference to the target dependency object using one of the bindings, as follows:</span></p>
<pre class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">&lt;</span><span style="font-size: 9pt; color: #a31515; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">DataTemplate</span><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">&gt;</span></pre>
<pre class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">  &lt;</span><span style="font-size: 9pt; color: #a31515; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">Grid</span><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">&gt;</span></pre>
<pre class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">    &lt;</span><span style="font-size: 9pt; color: #a31515; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">ContentControl</span><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">&gt;</span></pre>
<pre class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">      &lt;</span><span style="font-size: 9pt; color: #a31515; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">ContentControl.Content</span><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">&gt;</span></pre>
<pre class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">        &lt;</span><span style="font-size: 9pt; color: #a31515; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">MultiBinding </span><span style="font-size: 9pt; color: red; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">Converter</span><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">=</span><span style="font-size: 9pt; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">"<span style="color: blue">{StaticResource MyConverter}</span>"<span style="color: blue">&gt;</span></span></pre>
<pre class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">          &lt;</span><span style="font-size: 9pt; color: #a31515; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">MultiBinding.Bindings</span><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">&gt;</span></pre>
<pre class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">            &lt;</span><span style="font-size: 9pt; color: #a31515; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">Binding </span><span style="font-size: 9pt; color: red; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">RelativeSource</span><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">=</span><span style="font-size: 9pt; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">"<span style="color: blue">{RelativeSource Self}</span>"<span style="color: blue"> /&gt;</span></span></pre>
<pre class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">            &lt;</span><span style="font-size: 9pt; color: #a31515; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">Binding </span><span style="font-size: 9pt; color: red; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">Path</span><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">=</span><span style="font-size: 9pt; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">"<span style="color: blue">ID</span>"<span style="color: blue"> /&gt;</span></span></pre>
<pre class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">          &lt;/</span><span style="font-size: 9pt; color: #a31515; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">MultiBinding.Bindings</span><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">&gt;</span></pre>
<pre class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">        &lt;/</span><span style="font-size: 9pt; color: #a31515; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">MultiBinding</span><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">&gt;</span></pre>
<pre class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">      &lt;/</span><span style="font-size: 9pt; color: #a31515; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">ContentControl.Content</span><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">&gt;</span></pre>
<pre class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">    &lt;/</span><span style="font-size: 9pt; color: #a31515; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">ContentControl</span><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">&gt;</span></pre>
<pre class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">  &lt;/</span><span style="font-size: 9pt; color: #a31515; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">Grid</span><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">&gt;</span></pre>
<pre class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">&lt;/</span><span style="font-size: 9pt; color: #a31515; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">DataTemplate</span><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">&gt;</span></pre>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"><span style="font-size: 9pt; font-family: &quot;Arial&quot;,&quot;sans-serif&quot;; mso-fareast-font-family: 'Times New Roman'"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal"><span style="font-size: 9pt; font-family: &quot;Arial&quot;,&quot;sans-serif&quot;; mso-fareast-font-family: 'Times New Roman'">In this case, the converter class must implement the IMultiValueConverter interface so that it can be used with the multibinding. Since we have conveniently passed in a <em>Self</em> reference as the first binding, we now have a reference to the target dependency object in our converter. The converter’s Convert method might look something like the following:</span></p>
<pre class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"><span style="font-size: 9pt; color: blue; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">public</span><span style="font-size: 9pt; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'"> <span style="color: blue">object</span> Convert(<span style="color: blue">object</span>[] values, <span style="color: #2b91af">Type</span> targetType, </span></pre>
<pre class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"><span style="font-size: 9pt; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">    <span style="color: blue">object</span> parameter, <span style="color: #2b91af">CultureInfo</span> culture)</span></pre>
<pre class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"><span style="font-size: 9pt; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">{</span></pre>
<pre class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"><span style="font-size: 9pt; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">  <span style="color: #2b91af">FrameworkElement</span> targetObject = values[0] <span style="color: blue">as </span><span style="color: #2b91af">FrameworkElement</span>;</span></pre>
<pre class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"><span style="font-size: 9pt; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">  <span style="color: blue">if</span> (targetObject == <span style="color: blue">null</span>)</span></pre>
<pre class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"><span style="font-size: 9pt; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">  {</span></pre>
<pre class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"><span style="font-size: 9pt; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">    <span style="color: blue">return</span> <span style="color: #2b91af">DependencyProperty</span>.UnsetValue;</span></pre>
<pre class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"><span style="font-size: 9pt; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">  }</span></pre>
<pre class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"><span style="font-size: 9pt; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">  <span style="color: blue">return</span> targetObject.TryFindResource(values[1]);</span></pre>
<pre class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"><span style="font-size: 9pt; font-family: &quot;Courier New&quot;; mso-fareast-font-family: 'Times New Roman'">}</span></pre>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"><span style="font-size: 9pt; font-family: &quot;Arial&quot;,&quot;sans-serif&quot;; mso-fareast-font-family: 'Times New Roman'"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal"><span style="font-size: 9pt; font-family: &quot;Arial&quot;,&quot;sans-serif&quot;; mso-fareast-font-family: 'Times New Roman'">I hope this helps!</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal"><span style="font-size: 9pt; font-family: &quot;Arial&quot;,&quot;sans-serif&quot;; mso-fareast-font-family: 'Times New Roman'">Best regards,<br />
Dr. WPF</span></p>
]]></content:encoded>
			<wfw:commentRss>http://drwpf.com/blog/2007/08/18/can-my-value-converter-access-the-target-of-the-binding/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

