<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Tips and Tricks: Making Value Converters More Accessible in Markup</title>
	<atom:link href="http://drwpf.com/blog/2009/03/17/tips-and-tricks-making-value-converters-more-accessible-in-markup/feed/" rel="self" type="application/rss+xml" />
	<link>http://drwpf.com/blog/2009/03/17/tips-and-tricks-making-value-converters-more-accessible-in-markup/</link>
	<description>Drinking (and serving) the WPF Kool-Aid since 2002</description>
	<lastBuildDate>Mon, 30 Aug 2010 18:54:01 -0500</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Yury</title>
		<link>http://drwpf.com/blog/2009/03/17/tips-and-tricks-making-value-converters-more-accessible-in-markup/comment-page-1/#comment-2908</link>
		<dc:creator>Yury</dc:creator>
		<pubDate>Tue, 25 May 2010 21:45:35 +0000</pubDate>
		<guid isPermaLink="false">http://drwpf.com/blog/?p=48#comment-2908</guid>
		<description>If extension is already created and it is also converter we can just &quot;return this;&quot;. It can save us unnecessary ifs, allows base class without template and allows provide additional constructor params specific for instance.
BTW: x:Static is also MarkupExtension and will be created every time, but inside it has a lot of code.</description>
		<content:encoded><![CDATA[<p>If extension is already created and it is also converter we can just &#8220;return this;&#8221;. It can save us unnecessary ifs, allows base class without template and allows provide additional constructor params specific for instance.<br />
BTW: x:Static is also MarkupExtension and will be created every time, but inside it has a lot of code.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roman</title>
		<link>http://drwpf.com/blog/2009/03/17/tips-and-tricks-making-value-converters-more-accessible-in-markup/comment-page-1/#comment-428</link>
		<dc:creator>Roman</dc:creator>
		<pubDate>Wed, 05 Aug 2009 20:18:07 +0000</pubDate>
		<guid isPermaLink="false">http://drwpf.com/blog/?p=48#comment-428</guid>
		<description>Good tip. But we decided to use Jacob&#039;s approach.&lt;br&gt;One tip for all of you who are lazy. You can substitute the &lt;br&gt;&lt;br&gt; if (_converter == null)&lt;br&gt; {&lt;br&gt; _converter = new DummyConverter();&lt;br&gt; }&lt;br&gt; return _converter;&lt;br&gt;&lt;br&gt;part with the following code:&lt;br&gt;&lt;br&gt; return _converter ?? (_converter = new DummyConverter());</description>
		<content:encoded><![CDATA[<p>Good tip. But we decided to use Jacob&#8217;s approach.<br />One tip for all of you who are lazy. You can substitute the </p>
<p> if (_converter == null)<br /> {<br /> _converter = new DummyConverter();<br /> }<br /> return _converter;</p>
<p>part with the following code:</p>
<p> return _converter ?? (_converter = new DummyConverter());</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dr. WPF</title>
		<link>http://drwpf.com/blog/2009/03/17/tips-and-tricks-making-value-converters-more-accessible-in-markup/comment-page-1/#comment-427</link>
		<dc:creator>Dr. WPF</dc:creator>
		<pubDate>Sun, 05 Jul 2009 09:04:06 +0000</pubDate>
		<guid isPermaLink="false">http://drwpf.com/blog/?p=48#comment-427</guid>
		<description>Yes, you are technically correct, Bill. I should have said there is only a single converter that is &quot;used&quot; for conversions. &lt;br&gt;&lt;br&gt;The converter class in this example has no local fields (just methods), so it is very lightweight. Your argument about the class being instantiated repeatedly is valid, but the same thing is true about StaticResource, DynamicResource, etc, and we certainly don&#039;t worry about the perf impacts with these (which are far more prevalent). As such, I&#039;m okay with the overhead.&lt;br&gt;&lt;br&gt;But thanks for keeping me honest! :-p&lt;br&gt;&lt;br&gt;-dw</description>
		<content:encoded><![CDATA[<p>Yes, you are technically correct, Bill. I should have said there is only a single converter that is &#8220;used&#8221; for conversions. </p>
<p>The converter class in this example has no local fields (just methods), so it is very lightweight. Your argument about the class being instantiated repeatedly is valid, but the same thing is true about StaticResource, DynamicResource, etc, and we certainly don&#8217;t worry about the perf impacts with these (which are far more prevalent). As such, I&#8217;m okay with the overhead.</p>
<p>But thanks for keeping me honest! :-p</p>
<p>-dw</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: wekempf</title>
		<link>http://drwpf.com/blog/2009/03/17/tips-and-tricks-making-value-converters-more-accessible-in-markup/comment-page-1/#comment-426</link>
		<dc:creator>wekempf</dc:creator>
		<pubDate>Thu, 02 Jul 2009 19:15:18 +0000</pubDate>
		<guid isPermaLink="false">http://drwpf.com/blog/?p=48#comment-426</guid>
		<description>I just discovered this trick, felt stupid for not thinking about it earlier, and figured I&#039;d Google to see how many others had thought of it. That&#039;s when I found this page. It&#039;s a little comforting that the publication date wasn&#039;t that long ago ;).&lt;br&gt;&lt;br&gt;You call this a singleton and say that &quot;the markup extension only creates the converter the first time ProvideValue is called&quot;. That&#039;s not quite accurate. Each time the markup extension is used, a new instance is going to be created, because, well, that&#039;s how markup extensions work. What is true is that only one instance will ever be returned. Use this in a template where it will be used 1000 times, you&#039;ll get 1001 instances constructed, but only 1 of those will not be very shortly garbage collected. It may have some impact on performance, but I doubt you&#039;ll find a scenario where this impact is significant. It won&#039;t have an impact on memory.&lt;br&gt;&lt;br&gt;There&#039;s a few alternate implementation ideas to consider. First, you might want to consider using a weak reference instead of a strong reference, just so the &quot;singleton&quot; instance isn&#039;t held longer than it needs to be. If your converter is going to be configurable, you have a couple of choices. You can either cache multiple instances based on the values in the markup extension, or just &quot;return this;&quot; from the ProvideValue (knowing what this will mean to memory usage).&lt;br&gt;&lt;br&gt;The {x:Static foo:MyConverter.Instance} alternative isn&#039;t a bad idea, but only works for non-configurable converters, and is more verbose.</description>
		<content:encoded><![CDATA[<p>I just discovered this trick, felt stupid for not thinking about it earlier, and figured I&#8217;d Google to see how many others had thought of it. That&#8217;s when I found this page. It&#8217;s a little comforting that the publication date wasn&#8217;t that long ago <img src='http://drwpf.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
<p>You call this a singleton and say that &#8220;the markup extension only creates the converter the first time ProvideValue is called&#8221;. That&#8217;s not quite accurate. Each time the markup extension is used, a new instance is going to be created, because, well, that&#8217;s how markup extensions work. What is true is that only one instance will ever be returned. Use this in a template where it will be used 1000 times, you&#8217;ll get 1001 instances constructed, but only 1 of those will not be very shortly garbage collected. It may have some impact on performance, but I doubt you&#8217;ll find a scenario where this impact is significant. It won&#8217;t have an impact on memory.</p>
<p>There&#8217;s a few alternate implementation ideas to consider. First, you might want to consider using a weak reference instead of a strong reference, just so the &#8220;singleton&#8221; instance isn&#8217;t held longer than it needs to be. If your converter is going to be configurable, you have a couple of choices. You can either cache multiple instances based on the values in the markup extension, or just &#8220;return this;&#8221; from the ProvideValue (knowing what this will mean to memory usage).</p>
<p>The {x:Static foo:MyConverter.Instance} alternative isn&#8217;t a bad idea, but only works for non-configurable converters, and is more verbose.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dr. WPF</title>
		<link>http://drwpf.com/blog/2009/03/17/tips-and-tricks-making-value-converters-more-accessible-in-markup/comment-page-1/#comment-425</link>
		<dc:creator>Dr. WPF</dc:creator>
		<pubDate>Thu, 30 Apr 2009 02:00:25 +0000</pubDate>
		<guid isPermaLink="false">http://drwpf.com/blog/?p=48#comment-425</guid>
		<description>Very cool, Philipp!  Nice work! :-D</description>
		<content:encoded><![CDATA[<p>Very cool, Philipp!  Nice work! <img src='http://drwpf.com/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':-D' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Philipp</title>
		<link>http://drwpf.com/blog/2009/03/17/tips-and-tricks-making-value-converters-more-accessible-in-markup/comment-page-1/#comment-424</link>
		<dc:creator>Philipp</dc:creator>
		<pubDate>Sat, 25 Apr 2009 15:16:02 +0000</pubDate>
		<guid isPermaLink="false">http://drwpf.com/blog/?p=48#comment-424</guid>
		<description>&lt;div class=&quot;jbr&quot;&gt;Dear Doctor,&lt;br&gt;&lt;br&gt;I found the pattern also most helpful when it comes to declaring custom commands - thanks for sharing :-)&lt;br&gt;&lt;br&gt;I published the complementing extension &lt;a href=&quot;http://www.hardcodet.net/2009/04/wpf-command-markup-extension&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;&lt;/div&gt;</description>
		<content:encoded><![CDATA[<div class="jbr">Dear Doctor,</p>
<p>I found the pattern also most helpful when it comes to declaring custom commands &#8211; thanks for sharing <img src='http://drwpf.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>I published the complementing extension <a href="http://www.hardcodet.net/2009/04/wpf-command-markup-extension" target="_blank">here</a></div>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dr. WPF</title>
		<link>http://drwpf.com/blog/2009/03/17/tips-and-tricks-making-value-converters-more-accessible-in-markup/comment-page-1/#comment-423</link>
		<dc:creator>Dr. WPF</dc:creator>
		<pubDate>Fri, 03 Apr 2009 01:36:44 +0000</pubDate>
		<guid isPermaLink="false">http://drwpf.com/blog/?p=48#comment-423</guid>
		<description>Hi Eric,&lt;br&gt;&lt;br&gt;In the example I included above, the converter is a singleton. The markup extension only creates the converter the first time ProvideValue is called and thereafter returns the same instance. Like you say, this works for most converters.&lt;br&gt;&lt;br&gt;I, too, like AlanO&#039;s example, which takes my static instance approach and puts it in a base class for easy derivation.&lt;br&gt;&lt;br&gt;I do have some examples of per-instance (nonshared) converters that cache object references for performance reasons. I may post an example of one such converter that I think might help others... I did not include it with this article because it requires carefully explaining the usage scenarios so that the same converter is not inadvertently shared across controls. I&#039;ll see what I can write up. :-)</description>
		<content:encoded><![CDATA[<p>Hi Eric,</p>
<p>In the example I included above, the converter is a singleton. The markup extension only creates the converter the first time ProvideValue is called and thereafter returns the same instance. Like you say, this works for most converters.</p>
<p>I, too, like AlanO&#8217;s example, which takes my static instance approach and puts it in a base class for easy derivation.</p>
<p>I do have some examples of per-instance (nonshared) converters that cache object references for performance reasons. I may post an example of one such converter that I think might help others&#8230; I did not include it with this article because it requires carefully explaining the usage scenarios so that the same converter is not inadvertently shared across controls. I&#8217;ll see what I can write up. <img src='http://drwpf.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric Burke</title>
		<link>http://drwpf.com/blog/2009/03/17/tips-and-tricks-making-value-converters-more-accessible-in-markup/comment-page-1/#comment-422</link>
		<dc:creator>Eric Burke</dc:creator>
		<pubDate>Thu, 02 Apr 2009 22:44:21 +0000</pubDate>
		<guid isPermaLink="false">http://drwpf.com/blog/?p=48#comment-422</guid>
		<description>We tend to use static instance members (like Jacob) and then reference them as &quot;... Converter={x:Static src:TheFooConverter}&quot;.&lt;br&gt;&lt;br&gt;I&#039;m not sure how much overhead subclassing MarkupExtension is, but the bigger issue for me is that I&#039;m also not sure if I would create a new Converter instance every time ProvideValue() is called -- think creating 1000 of these as part of a template. Could put a lot of memory pressure on the app without realizing it, and without necessity -- generally you only need one converter instance because it&#039;s reused and does not need to be thread-safe.&lt;br&gt;&lt;br&gt;I like AlanO&#039;s static approach -- best of both worlds.&lt;br&gt;&lt;br&gt;For parameterizablility (is that even a word?), the MarkupExtension approach gives you more flexibility because you can create the instance inline as opposed to as a resource or as part of the codebehind. But these situations are rare in my experience.</description>
		<content:encoded><![CDATA[<p>We tend to use static instance members (like Jacob) and then reference them as &#8220;&#8230; Converter={x:Static src:TheFooConverter}&#8221;.</p>
<p>I&#8217;m not sure how much overhead subclassing MarkupExtension is, but the bigger issue for me is that I&#8217;m also not sure if I would create a new Converter instance every time ProvideValue() is called &#8212; think creating 1000 of these as part of a template. Could put a lot of memory pressure on the app without realizing it, and without necessity &#8212; generally you only need one converter instance because it&#8217;s reused and does not need to be thread-safe.</p>
<p>I like AlanO&#8217;s static approach &#8212; best of both worlds.</p>
<p>For parameterizablility (is that even a word?), the MarkupExtension approach gives you more flexibility because you can create the instance inline as opposed to as a resource or as part of the codebehind. But these situations are rare in my experience.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AlanO</title>
		<link>http://drwpf.com/blog/2009/03/17/tips-and-tricks-making-value-converters-more-accessible-in-markup/comment-page-1/#comment-418</link>
		<dc:creator>AlanO</dc:creator>
		<pubDate>Thu, 19 Mar 2009 03:32:39 +0000</pubDate>
		<guid isPermaLink="false">http://drwpf.com/blog/?p=48#comment-418</guid>
		<description>Dr. WPF,&lt;br&gt;&lt;br&gt;Thank you so much for the great tip!&lt;br&gt;&lt;br&gt;I extended it to save one extra step (because I am lazy too) by making an abstract generic class. This makes it easier to convert my existing ValueConverters to use your technique. Sample code follows:&lt;br&gt;&lt;br&gt;// Base class&lt;br&gt;public abstract class ConverterMarkupExtension&lt;T&gt;: MarkupExtension, IValueConverter&lt;br&gt; where T: class, new()&lt;br&gt; {&lt;br&gt; private static T m_converter = null;&lt;br&gt;&lt;br&gt; public override object ProvideValue(IServiceProvider serviceProvider)&lt;br&gt; {&lt;br&gt; if (m_converter == null)&lt;br&gt; {&lt;br&gt; m_converter = new T();&lt;br&gt; }&lt;br&gt; return m_converter;&lt;br&gt; }&lt;br&gt;&lt;br&gt; #region IValueConverter Members&lt;br&gt;&lt;br&gt; public abstract object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture);&lt;br&gt; public abstract object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture);&lt;br&gt;&lt;br&gt; #endregion&lt;br&gt; }&lt;br&gt;&lt;br&gt;&lt;br&gt;//Sample derived class&lt;br&gt; public class BytesToKBValueConverter: ConverterMarkupExtension&lt;BytesToKBValueConverter&gt;&lt;br&gt; {&lt;br&gt; public override object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)&lt;br&gt; {&lt;br&gt; if (value != null &amp;&amp; value is double)&lt;br&gt; {&lt;br&gt; double bytes = System.Convert.ToDouble(value);&lt;br&gt; return bytes / 1024.0d;&lt;br&gt; }&lt;br&gt; else&lt;br&gt; return value;&lt;br&gt; }&lt;br&gt;&lt;br&gt; public override object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)&lt;br&gt; {&lt;br&gt; throw new NotSupportedException();&lt;br&gt; }&lt;br&gt; }</description>
		<content:encoded><![CDATA[<p>Dr. WPF,</p>
<p>Thank you so much for the great tip!</p>
<p>I extended it to save one extra step (because I am lazy too) by making an abstract generic class. This makes it easier to convert my existing ValueConverters to use your technique. Sample code follows:</p>
<p>// Base class<br />public abstract class ConverterMarkupExtension<t>: MarkupExtension, IValueConverter<br /> where T: class, new()<br /> {<br /> private static T m_converter = null;</p>
<p> public override object ProvideValue(IServiceProvider serviceProvider)<br /> {<br /> if (m_converter == null)<br /> {<br /> m_converter = new T();<br /> }<br /> return m_converter;<br /> }</p>
<p> #region IValueConverter Members</p>
<p> public abstract object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture);<br /> public abstract object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture);</p>
<p> #endregion<br /> }</p>
<p>//Sample derived class<br /> public class BytesToKBValueConverter: ConverterMarkupExtension<bytestokbvalueconverter><br /> {<br /> public override object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)<br /> {<br /> if (value != null &#038;&#038; value is double)<br /> {<br /> double bytes = System.Convert.ToDouble(value);<br /> return bytes / 1024.0d;<br /> }<br /> else<br /> return value;<br /> }</p>
<p> public override object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)<br /> {<br /> throw new NotSupportedException();<br /> }<br /> }</bytestokbvalueconverter></t></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dr. WPF</title>
		<link>http://drwpf.com/blog/2009/03/17/tips-and-tricks-making-value-converters-more-accessible-in-markup/comment-page-1/#comment-421</link>
		<dc:creator>Dr. WPF</dc:creator>
		<pubDate>Thu, 19 Mar 2009 03:06:35 +0000</pubDate>
		<guid isPermaLink="false">http://drwpf.com/blog/?p=48#comment-421</guid>
		<description>Hi David, &lt;br&gt;&lt;br&gt;Do you mean for specifying the ConverterParameter or for supplying a parameter to the constructor of the converter? Both will work, but you&#039;re pretty much stuck with supplying the parameter as a StaticResource (the same as you&#039;d normally do for ConverterParameter) or you could leverage the ObjectReference markup extension I described a while back.&lt;br&gt;&lt;br&gt;Cheers,&lt;br&gt;-dw</description>
		<content:encoded><![CDATA[<p>Hi David, </p>
<p>Do you mean for specifying the ConverterParameter or for supplying a parameter to the constructor of the converter? Both will work, but you&#8217;re pretty much stuck with supplying the parameter as a StaticResource (the same as you&#8217;d normally do for ConverterParameter) or you could leverage the ObjectReference markup extension I described a while back.</p>
<p>Cheers,<br />-dw</p>
]]></content:encoded>
	</item>
</channel>
</rss>

