<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" version="2.0">
  <channel>
    <title>ploeh blog - WPF</title>
    <link>http://blog.ploeh.dk/</link>
    <description>Mark Seemann's .NET blog</description>
    <language>en-us</language>
    <copyright>Mark Seemann</copyright>
    <lastBuildDate>Thu, 26 Nov 2009 21:23:46 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>mark@seemann.ms</managingEditor>
    <webMaster>mark@seemann.ms</webMaster>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=9da2b80c-55e5-41e9-9940-f660e561204a</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,9da2b80c-55e5-41e9-9940-f660e561204a.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,9da2b80c-55e5-41e9-9940-f660e561204a.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=9da2b80c-55e5-41e9-9940-f660e561204a</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In unit testing an important step is to exercise the <a href="http://xunitpatterns.com/SUT.html">SUT</a>.
The member you want to invoke is often a method that takes one or more parameters,
but in some test cases you don't care about the values of those parameters – you just
want to invoke the method.
</p>
        <p>
You can always make up one or more <a href="http://xunitpatterns.com/Dummy%20Object.html">Dummy</a> parameters
and pass them to the method in question, but you could also use one of <a href="http://autofixture.codeplex.com/">AutoFixture</a>'s
Do convenience methods. There are several overloads that all take delegates that specify
the action in question while providing you with Dummies of any parameters you don't
care about.
</p>
        <p>
A good example is WPF's <a href="http://msdn.microsoft.com/en-us/library/system.windows.input.icommand.aspx">ICommand</a> interface.
The most prevalent method is the Execute method that takes a single <em>parameter</em> parameter:
</p>
        <div style="font-family: consolas; background: white; color: black; font-size: 10pt">
          <p style="margin: 0px">
            <span style="color: blue">void</span> Execute(<span style="color: blue">object</span> parameter);
</p>
        </div>
        <p>
Most of the time we don't really care about the parameter because we only care that
the command was invoked. However, we still need to supply a value for the parameter
when we unit test our ICommand implementations. Obviously, we could just pass in a
new System.Object every time, but why not let AutoFixture take care of that for us?
</p>
        <p>
(You may think that new'ing up a System.Object is something you can easily do yourself,
but imagine other APIs that require much more complex input parameters, and you should
begin to see the potential.)
</p>
        <p>
Here's a state-based unit test that verifies that the Message property of the MyViewModel
class has the correct value after the FooCommand has been invoked:
</p>
        <div style="font-family: consolas; background: white; color: black; font-size: 10pt">
          <p style="margin: 0px">
[<span style="color: #2b91af">TestMethod</span>]
</p>
          <p style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">void</span> FooWillUpdateMessage()
</p>
          <p style="margin: 0px">
{
</p>
          <p style="margin: 0px">
    <span style="color: green">// Fixture setup</span></p>
          <p style="margin: 0px">
    <span style="color: blue">var</span> fixture = <span style="color: blue">new</span><span style="color: #2b91af">Fixture</span>();
</p>
          <p style="margin: 0px">
    <span style="color: blue">var</span> sut = fixture.CreateAnonymous&lt;<span style="color: #2b91af">MyViewModel</span>&gt;();
</p>
          <p style="margin: 0px">
    <span style="color: green">// Exercise system</span></p>
          <p style="margin: 0px">
    fixture.Do((<span style="color: blue">object</span> parameter)
=&gt; 
</p>
          <p style="margin: 0px">
        sut.FooCommand.Execute(parameter));
</p>
          <p style="margin: 0px">
    <span style="color: green">// Verify outcome</span></p>
          <p style="margin: 0px">
    <span style="color: #2b91af">Assert</span>.AreEqual(<span style="color: #a31515">"Foo"</span>,
sut.Message, <span style="color: #a31515">"Message"</span>);
</p>
          <p style="margin: 0px">
    <span style="color: green">// Teardown</span></p>
          <p style="margin: 0px">
}
</p>
        </div>
        <p>
Notice how the Do method takes an Action&lt;object&gt; to specify the method to invoke.
AutoFixture automatically supplies an instance for the <em>parameter</em> parameter
using the same engine to create <a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx">Anonymous
variables</a> that it uses for everything else.
</p>
        <p>
The Do method in question is really a generic method with this signature:
</p>
        <div style="font-family: consolas; background: white; color: black; font-size: 10pt">
          <p style="margin: 0px">
            <span style="color: blue">public</span>
            <span style="color: blue">void</span> Do&lt;T&gt;(<span style="color: #2b91af">Action</span>&lt;T&gt;
action);
</p>
        </div>
        <p>
There are also overloads that take two, three or four input parameters, corresponding
to the available Action types available in the BCL.
</p>
        <p>
These methods are simply convenience methods that allow you to express your test code
more succinctly than you would otherwise have been able to do.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=9da2b80c-55e5-41e9-9940-f660e561204a" />
      </body>
      <title>Anonymous Do</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,9da2b80c-55e5-41e9-9940-f660e561204a.aspx</guid>
      <link>http://blog.ploeh.dk/2009/11/26/AnonymousDo.aspx</link>
      <pubDate>Thu, 26 Nov 2009 21:23:46 GMT</pubDate>
      <description>&lt;p&gt;
