<?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</title>
	<atom:link href="http://drwpf.com/blog/category/markup/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>Worth the wait&#8230; this app KaRocks!</title>
		<link>http://drwpf.com/blog/2008/01/08/worth-the-wait-this-app-karocks/</link>
		<comments>http://drwpf.com/blog/2008/01/08/worth-the-wait-this-app-karocks/#comments</comments>
		<pubDate>Tue, 08 Jan 2008 18:08:41 +0000</pubDate>
		<dc:creator>Dr. WPF</dc:creator>
				<category><![CDATA[Markup]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://drwpf.com/blog/?p=25</guid>
		<description><![CDATA[
When answering WPF questions, I like to&#160;provide pure markup samples, whenever possible.&#160; I&#8217;m going to divulge a little secret now&#8230;&#160; Whenever I post such a &#34;XamlPad-ready&#34; sample, there&#8217;s a 98% chance that I&#8217;ve never actually tested it in XamlPad.&#160; That&#8217;s because I&#8217;ve been a kaxaml user since the 0.1 alpha release.
I&#8217;ve always liked kaxaml because [...]]]></description>
			<content:encoded><![CDATA[<div class="jbr">
<p>When answering WPF questions, I like to&nbsp;provide <em>pure markup </em>samples, whenever possible.&nbsp; I&#8217;m going to divulge a little secret now&#8230;&nbsp; Whenever I post such a <em>&quot;XamlPad-ready&quot;</em> sample, there&#8217;s a 98% chance that I&#8217;ve never actually tested it in XamlPad.&nbsp; That&#8217;s because I&#8217;ve been a <a href="http://www.kaxaml.com/">kaxaml</a> user since the 0.1 alpha release.</p>
<p>I&#8217;ve always liked kaxaml because of its &quot;snippets&quot; feature, but now there are&nbsp;<a href="http://notstatic.com/archives/121/trackback">many new reasons</a> to really <em><strong>love</strong></em> this software.&nbsp; (I&#8217;m not just saying this because <a href="http://notstatic.com/archives/119/trackback">Robby likes my snippets</a>, either!)&nbsp; Seriously, check it out!</p>
<p>(Ahhh&#8230; finally, I can cut and paste code directly from kaxaml into my forum posts&#8230; no more interim step of pasting into VS to get the syntax coloring!&nbsp; And intellisense too!&nbsp; Life is good.&nbsp;:D)</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://drwpf.com/blog/2008/01/08/worth-the-wait-this-app-karocks/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