In unit testing an important step is to exercise the &lt;a href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/a&gt;.
The member you want to invoke is often a method that takes one or more parameters,
but in some test cases you don't care about the values of those parameters – you just
want to invoke the method.
&lt;/p&gt;
&lt;p&gt;
You can always make up one or more &lt;a href="http://xunitpatterns.com/Dummy%20Object.html"&gt;Dummy&lt;/a&gt; parameters
and pass them to the method in question, but you could also use one of &lt;a href="http://autofixture.codeplex.com/"&gt;AutoFixture&lt;/a&gt;'s
Do convenience methods. There are several overloads that all take delegates that specify
the action in question while providing you with Dummies of any parameters you don't
care about.
&lt;/p&gt;
&lt;p&gt;
A good example is WPF's &lt;a href="http://msdn.microsoft.com/en-us/library/system.windows.input.icommand.aspx"&gt;ICommand&lt;/a&gt; interface.
The most prevalent method is the Execute method that takes a single &lt;em&gt;parameter&lt;/em&gt; parameter:
&lt;/p&gt;
&lt;div style="font-family: consolas; background: white; color: black; font-size: 10pt"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;void&lt;/span&gt; Execute(&lt;span style="color: blue"&gt;object&lt;/span&gt; parameter);
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
Most of the time we don't really care about the parameter because we only care that
the command was invoked. However, we still need to supply a value for the parameter
when we unit test our ICommand implementations. Obviously, we could just pass in a
new System.Object every time, but why not let AutoFixture take care of that for us?
&lt;/p&gt;
&lt;p&gt;
(You may think that new'ing up a System.Object is something you can easily do yourself,
but imagine other APIs that require much more complex input parameters, and you should
begin to see the potential.)
&lt;/p&gt;
&lt;p&gt;
Here's a state-based unit test that verifies that the Message property of the MyViewModel
class has the correct value after the FooCommand has been invoked:
&lt;/p&gt;
&lt;div style="font-family: consolas; background: white; color: black; font-size: 10pt"&gt;
&lt;p style="margin: 0px"&gt;
[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; FooWillUpdateMessage()
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
{
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Fixture setup&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; fixture = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Fixture&lt;/span&gt;();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; sut = fixture.CreateAnonymous&amp;lt;&lt;span style="color: #2b91af"&gt;MyViewModel&lt;/span&gt;&amp;gt;();
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Exercise system&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; fixture.Do((&lt;span style="color: blue"&gt;object&lt;/span&gt; parameter)
=&amp;gt; 
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sut.FooCommand.Execute(parameter));
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Verify outcome&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(&lt;span style="color: #a31515"&gt;"Foo"&lt;/span&gt;,
sut.Message, &lt;span style="color: #a31515"&gt;"Message"&lt;/span&gt;);
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Teardown&lt;/span&gt;
&lt;/p&gt;
&lt;p style="margin: 0px"&gt;
}
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
Notice how the Do method takes an Action&amp;lt;object&amp;gt; to specify the method to invoke.
AutoFixture automatically supplies an instance for the &lt;em&gt;parameter&lt;/em&gt; parameter
using the same engine to create &lt;a href="http://blogs.msdn.com/ploeh/archive/2008/11/17/anonymous-variables.aspx"&gt;Anonymous
variables&lt;/a&gt; that it uses for everything else.
&lt;/p&gt;
&lt;p&gt;
The Do method in question is really a generic method with this signature:
&lt;/p&gt;
&lt;div style="font-family: consolas; background: white; color: black; font-size: 10pt"&gt;
&lt;p style="margin: 0px"&gt;
&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Do&amp;lt;T&amp;gt;(&lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;T&amp;gt;
action);
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
There are also overloads that take two, three or four input parameters, corresponding
to the available Action types available in the BCL.
&lt;/p&gt;
&lt;p&gt;
These methods are simply convenience methods that allow you to express your test code
more succinctly than you would otherwise have been able to do.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=9da2b80c-55e5-41e9-9940-f660e561204a" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,9da2b80c-55e5-41e9-9940-f660e561204a.aspx</comments>
      <category>AutoFixture</category>
      <category>Unit Testing</category>
      <category>WPF</category>
    </item>
    <item>
      <trackback:ping>http://blog.ploeh.dk/Trackback.aspx?guid=2691f07e-2813-4c4e-a861-43d7d04be692</trackback:ping>
      <pingback:server>http://blog.ploeh.dk/pingback.aspx</pingback:server>
      <pingback:target>http://blog.ploeh.dk/PermaLink,guid,2691f07e-2813-4c4e-a861-43d7d04be692.aspx</pingback:target>
      <dc:creator>Mark Seemann</dc:creator>
      <wfw:comment>http://blog.ploeh.dk/CommentView,guid,2691f07e-2813-4c4e-a861-43d7d04be692.aspx</wfw:comment>
      <wfw:commentRss>http://blog.ploeh.dk/SyndicationService.asmx/GetEntryCommentsRss?guid=2691f07e-2813-4c4e-a861-43d7d04be692</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
The expressiveness of WPF is amazing. I particularly like the databinding and templating
features. The ability to selectively render an object based on its type is very strong.
</p>
        <p>
When I recently began working with ASP.NET MVC (which I like so far), I quickly ran
into a scenario where I would have liked to have WPF's DataTemplates at my disposal.
Maybe it's just because I've become used to WPF, but I missed the feature and set
out to find out if something similar is possible in ASP.NET MVC.
</p>
        <p>
Before we dive into that, I'd like to present the 'problem' in WPF terms, but the
underlying View Model that I want to expose will be shared between both solutions.
</p>
        <p>
          <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="PresentationModel" border="0" alt="PresentationModel" src="http://blog.ploeh.dk/content/binary/WindowsLiveWriter/DataTemplatingInASP.NETMVC_13643/PresentationModel_3.png" width="619" height="629" />
        </p>
        <p>
The main point is that the <em>Items</em> property exposes a polymorphic list. While
all items in this list share a common property (Name), they are otherwise different;
one contains a piece of Text, one contains a Color, and one is a complex item that
contains child items.
</p>
        <p>
When I render this list, I want each item to render according to its type.
</p>
        <p>
In WPF, this is fairly easy to accomplish with DataTemplates:
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red163\green21\blue21;\red255\green0\blue0;\red0\green0\blue0;}??\fs20 \cf1 &lt;\cf3 ListBox.Resources\cf1 &gt;\par ??\cf3     \cf1 &lt;\cf3 DataTemplate\cf4  DataType\cf1 ="\{\cf3 x\cf1 :\cf3 Type\cf4  pm\cf1 :\cf4 NamedTextItem\cf1 \}"&gt;\par ??\cf3         \cf1 &lt;\cf3 StackPanel\cf1 &gt;\par ??\cf3             \cf1 &lt;\cf3 TextBlock\cf4  Text\cf1 ="\{\cf3 Binding\cf4  Path\cf1 =Name\}"\par ??\cf0                       \cf4  FontWeight\cf1 ="bold" /&gt;\par ??\cf3             \cf1 &lt;\cf3 TextBlock\cf4  Text\cf1 ="\{\cf3 Binding\cf4  Path\cf1 =Text\}" /&gt;\par ??\cf3         \cf1 &lt;/\cf3 StackPanel\cf1 &gt;\par ??\cf3     \cf1 &lt;/\cf3 DataTemplate\cf1 &gt;\par ??\cf3     \cf1 &lt;\cf3 DataTemplate\cf4  DataType\cf1 ="\{\cf3 x\cf1 :\cf3 Type\cf4  pm\cf1 :\cf4 NamedColorItem\cf1 \}"&gt;\par ??\cf3         \cf1 &lt;\cf3 StackPanel\cf1 &gt;\par ??\cf3             \cf1 &lt;\cf3 TextBlock\cf4  Text\cf1 ="\{\cf3 Binding\cf4  Path\cf1 =Name\}"\par ??\cf0                       \cf4  FontWeight\cf1 ="bold" /&gt;\par ??\cf3             \cf1 &lt;\cf3 Ellipse\cf4  Height\cf1 ="25"\cf4  Width\cf1 ="25"\par ??\cf0                     \cf4  HorizontalAlignment\cf1 ="Left"\par ??\cf0                     \cf4  Fill\cf1 ="\{\cf3 Binding\cf4  Path\cf1 =Brush\}"\par ??\cf0                     \cf4  Stroke\cf1 ="DarkGray" /&gt;\par ??\cf3         \cf1 &lt;/\cf3 StackPanel\cf1 &gt;\par ??\cf3     \cf1 &lt;/\cf3 DataTemplate\cf1 &gt;\par ??\cf3     \cf1 &lt;\cf3 DataTemplate\cf4  DataType\cf1 ="\{\cf3 x\cf1 :\cf3 Type\cf4  pm\cf1 :\cf4 NamedComplexItem\cf1 \}"&gt;\par ??\cf3         \cf1 &lt;\cf3 StackPanel\cf1 &gt;\par ??\cf3             \cf1 &lt;\cf3 TextBlock\cf4  Text\cf1 ="\{\cf3 Binding\cf4  Path\cf1 =Name\}"\par ??\cf0                       \cf4  FontWeight\cf1 ="bold" /&gt;\par ??\cf3             \cf1 &lt;\cf3 ListBox\cf4  ItemsSource\cf1 ="\{\cf3 Binding\cf4  Path\cf1 =Children\}"\par ??\cf0                     \cf4  BorderThickness\cf1 ="0"&gt;\par ??\cf3                 \cf1 &lt;\cf3 ListBox.Resources\cf1 &gt;\par ??\cf3                     \cf1 &lt;\cf3 DataTemplate\par ??\cf0                        \cf4  DataType\cf1 ="\{\cf3 x\cf1 :\cf3 Type\cf4  pm\cf1 :\cf4 ChildItem\cf1 \}"&gt;\par ??\cf3                         \cf1 &lt;\cf3 TextBlock\par ??\cf0                            \cf4  Text\cf1 ="\{\cf3 Binding\cf4  Path\cf1 =Text\}" /&gt;\par ??\cf3                     \cf1 &lt;/\cf3 DataTemplate\cf1 &gt;\par ??\cf3                 \cf1 &lt;/\cf3 ListBox.Resources\cf1 &gt;\par ??\cf3             \cf1 &lt;/\cf3 ListBox\cf1 &gt;\par ??\cf3         \cf1 &lt;/\cf3 StackPanel\cf1 &gt;\par ??\cf3     \cf1 &lt;/\cf3 DataTemplate\cf1 &gt;\par ??&lt;/\cf3 ListBox.Resources\cf1 &gt;}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">&lt;</span>
            <span style="color: #a31515">ListBox.Resources</span>
            <span style="color: blue">&gt;</span>
          </pre>
          <pre style="margin: 0px">
            <span style="color: #a31515">    </span>
            <span style="color: blue">&lt;</span>
            <span style="color: #a31515">DataTemplate</span>
            <span style="color: red"> DataType</span>
            <span style="color: blue">="{</span>
            <span style="color: #a31515">x</span>
            <span style="color: blue">:</span>
            <span style="color: #a31515">Type</span>
            <span style="color: red"> pm</span>
            <span style="color: blue">:</span>
            <span style="color: red">NamedTextItem</span>
            <span style="color: blue">}"&gt;</span>
          </pre>
          <pre style="margin: 0px">
            <span style="color: #a31515">        </span>
            <span style="color: blue">&lt;</span>
            <span style="color: #a31515">StackPanel</span>
            <span style="color: blue">&gt;</span>
          </pre>
          <pre style="margin: 0px">
            <span style="color: #a31515">            </span>
            <span style="color: blue">&lt;</span>
            <span style="color: #a31515">TextBlock</span>
            <span style="color: red"> Text</span>
            <span style="color: blue">="{</span>
            <span style="color: #a31515">Binding</span>
            <span style="color: red"> Path</span>
            <span style="color: blue">=Name}"</span>
          </pre>
          <pre style="margin: 0px">                      <span style="color: red"> FontWeight</span><span style="color: blue">="bold"
/&gt;</span></pre>
          <pre style="margin: 0px">
            <span style="color: #a31515">            </span>
            <span style="color: blue">&lt;</span>
            <span style="color: #a31515">TextBlock</span>
            <span style="color: red"> Text</span>
            <span style="color: blue">="{</span>
            <span style="color: #a31515">Binding</span>
            <span style="color: red"> Path</span>
            <span style="color: blue">=Text}"
/&gt;</span>
          </pre>
          <pre style="margin: 0px">
            <span style="color: #a31515">        </span>
            <span style="color: blue">&lt;/</span>
            <span style="color: #a31515">StackPanel</span>
            <span style="color: blue">&gt;</span>
          </pre>
          <pre style="margin: 0px">
            <span style="color: #a31515">    </span>
            <span style="color: blue">&lt;/</span>
            <span style="color: #a31515">DataTemplate</span>
            <span style="color: blue">&gt;</span>
          </pre>
          <pre style="margin: 0px">
            <span style="color: #a31515">    </span>
            <span style="color: blue">&lt;</span>
            <span style="color: #a31515">DataTemplate</span>
            <span style="color: red"> DataType</span>
            <span style="color: blue">="{</span>
            <span style="color: #a31515">x</span>
            <span style="color: blue">:</span>
            <span style="color: #a31515">Type</span>
            <span style="color: red"> pm</span>
            <span style="color: blue">:</span>
            <span style="color: red">NamedColorItem</span>
            <span style="color: blue">}"&gt;</span>
          </pre>
          <pre style="margin: 0px">
            <span style="color: #a31515">        </span>
            <span style="color: blue">&lt;</span>
            <span style="color: #a31515">StackPanel</span>
            <span style="color: blue">&gt;</span>
          </pre>
          <pre style="margin: 0px">
            <span style="color: #a31515">            </span>
            <span style="color: blue">&lt;</span>
            <span style="color: #a31515">TextBlock</span>
            <span style="color: red"> Text</span>
            <span style="color: blue">="{</span>
            <span style="color: #a31515">Binding</span>
            <span style="color: red"> Path</span>
            <span style="color: blue">=Name}"</span>
          </pre>
          <pre style="margin: 0px">                      <span style="color: red"> FontWeight</span><span style="color: blue">="bold"
/&gt;</span></pre>
          <pre style="margin: 0px">
            <span style="color: #a31515">            </span>
            <span style="color: blue">&lt;</span>
            <span style="color: #a31515">Ellipse</span>
            <span style="color: red"> Height</span>
            <span style="color: blue">="25"</span>
            <span style="color: red"> Width</span>
            <span style="color: blue">="25"</span>
          </pre>
          <pre style="margin: 0px">                    <span style="color: red"> HorizontalAlignment</span><span style="color: blue">="Left"</span></pre>
          <pre style="margin: 0px">                    <span style="color: red"> Fill</span><span style="color: blue">="{</span><span style="color: #a31515">Binding</span><span style="color: red"> Path</span><span style="color: blue">=Brush}"</span></pre>
          <pre style="margin: 0px">                    <span style="color: red"> Stroke</span><span style="color: blue">="DarkGray"
/&gt;</span></pre>
          <pre style="margin: 0px">
            <span style="color: #a31515">        </span>
            <span style="color: blue">&lt;/</span>
            <span style="color: #a31515">StackPanel</span>
            <span style="color: blue">&gt;</span>
          </pre>
          <pre style="margin: 0px">
            <span style="color: #a31515">    </span>
            <span style="color: blue">&lt;/</span>
            <span style="color: #a31515">DataTemplate</span>
            <span style="color: blue">&gt;</span>
          </pre>
          <pre style="margin: 0px">
            <span style="color: #a31515">    </span>
            <span style="color: blue">&lt;</span>
            <span style="color: #a31515">DataTemplate</span>
            <span style="color: red"> DataType</span>
            <span style="color: blue">="{</span>
            <span style="color: #a31515">x</span>
            <span style="color: blue">:</span>
            <span style="color: #a31515">Type</span>
            <span style="color: red"> pm</span>
            <span style="color: blue">:</span>
            <span style="color: red">NamedComplexItem</span>
            <span style="color: blue">}"&gt;</span>
          </pre>
          <pre style="margin: 0px">
            <span style="color: #a31515">        </span>
            <span style="color: blue">&lt;</span>
            <span style="color: #a31515">StackPanel</span>
            <span style="color: blue">&gt;</span>
          </pre>
          <pre style="margin: 0px">
            <span style="color: #a31515">            </span>
            <span style="color: blue">&lt;</span>
            <span style="color: #a31515">TextBlock</span>
            <span style="color: red"> Text</span>
            <span style="color: blue">="{</span>
            <span style="color: #a31515">Binding</span>
            <span style="color: red"> Path</span>
            <span style="color: blue">=Name}"</span>
          </pre>
          <pre style="margin: 0px">                      <span style="color: red"> FontWeight</span><span style="color: blue">="bold"
/&gt;</span></pre>
          <pre style="margin: 0px">
            <span style="color: #a31515">            </span>
            <span style="color: blue">&lt;</span>
            <span style="color: #a31515">ListBox</span>
            <span style="color: red"> ItemsSource</span>
            <span style="color: blue">="{</span>
            <span style="color: #a31515">Binding</span>
            <span style="color: red"> Path</span>
            <span style="color: blue">=Children}"</span>
          </pre>
          <pre style="margin: 0px">                    <span style="color: red"> BorderThickness</span><span style="color: blue">="0"&gt;</span></pre>
          <pre style="margin: 0px">
            <span style="color: #a31515">                </span>
            <span style="color: blue">&lt;</span>
            <span style="color: #a31515">ListBox.Resources</span>
            <span style="color: blue">&gt;</span>
          </pre>
          <pre style="margin: 0px">
            <span style="color: #a31515">                    </span>
            <span style="color: blue">&lt;</span>
            <span style="color: #a31515">DataTemplate</span>
          </pre>
          <pre style="margin: 0px">                       <span style="color: red"> DataType</span><span style="color: blue">="{</span><span style="color: #a31515">x</span><span style="color: blue">:</span><span style="color: #a31515">Type</span><span style="color: red"> pm</span><span style="color: blue">:</span><span style="color: red">ChildItem</span><span style="color: blue">}"&gt;</span></pre>
          <pre style="margin: 0px">
            <span style="color: #a31515">                        </span>
            <span style="color: blue">&lt;</span>
            <span style="color: #a31515">TextBlock</span>
          </pre>
          <pre style="margin: 0px">                           <span style="color: red"> Text</span><span style="color: blue">="{</span><span style="color: #a31515">Binding</span><span style="color: red"> Path</span><span style="color: blue">=Text}"
/&gt;</span></pre>
          <pre style="margin: 0px">
            <span style="color: #a31515">                    </span>
            <span style="color: blue">&lt;/</span>
            <span style="color: #a31515">DataTemplate</span>
            <span style="color: blue">&gt;</span>
          </pre>
          <pre style="margin: 0px">
            <span style="color: #a31515">                </span>
            <span style="color: blue">&lt;/</span>
            <span style="color: #a31515">ListBox.Resources</span>
            <span style="color: blue">&gt;</span>
          </pre>
          <pre style="margin: 0px">
            <span style="color: #a31515">            </span>
            <span style="color: blue">&lt;/</span>
            <span style="color: #a31515">ListBox</span>
            <span style="color: blue">&gt;</span>
          </pre>
          <pre style="margin: 0px">
            <span style="color: #a31515">        </span>
            <span style="color: blue">&lt;/</span>
            <span style="color: #a31515">StackPanel</span>
            <span style="color: blue">&gt;</span>
          </pre>
          <pre style="margin: 0px">
            <span style="color: #a31515">    </span>
            <span style="color: blue">&lt;/</span>
            <span style="color: #a31515">DataTemplate</span>
            <span style="color: blue">&gt;</span>
          </pre>
          <pre style="margin: 0px">
            <span style="color: blue">&lt;/</span>
            <span style="color: #a31515">ListBox.Resources</span>
            <span style="color: blue">&gt;</span>
          </pre>
        </div>
        <p>
Each DataTemplate is contained within a ListBox. When the ListBox binds to each item
in the <em>Items</em> list, it automatically picks the correct template for the item.
</p>
        <p>
The result is something like this:
</p>
        <p>
          <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="WpfApp" border="0" alt="WpfApp" src="http://blog.ploeh.dk/content/binary/WindowsLiveWriter/DataTemplatingInASP.NETMVC_13643/WpfApp_5.png" width="167" height="333" />
        </p>
        <p>
The NamedTextItem is rendered as a box containing the Name and the Text on two separate
lines; the NamedColorItem is rendered as a box containing the Name and a circle filled
with the Color defined by the item; and the NamedComplexItem is rendered as a box
with the Name and each child of the Children list.
</p>
        <p>
This is all implemented declaratively without a single line of imperative UI code.
</p>
        <p>
Is it possible to do the same in ASP.NET MVC?
</p>
        <p>
To my knowledge (but please correct me if I'm wrong), ASP.NET MVC has no explicit
concept of a DataTemplate, so we will have to mimic it. The following describes the
best I've been able to come up with so far.
</p>
        <p>
In ASP.NET MVC, there's no declarative databinding, so we will need to loop through
the list of items. My View page derives from ViewPage&lt;MyViewModel&gt;, so I can
write
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green238\blue98;\red255\green255\blue255;\red0\green0\blue255;\red163\green21\blue21;\red255\green0\blue0;\red0\green128\blue0;}??\fs20 \cb2\highlight2 &lt;%\cb0\highlight0  \cf4 foreach\cf0  (\cf4 var\cf0  item \cf4 in\cf0  \cf4 this\cf0 .Model.Items)\par ??   \{ \cb2\highlight2 %&gt;\par ??\cb0\highlight0    \cf4 &lt;\cf5 div\cf0  \cf6 class\cf4 ="ploeh"&gt;\par ??\cf0    \cb2\highlight2 &lt;%\cb0\highlight0  \cf7 // Render each item \cf0\cb2\highlight2 %&gt;\par ??\cb0\highlight0    \cf4 &lt;/\cf5 div\cf4 &gt;\par ??\cf0\cb2\highlight2 &lt;%\cb0\highlight0  \} \cb2\highlight2 %&gt;}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="background: #ffee62">&lt;%</span>
            <span style="color: blue">foreach</span> (<span style="color: blue">var</span> item <span style="color: blue">in</span><span style="color: blue">this</span>.Model.Items)</pre>
          <pre style="margin: 0px">   { <span style="background: #ffee62">%&gt;</span></pre>
          <pre style="margin: 0px">   <span style="color: blue">&lt;</span><span style="color: #a31515">div</span><span style="color: red">class</span><span style="color: blue">="ploeh"&gt;</span></pre>
          <pre style="margin: 0px">   <span style="background: #ffee62">&lt;%</span><span style="color: green">//
Render each item </span><span style="background: #ffee62">%&gt;</span></pre>
          <pre style="margin: 0px">   <span style="color: blue">&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;</span></pre>
          <pre style="margin: 0px">
            <span style="background: #ffee62">&lt;%</span> } <span style="background: #ffee62">%&gt;</span></pre>
        </div>
        <p>
The challenge is to figure out how to render each item according to its own template.
</p>
        <p>
To define the templates, I create a UserControl for each item. The NamedTextItemUserControl
derives from ViewUserControl&lt;NamedTextItem&gt;, which gives me a strongly typed
Model:
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red163\green21\blue21;\red0\green0\blue0;\red255\green238\blue98;}??\fs20 \cf1 &lt;\cf3 div\cf1 &gt;&lt;\cf3 strong\cf1 &gt;\cf0\cb5\highlight5 &lt;%\cf1\cb0\highlight0 =\cf0  \cf1 this\cf0 .Model.Name \cb5\highlight5 %&gt;\cf1\cb0\highlight0 &lt;/\cf3 strong\cf1 &gt;&lt;/\cf3 div\cf1 &gt;\par ??&lt;\cf3 div\cf1 &gt;\cf0\cb5\highlight5 &lt;%\cf1\cb0\highlight0 =\cf0  \cf1 this\cf0 .Model.Text \cb5\highlight5 %&gt;\cf1\cb0\highlight0 &lt;/\cf3 div\cf1 &gt;}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="color: blue">&lt;</span>
            <span style="color: #a31515">div</span>
            <span style="color: blue">&gt;&lt;</span>
            <span style="color: #a31515">strong</span>
            <span style="color: blue">&gt;</span>
            <span style="background: #ffee62">&lt;%</span>
            <span style="color: blue">=</span>
            <span style="color: blue">this</span>.Model.Name <span style="background: #ffee62">%&gt;</span><span style="color: blue">&lt;/</span><span style="color: #a31515">strong</span><span style="color: blue">&gt;&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;</span></pre>
          <pre style="margin: 0px">
            <span style="color: blue">&lt;</span>
            <span style="color: #a31515">div</span>
            <span style="color: blue">&gt;</span>
            <span style="background: #ffee62">&lt;%</span>
            <span style="color: blue">=</span>
            <span style="color: blue">this</span>.Model.Text <span style="background: #ffee62">%&gt;</span><span style="color: blue">&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;</span></pre>
        </div>
        <p>
The other two UserControls are implemented similarly.
</p>
        <p>
A UserControl can be rendered using the RenderPartial extension method, so the only
thing left is to select the correct UserControl name for each item. It would be nice
to be able to do this in markup, like WPF, but I'm not aware of any way that is possible.
</p>
        <p>
I will have to resort to code, but we can at least strive for code that is as declarative
in style as possible.
</p>
        <p>
First, I need to define the map from type to UserControl:
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green238\blue98;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;\red163\green21\blue21;}??\fs20 \cb2\highlight2 &lt;%\par ??\cb0\highlight0     \cf4 var\cf0  dataTemplates = \cf4 new\cf0  \cf5 Dictionary\cf0 &lt;\cf5 Type\cf0 , \cf4 string\cf0 &gt;();\par ??    dataTemplates[\cf4 typeof\cf0 (\cf5 NamedTextItem\cf0 )] =\par ??        \cf6 "NamedTextItemUserControl"\cf0 ;\par ??    dataTemplates[\cf4 typeof\cf0 (\cf5 NamedColorItem\cf0 )] =\par ??        \cf6 "NamedColorItemUserControl"\cf0 ;\par ??    dataTemplates[\cf4 typeof\cf0 (\cf5 NamedComplexItem\cf0 )] =\par ??        \cf6 "NamedComplexItemUserControl"\cf0 ;\par ??\cb2\highlight2 %&gt;}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="background: #ffee62">&lt;%</span>
          </pre>
          <pre style="margin: 0px">    <span style="color: blue">var</span> dataTemplates
= <span style="color: blue">new</span><span style="color: #2b91af">Dictionary</span>&lt;<span style="color: #2b91af">Type</span>, <span style="color: blue">string</span>&gt;();</pre>
          <pre style="margin: 0px">    dataTemplates[<span style="color: blue">typeof</span>(<span style="color: #2b91af">NamedTextItem</span>)]
=</pre>
          <pre style="margin: 0px">        <span style="color: #a31515">"NamedTextItemUserControl"</span>;</pre>
          <pre style="margin: 0px">    dataTemplates[<span style="color: blue">typeof</span>(<span style="color: #2b91af">NamedColorItem</span>)]
=</pre>
          <pre style="margin: 0px">        <span style="color: #a31515">"NamedColorItemUserControl"</span>;</pre>
          <pre style="margin: 0px">    dataTemplates[<span style="color: blue">typeof</span>(<span style="color: #2b91af">NamedComplexItem</span>)]
=</pre>
          <pre style="margin: 0px">        <span style="color: #a31515">"NamedComplexItemUserControl"</span>;</pre>
          <pre style="margin: 0px">
            <span style="background: #ffee62">%&gt;</span>
          </pre>
        </div>
        <p>
Next, I can use this map to render each item correctly:
</p>
        <!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green238\blue98;\red255\green255\blue255;\red0\green0\blue255;\red163\green21\blue21;\red255\green0\blue0;\red0\green128\blue0;}??\fs20 \cb2\highlight2 &lt;%\cb0\highlight0  \cf4 foreach\cf0  (\cf4 var\cf0  item \cf4 in\cf0  \cf4 this\cf0 .Model.Items)\par ??   \{ \cb2\highlight2 %&gt;\par ??\cb0\highlight0    \cf4 &lt;\cf5 div\cf0  \cf6 class\cf4 ="ploeh"&gt;\par ??\cf0    \cb2\highlight2 &lt;%\cb0\highlight0  \cf7 // Render each item \cf0\cb2\highlight2 %&gt;\par ??\cb0\highlight0    \cb2\highlight2 &lt;%\cb0\highlight0  \cf4 this\cf0 .Html.RenderPartial(dataTemplates[item.GetType()],\par ??                             item); \cb2\highlight2 %&gt;\par ??\cb0\highlight0    \cf4 &lt;/\cf5 div\cf4 &gt;\par ??\cf0\cb2\highlight2 &lt;%\cb0\highlight0  \} \cb2\highlight2 %&gt;}
-->
        <div style="font-family: courier new; background: white; color: black; font-size: 10pt">
          <pre style="margin: 0px">
            <span style="background: #ffee62">&lt;%</span>
            <span style="color: blue">foreach</span> (<span style="color: blue">var</span> item <span style="color: blue">in</span><span style="color: blue">this</span>.Model.Items)</pre>
          <pre style="margin: 0px">   { <span style="background: #ffee62">%&gt;</span></pre>
          <pre style="margin: 0px">   <span style="color: blue">&lt;</span><span style="color: #a31515">div</span><span style="color: red">class</span><span style="color: blue">="ploeh"&gt;</span></pre>
          <pre style="margin: 0px">   <span style="background: #ffee62">&lt;%</span><span style="color: green">//
Render each item </span><span style="background: #ffee62">%&gt;</span></pre>
          <pre style="margin: 0px">   <span style="background: #ffee62">&lt;%</span><span style="color: blue">this</span>.Html.RenderPartial(dataTemplates[item.GetType()],</pre>
          <pre style="margin: 0px">                            item); <span style="background: #ffee62">%&gt;</span></pre>
          <pre style="margin: 0px">   <span style="color: blue">&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;</span></pre>
          <pre style="margin: 0px">
            <span style="background: #ffee62">&lt;%</span> } <span style="background: #ffee62">%&gt;</span></pre>
        </div>
        <p>
This is definitely less pretty than with WPF, but if you overlook the aesthetics and
focus on the structure of the code, it's darn close to markup. The Cyclomatic Complexity
of the page is only <em>2</em>, and that's even because of the <em>foreach</em> statement
that we need in any case.
</p>
        <p>
The resulting page looks like this:
</p>
        <p>
          <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="AspNetMvcApp" border="0" alt="AspNetMvcApp" src="http://blog.ploeh.dk/content/binary/WindowsLiveWriter/DataTemplatingInASP.NETMVC_13643/AspNetMvcApp_1.png" width="156" height="246" />
        </p>
        <p>
My HTML skills aren't good enough to draw circles with markup, so I had to replace
them with blocks, but apart from that, the result is pretty much the same.
</p>
        <p>
A potential improvement on this technique could be to embed the knowledge of the UserControl
into each item. ASP.NET MVC Controllers already know of Views in an abstract sense,
so letting the View Model know about a UserControl (identified as a string) may be
conceptually sound.
</p>
        <p>
The advantage would be that we could get rid of the Dictionary in the ViewPage and
instead let the item itself tell us the name of the UserControl that should be used
to render it.
</p>
        <p>
The disadvantage would be that we lose some flexibility. It would then require a recompilation
of the application if we wanted to render an item using a different UserControl.
</p>
        <p>
The technique outlined here represents an explorative work in progress, so comments
are welcome.
</p>
        <img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=2691f07e-2813-4c4e-a861-43d7d04be692" />
      </body>
      <title>DataTemplating In ASP.NET MVC</title>
      <guid isPermaLink="false">http://blog.ploeh.dk/PermaLink,guid,2691f07e-2813-4c4e-a861-43d7d04be692.aspx</guid>
      <link>http://blog.ploeh.dk/2009/07/16/DataTemplatingInASPNETMVC.aspx</link>
      <pubDate>Thu, 16 Jul 2009 19:33:48 GMT</pubDate>
      <description>&lt;p&gt;
The expressiveness of WPF is amazing. I particularly like the databinding and templating
features. The ability to selectively render an object based on its type is very strong.
&lt;/p&gt;
&lt;p&gt;
When I recently began working with ASP.NET MVC (which I like so far), I quickly ran
into a scenario where I would have liked to have WPF's DataTemplates at my disposal.
Maybe it's just because I've become used to WPF, but I missed the feature and set
out to find out if something similar is possible in ASP.NET MVC.
&lt;/p&gt;
&lt;p&gt;
Before we dive into that, I'd like to present the 'problem' in WPF terms, but the
underlying View Model that I want to expose will be shared between both solutions.
&lt;/p&gt;
&lt;p&gt;
&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="PresentationModel" border="0" alt="PresentationModel" src="http://blog.ploeh.dk/content/binary/WindowsLiveWriter/DataTemplatingInASP.NETMVC_13643/PresentationModel_3.png" width="619" height="629"&gt; 
&lt;/p&gt;
&lt;p&gt;
The main point is that the &lt;em&gt;Items&lt;/em&gt; property exposes a polymorphic list. While
all items in this list share a common property (Name), they are otherwise different;
one contains a piece of Text, one contains a Color, and one is a complex item that
contains child items.
&lt;/p&gt;
&lt;p&gt;
When I render this list, I want each item to render according to its type.
&lt;/p&gt;
&lt;p&gt;
In WPF, this is fairly easy to accomplish with DataTemplates:
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red163\green21\blue21;\red255\green0\blue0;\red0\green0\blue0;}??\fs20 \cf1 &amp;lt;\cf3 ListBox.Resources\cf1 &amp;gt;\par ??\cf3     \cf1 &amp;lt;\cf3 DataTemplate\cf4  DataType\cf1 ="\{\cf3 x\cf1 :\cf3 Type\cf4  pm\cf1 :\cf4 NamedTextItem\cf1 \}"&amp;gt;\par ??\cf3         \cf1 &amp;lt;\cf3 StackPanel\cf1 &amp;gt;\par ??\cf3             \cf1 &amp;lt;\cf3 TextBlock\cf4  Text\cf1 ="\{\cf3 Binding\cf4  Path\cf1 =Name\}"\par ??\cf0                       \cf4  FontWeight\cf1 ="bold" /&amp;gt;\par ??\cf3             \cf1 &amp;lt;\cf3 TextBlock\cf4  Text\cf1 ="\{\cf3 Binding\cf4  Path\cf1 =Text\}" /&amp;gt;\par ??\cf3         \cf1 &amp;lt;/\cf3 StackPanel\cf1 &amp;gt;\par ??\cf3     \cf1 &amp;lt;/\cf3 DataTemplate\cf1 &amp;gt;\par ??\cf3     \cf1 &amp;lt;\cf3 DataTemplate\cf4  DataType\cf1 ="\{\cf3 x\cf1 :\cf3 Type\cf4  pm\cf1 :\cf4 NamedColorItem\cf1 \}"&amp;gt;\par ??\cf3         \cf1 &amp;lt;\cf3 StackPanel\cf1 &amp;gt;\par ??\cf3             \cf1 &amp;lt;\cf3 TextBlock\cf4  Text\cf1 ="\{\cf3 Binding\cf4  Path\cf1 =Name\}"\par ??\cf0                       \cf4  FontWeight\cf1 ="bold" /&amp;gt;\par ??\cf3             \cf1 &amp;lt;\cf3 Ellipse\cf4  Height\cf1 ="25"\cf4  Width\cf1 ="25"\par ??\cf0                     \cf4  HorizontalAlignment\cf1 ="Left"\par ??\cf0                     \cf4  Fill\cf1 ="\{\cf3 Binding\cf4  Path\cf1 =Brush\}"\par ??\cf0                     \cf4  Stroke\cf1 ="DarkGray" /&amp;gt;\par ??\cf3         \cf1 &amp;lt;/\cf3 StackPanel\cf1 &amp;gt;\par ??\cf3     \cf1 &amp;lt;/\cf3 DataTemplate\cf1 &amp;gt;\par ??\cf3     \cf1 &amp;lt;\cf3 DataTemplate\cf4  DataType\cf1 ="\{\cf3 x\cf1 :\cf3 Type\cf4  pm\cf1 :\cf4 NamedComplexItem\cf1 \}"&amp;gt;\par ??\cf3         \cf1 &amp;lt;\cf3 StackPanel\cf1 &amp;gt;\par ??\cf3             \cf1 &amp;lt;\cf3 TextBlock\cf4  Text\cf1 ="\{\cf3 Binding\cf4  Path\cf1 =Name\}"\par ??\cf0                       \cf4  FontWeight\cf1 ="bold" /&amp;gt;\par ??\cf3             \cf1 &amp;lt;\cf3 ListBox\cf4  ItemsSource\cf1 ="\{\cf3 Binding\cf4  Path\cf1 =Children\}"\par ??\cf0                     \cf4  BorderThickness\cf1 ="0"&amp;gt;\par ??\cf3                 \cf1 &amp;lt;\cf3 ListBox.Resources\cf1 &amp;gt;\par ??\cf3                     \cf1 &amp;lt;\cf3 DataTemplate\par ??\cf0                        \cf4  DataType\cf1 ="\{\cf3 x\cf1 :\cf3 Type\cf4  pm\cf1 :\cf4 ChildItem\cf1 \}"&amp;gt;\par ??\cf3                         \cf1 &amp;lt;\cf3 TextBlock\par ??\cf0                            \cf4  Text\cf1 ="\{\cf3 Binding\cf4  Path\cf1 =Text\}" /&amp;gt;\par ??\cf3                     \cf1 &amp;lt;/\cf3 DataTemplate\cf1 &amp;gt;\par ??\cf3                 \cf1 &amp;lt;/\cf3 ListBox.Resources\cf1 &amp;gt;\par ??\cf3             \cf1 &amp;lt;/\cf3 ListBox\cf1 &amp;gt;\par ??\cf3         \cf1 &amp;lt;/\cf3 StackPanel\cf1 &amp;gt;\par ??\cf3     \cf1 &amp;lt;/\cf3 DataTemplate\cf1 &amp;gt;\par ??&amp;lt;/\cf3 ListBox.Resources\cf1 &amp;gt;}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;ListBox.Resources&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #a31515"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataTemplate&lt;/span&gt;&lt;span style="color: red"&gt; DataType&lt;/span&gt;&lt;span style="color: blue"&gt;="{&lt;/span&gt;&lt;span style="color: #a31515"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;Type&lt;/span&gt;&lt;span style="color: red"&gt; pm&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;NamedTextItem&lt;/span&gt;&lt;span style="color: blue"&gt;}"&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #a31515"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;StackPanel&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #a31515"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBlock&lt;/span&gt;&lt;span style="color: red"&gt; Text&lt;/span&gt;&lt;span style="color: blue"&gt;="{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding&lt;/span&gt;&lt;span style="color: red"&gt; Path&lt;/span&gt;&lt;span style="color: blue"&gt;=Name}"&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: red"&gt; FontWeight&lt;/span&gt;&lt;span style="color: blue"&gt;="bold"
/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #a31515"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBlock&lt;/span&gt;&lt;span style="color: red"&gt; Text&lt;/span&gt;&lt;span style="color: blue"&gt;="{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding&lt;/span&gt;&lt;span style="color: red"&gt; Path&lt;/span&gt;&lt;span style="color: blue"&gt;=Text}"
/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #a31515"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;StackPanel&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #a31515"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataTemplate&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #a31515"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataTemplate&lt;/span&gt;&lt;span style="color: red"&gt; DataType&lt;/span&gt;&lt;span style="color: blue"&gt;="{&lt;/span&gt;&lt;span style="color: #a31515"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;Type&lt;/span&gt;&lt;span style="color: red"&gt; pm&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;NamedColorItem&lt;/span&gt;&lt;span style="color: blue"&gt;}"&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #a31515"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;StackPanel&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #a31515"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBlock&lt;/span&gt;&lt;span style="color: red"&gt; Text&lt;/span&gt;&lt;span style="color: blue"&gt;="{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding&lt;/span&gt;&lt;span style="color: red"&gt; Path&lt;/span&gt;&lt;span style="color: blue"&gt;=Name}"&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: red"&gt; FontWeight&lt;/span&gt;&lt;span style="color: blue"&gt;="bold"
/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #a31515"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;Ellipse&lt;/span&gt;&lt;span style="color: red"&gt; Height&lt;/span&gt;&lt;span style="color: blue"&gt;="25"&lt;/span&gt;&lt;span style="color: red"&gt; Width&lt;/span&gt;&lt;span style="color: blue"&gt;="25"&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: red"&gt; HorizontalAlignment&lt;/span&gt;&lt;span style="color: blue"&gt;="Left"&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: red"&gt; Fill&lt;/span&gt;&lt;span style="color: blue"&gt;="{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding&lt;/span&gt;&lt;span style="color: red"&gt; Path&lt;/span&gt;&lt;span style="color: blue"&gt;=Brush}"&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: red"&gt; Stroke&lt;/span&gt;&lt;span style="color: blue"&gt;="DarkGray"
/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #a31515"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;StackPanel&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #a31515"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataTemplate&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #a31515"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataTemplate&lt;/span&gt;&lt;span style="color: red"&gt; DataType&lt;/span&gt;&lt;span style="color: blue"&gt;="{&lt;/span&gt;&lt;span style="color: #a31515"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;Type&lt;/span&gt;&lt;span style="color: red"&gt; pm&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;NamedComplexItem&lt;/span&gt;&lt;span style="color: blue"&gt;}"&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #a31515"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;StackPanel&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #a31515"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBlock&lt;/span&gt;&lt;span style="color: red"&gt; Text&lt;/span&gt;&lt;span style="color: blue"&gt;="{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding&lt;/span&gt;&lt;span style="color: red"&gt; Path&lt;/span&gt;&lt;span style="color: blue"&gt;=Name}"&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: red"&gt; FontWeight&lt;/span&gt;&lt;span style="color: blue"&gt;="bold"
/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #a31515"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;ListBox&lt;/span&gt;&lt;span style="color: red"&gt; ItemsSource&lt;/span&gt;&lt;span style="color: blue"&gt;="{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding&lt;/span&gt;&lt;span style="color: red"&gt; Path&lt;/span&gt;&lt;span style="color: blue"&gt;=Children}"&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: red"&gt; BorderThickness&lt;/span&gt;&lt;span style="color: blue"&gt;="0"&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #a31515"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;ListBox.Resources&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #a31515"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataTemplate&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: red"&gt; DataType&lt;/span&gt;&lt;span style="color: blue"&gt;="{&lt;/span&gt;&lt;span style="color: #a31515"&gt;x&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;Type&lt;/span&gt;&lt;span style="color: red"&gt; pm&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: red"&gt;ChildItem&lt;/span&gt;&lt;span style="color: blue"&gt;}"&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #a31515"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBlock&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: red"&gt; Text&lt;/span&gt;&lt;span style="color: blue"&gt;="{&lt;/span&gt;&lt;span style="color: #a31515"&gt;Binding&lt;/span&gt;&lt;span style="color: red"&gt; Path&lt;/span&gt;&lt;span style="color: blue"&gt;=Text}"
/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #a31515"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataTemplate&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #a31515"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;ListBox.Resources&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #a31515"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;ListBox&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #a31515"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;StackPanel&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: #a31515"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;DataTemplate&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;ListBox.Resources&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Each DataTemplate is contained within a ListBox. When the ListBox binds to each item
in the &lt;em&gt;Items&lt;/em&gt; list, it automatically picks the correct template for the item.
&lt;/p&gt;
&lt;p&gt;
The result is something like this:
&lt;/p&gt;
&lt;p&gt;
&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="WpfApp" border="0" alt="WpfApp" src="http://blog.ploeh.dk/content/binary/WindowsLiveWriter/DataTemplatingInASP.NETMVC_13643/WpfApp_5.png" width="167" height="333"&gt; 
&lt;/p&gt;
&lt;p&gt;
The NamedTextItem is rendered as a box containing the Name and the Text on two separate
lines; the NamedColorItem is rendered as a box containing the Name and a circle filled
with the Color defined by the item; and the NamedComplexItem is rendered as a box
with the Name and each child of the Children list.
&lt;/p&gt;
&lt;p&gt;
This is all implemented declaratively without a single line of imperative UI code.
&lt;/p&gt;
&lt;p&gt;
Is it possible to do the same in ASP.NET MVC?
&lt;/p&gt;
&lt;p&gt;
To my knowledge (but please correct me if I'm wrong), ASP.NET MVC has no explicit
concept of a DataTemplate, so we will have to mimic it. The following describes the
best I've been able to come up with so far.
&lt;/p&gt;
&lt;p&gt;
In ASP.NET MVC, there's no declarative databinding, so we will need to loop through
the list of items. My View page derives from ViewPage&amp;lt;MyViewModel&amp;gt;, so I can
write
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green238\blue98;\red255\green255\blue255;\red0\green0\blue255;\red163\green21\blue21;\red255\green0\blue0;\red0\green128\blue0;}??\fs20 \cb2\highlight2 &amp;lt;%\cb0\highlight0  \cf4 foreach\cf0  (\cf4 var\cf0  item \cf4 in\cf0  \cf4 this\cf0 .Model.Items)\par ??   \{ \cb2\highlight2 %&amp;gt;\par ??\cb0\highlight0    \cf4 &amp;lt;\cf5 div\cf0  \cf6 class\cf4 ="ploeh"&amp;gt;\par ??\cf0    \cb2\highlight2 &amp;lt;%\cb0\highlight0  \cf7 // Render each item \cf0\cb2\highlight2 %&amp;gt;\par ??\cb0\highlight0    \cf4 &amp;lt;/\cf5 div\cf4 &amp;gt;\par ??\cf0\cb2\highlight2 &amp;lt;%\cb0\highlight0  \} \cb2\highlight2 %&amp;gt;}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt; &lt;span style="color: blue"&gt;foreach&lt;/span&gt; (&lt;span style="color: blue"&gt;var&lt;/span&gt; item &lt;span style="color: blue"&gt;in&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.Model.Items)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp; { &lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;div&lt;/span&gt; &lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;="ploeh"&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp; &lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt; &lt;span style="color: green"&gt;//
Render each item &lt;/span&gt;&lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;div&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt; } &lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
The challenge is to figure out how to render each item according to its own template.
&lt;/p&gt;
&lt;p&gt;
To define the templates, I create a UserControl for each item. The NamedTextItemUserControl
derives from ViewUserControl&amp;lt;NamedTextItem&amp;gt;, which gives me a strongly typed
Model:
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red163\green21\blue21;\red0\green0\blue0;\red255\green238\blue98;}??\fs20 \cf1 &amp;lt;\cf3 div\cf1 &amp;gt;&amp;lt;\cf3 strong\cf1 &amp;gt;\cf0\cb5\highlight5 &amp;lt;%\cf1\cb0\highlight0 =\cf0  \cf1 this\cf0 .Model.Name \cb5\highlight5 %&amp;gt;\cf1\cb0\highlight0 &amp;lt;/\cf3 strong\cf1 &amp;gt;&amp;lt;/\cf3 div\cf1 &amp;gt;\par ??&amp;lt;\cf3 div\cf1 &amp;gt;\cf0\cb5\highlight5 &amp;lt;%\cf1\cb0\highlight0 =\cf0  \cf1 this\cf0 .Model.Text \cb5\highlight5 %&amp;gt;\cf1\cb0\highlight0 &amp;lt;/\cf3 div\cf1 &amp;gt;}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;div&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;strong&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.Model.Name &lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;strong&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;div&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;div&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.Model.Text &lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;div&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
The other two UserControls are implemented similarly.
&lt;/p&gt;
&lt;p&gt;
A UserControl can be rendered using the RenderPartial extension method, so the only
thing left is to select the correct UserControl name for each item. It would be nice
to be able to do this in markup, like WPF, but I'm not aware of any way that is possible.
&lt;/p&gt;
&lt;p&gt;
I will have to resort to code, but we can at least strive for code that is as declarative
in style as possible.
&lt;/p&gt;
&lt;p&gt;
First, I need to define the map from type to UserControl:
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green238\blue98;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;\red163\green21\blue21;}??\fs20 \cb2\highlight2 &amp;lt;%\par ??\cb0\highlight0     \cf4 var\cf0  dataTemplates = \cf4 new\cf0  \cf5 Dictionary\cf0 &amp;lt;\cf5 Type\cf0 , \cf4 string\cf0 &amp;gt;();\par ??    dataTemplates[\cf4 typeof\cf0 (\cf5 NamedTextItem\cf0 )] =\par ??        \cf6 "NamedTextItemUserControl"\cf0 ;\par ??    dataTemplates[\cf4 typeof\cf0 (\cf5 NamedColorItem\cf0 )] =\par ??        \cf6 "NamedColorItemUserControl"\cf0 ;\par ??    dataTemplates[\cf4 typeof\cf0 (\cf5 NamedComplexItem\cf0 )] =\par ??        \cf6 "NamedComplexItemUserControl"\cf0 ;\par ??\cb2\highlight2 %&amp;gt;}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; dataTemplates
= &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt;, &lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; dataTemplates[&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;NamedTextItem&lt;/span&gt;)]
=&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"NamedTextItemUserControl"&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; dataTemplates[&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;NamedColorItem&lt;/span&gt;)]
=&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"NamedColorItemUserControl"&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; dataTemplates[&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;NamedComplexItem&lt;/span&gt;)]
=&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"NamedComplexItemUserControl"&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
Next, I can use this map to render each item correctly:
&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green238\blue98;\red255\green255\blue255;\red0\green0\blue255;\red163\green21\blue21;\red255\green0\blue0;\red0\green128\blue0;}??\fs20 \cb2\highlight2 &amp;lt;%\cb0\highlight0  \cf4 foreach\cf0  (\cf4 var\cf0  item \cf4 in\cf0  \cf4 this\cf0 .Model.Items)\par ??   \{ \cb2\highlight2 %&amp;gt;\par ??\cb0\highlight0    \cf4 &amp;lt;\cf5 div\cf0  \cf6 class\cf4 ="ploeh"&amp;gt;\par ??\cf0    \cb2\highlight2 &amp;lt;%\cb0\highlight0  \cf7 // Render each item \cf0\cb2\highlight2 %&amp;gt;\par ??\cb0\highlight0    \cb2\highlight2 &amp;lt;%\cb0\highlight0  \cf4 this\cf0 .Html.RenderPartial(dataTemplates[item.GetType()],\par ??                             item); \cb2\highlight2 %&amp;gt;\par ??\cb0\highlight0    \cf4 &amp;lt;/\cf5 div\cf4 &amp;gt;\par ??\cf0\cb2\highlight2 &amp;lt;%\cb0\highlight0  \} \cb2\highlight2 %&amp;gt;}
--&gt;
&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt; &lt;span style="color: blue"&gt;foreach&lt;/span&gt; (&lt;span style="color: blue"&gt;var&lt;/span&gt; item &lt;span style="color: blue"&gt;in&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.Model.Items)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp; { &lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;div&lt;/span&gt; &lt;span style="color: red"&gt;class&lt;/span&gt;&lt;span style="color: blue"&gt;="ploeh"&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp; &lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt; &lt;span style="color: green"&gt;//
Render each item &lt;/span&gt;&lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp; &lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.Html.RenderPartial(dataTemplates[item.GetType()],&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; item); &lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;div&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt; } &lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
This is definitely less pretty than with WPF, but if you overlook the aesthetics and
focus on the structure of the code, it's darn close to markup. The Cyclomatic Complexity
of the page is only &lt;em&gt;2&lt;/em&gt;, and that's even because of the &lt;em&gt;foreach&lt;/em&gt; statement
that we need in any case.
&lt;/p&gt;
&lt;p&gt;
The resulting page looks like this:
&lt;/p&gt;
&lt;p&gt;
&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="AspNetMvcApp" border="0" alt="AspNetMvcApp" src="http://blog.ploeh.dk/content/binary/WindowsLiveWriter/DataTemplatingInASP.NETMVC_13643/AspNetMvcApp_1.png" width="156" height="246"&gt; 
&lt;/p&gt;
&lt;p&gt;
My HTML skills aren't good enough to draw circles with markup, so I had to replace
them with blocks, but apart from that, the result is pretty much the same.
&lt;/p&gt;
&lt;p&gt;
A potential improvement on this technique could be to embed the knowledge of the UserControl
into each item. ASP.NET MVC Controllers already know of Views in an abstract sense,
so letting the View Model know about a UserControl (identified as a string) may be
conceptually sound.
&lt;/p&gt;
&lt;p&gt;
The advantage would be that we could get rid of the Dictionary in the ViewPage and
instead let the item itself tell us the name of the UserControl that should be used
to render it.
&lt;/p&gt;
&lt;p&gt;
The disadvantage would be that we lose some flexibility. It would then require a recompilation
of the application if we wanted to render an item using a different UserControl.
&lt;/p&gt;
&lt;p&gt;
The technique outlined here represents an explorative work in progress, so comments
are welcome.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.ploeh.dk/aggbug.ashx?id=2691f07e-2813-4c4e-a861-43d7d04be692" /&gt;</description>
      <comments>http://blog.ploeh.dk/CommentView,guid,2691f07e-2813-4c4e-a861-43d7d04be692.aspx</comments>
      <category>ASP.NET MVC</category>
      <category>WPF</category>
    </item>
  </channel>
</rss>