﻿<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Ask Dr. WPF</title>
    <description>&lt;span class="jbr"&gt;Do you have questions about Windows Presentation Foundation that might have broad appeal? Ask &lt;a href="mailto:ask@drwpf.com"&gt;Dr. WPF&lt;/a&gt;!&lt;/span&gt;</description>
    <link>http://www.drwpf.com/blog/Home/tabid/36/BlogId/1/Default.aspx</link>
    <language>en-US</language>
    <managingEditor>ask@drwpf.com</managingEditor>
    <webMaster>admin@drwpf.com</webMaster>
    <pubDate>Fri, 25 Jul 2008 06:58:05 GMT</pubDate>
    <lastBuildDate>Fri, 25 Jul 2008 06:58:05 GMT</lastBuildDate>
    <docs>http://backend.userland.com/rss</docs>
    <generator>Blog RSS Generator Version 3.3.0.16726</generator>
    <item>
      <title>ItemsControl:  'G' is for Generator</title>
      <description>&lt;div class="jbr"&gt;
&lt;p&gt;In &lt;a target="_blank" href="http://drwpf.com/blog/Home/tabid/36/EntryID/32/Default.aspx"&gt;&lt;em&gt;'I' is for Item Container&lt;/em&gt;&lt;/a&gt;, we learned that each item within an ItemsControl is represented by visuals hosted within a container element.  The type of this &lt;em&gt;"item container"&lt;/em&gt; is specific to the type of the ItemsControl.  For example, the container for an item in a ListBox is the ListBoxItem element.  Similarly, the container for an item in a ComboBox is the ComboBoxItem element.  (A complete list of the native ItemsControl classes and their respective item containers can be found at the end of &lt;a href="http://drwpf.com/blog/Home/tabid/36/EntryID/32/Default.aspx" target="_blank"&gt;&lt;em&gt;'I' is for Item Container&lt;/em&gt;&lt;/a&gt;.)&lt;/p&gt;
&lt;p&gt;In this episode, we examine the mechanism by which item containers come into existence.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Where did these containers come from?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In the previous examples in this series, the item containers have mysteriously (or perhaps magically) been created without our knowledge.  All we typically do is set a binding on the ItemsSource property for the control.  Consider the following ListBox example (again, from &lt;a target="_blank" href="http://drwpf.com/blog/Home/tabid/36/EntryID/32/Default.aspx"&gt;&lt;em&gt;'I' is for Item Container&lt;/em&gt;&lt;/a&gt;):&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;&lt;&lt;font color="#993300"&gt;ListBox &lt;/font&gt;&lt;font color="#ff0000"&gt;ItemsSource&lt;/font&gt;="{&lt;font color="#993300"&gt;Binding&lt;/font&gt; &lt;font color="#ff0000"&gt;Source&lt;/font&gt;={&lt;font color="#993300"&gt;StaticResource&lt;/font&gt; &lt;font color="#ff0000"&gt;Characters&lt;/font&gt;}}" &lt;br /&gt;
    &lt;font color="#ff0000"&gt;ItemContainerStyle&lt;/font&gt;="{&lt;font color="#993300"&gt;StaticResource&lt;/font&gt; &lt;font color="#ff0000"&gt;CharacterContainerStyle&lt;/font&gt;}"&gt;&lt;br /&gt;
  &lt;&lt;font color="#993300"&gt;ListBox.ItemsPanel&lt;/font&gt;&gt;&lt;br /&gt;
    &lt;&lt;font color="#993300"&gt;ItemsPanelTemplate&lt;/font&gt;&gt;&lt;br /&gt;
      &lt;&lt;font color="#993300"&gt;Canvas&lt;/font&gt; /&gt;&lt;br /&gt;
    &lt;/&lt;font color="#993300"&gt;ItemsPanelTemplate&lt;/font&gt;&gt;&lt;br /&gt;
  &lt;/&lt;font color="#993300"&gt;ListBox.ItemsPanel&lt;/font&gt;&gt;&lt;br /&gt;
&lt;/&lt;font color="#993300"&gt;ListBox&lt;/font&gt;&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;img height="300" alt="" width="340" src="http://www.drwpf.com/Blog/Portals/0/Images/ItemsControl/I/CharacterListBoxWithCanvasAndLocationBound.jpg" /&gt;&lt;/p&gt;
&lt;p&gt;We know that each item in the ListBox is contained within a ListBoxItem.  What we don't know is who created that ListBoxItem.  It is logical to assume that the ItemsControl, itself, created the ListBoxItems, but as we'll see moving forward, this is not the case.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Introduction to the ItemContainerGenerator class&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;It turns out that every ItemsControl has its own instance of an &lt;em&gt;ItemContainerGenerator&lt;/em&gt; object.  This generator is accessible via an appropriately named ItemContainerGenerator property.&lt;/p&gt;
&lt;p&gt;As the class name implies, an ItemContainerGenerator provides methods by which item containers can be generated for items within an ItemsControl.  Specifically, an ItemContainerGenerator knows how to create a container and link it to the item it will contain.  It also knows how to remove a container that is no longer needed.  Finally, the ItemContainerGenerator class contains several invaluable methods for mapping existing items within an ItemsControl to their containers and vice versa.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Who really holds the reigns of the generator?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;An ItemContainerGenerator knows how to create a container and link it to an item, but who actually gets to decide &lt;em&gt;when&lt;/em&gt; this container generation occurs?  You might think that the generator, itself, should get to decide when to do its job, but actually, it intentionally remains "hands off" in these matters.&lt;/p&gt;
&lt;p&gt;The ItemContainerGenerator does, however, monitor the collection view of its associated items collection (via a weak event listener for the INotifyCollectionChanged events).  It uses the change notifications to know when to unlink an existing container from an item that is removed, but it never makes a decision on its own to generate a container for an added item.  It just quietly maintains a record of all the items within the collection so that it is poised to create a container at a moment's notice.&lt;/p&gt;
&lt;p&gt;If it's not the ItemContainerGenerator, then it must be the ItemsControl, right?  Well, no, the ItemsControl does not directly generate the containers either, but it definitely plays some key roles in the process.  Namely, it creates and owns the "generator" of containers (the ItemContainerGenerator instance) and it gets to decide the &lt;em&gt;type &lt;/em&gt;of containers that will be created.&lt;/p&gt;
&lt;p&gt;Okay, then who else could possibly be responsible for deciding when containers will be generated?&lt;/p&gt;
&lt;p&gt;If you've been following this series for a while, it should be pretty obvious that neither the ItemsControl nor the ItemContainerGenerator can be the sole decision makers in the process because the ItemsControl class supports something called UI virtualization (wherein only visible UI elements are instantiated and added to the element tree).&lt;/p&gt;
&lt;p&gt;In &lt;a target="_blank" href="http://drwpf.com/blog/Home/tabid/36/EntryID/28/Default.aspx"&gt;&lt;em&gt;'P' is for Panel&lt;/em&gt;&lt;/a&gt;, we learned that the items host (a.k.a., the items panel) of an ItemsControl is dynamic.  As such, there is no way for the ItemsControl or its ItemContainerGenerator to generically know when containers will be visible.  Clearly, the items panel must be directly involved in container generation.  After all, the item containers are direct visual children of the items panel.  Since the panel knows precisely where to place these children, it is very logical that it would be the one holding the proverbial reigns of the ItemContainerGenerator and telling it when to generate the containers.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How does the generator know what type of container to generate?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When it is time to generate a container, you might be wondering how the ItemContainerGenerator knows what type of object to instantiate.  Well, really, it doesn't know at all.  Each ItemsControl gets to specify its own type of item container.  The generator simply defers this container creation to its associated ItemsControl by calling its GetContainerForItem() method.  This method is responsible for creating and returning a new container for an item.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What happens if the item is already the same type as the container?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;We've previously looked at examples like the following in which a ListBoxItem is added directly to a ListBox:&lt;/p&gt;
&lt;p&gt;
&lt;table cellspacing="1" cellpadding="1" align="left" border="0"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;&lt;font face="Courier New" size="2"&gt;&lt;&lt;font color="#993300"&gt;ListBox&lt;/font&gt; &lt;font color="#ff0000"&gt;SelectedIndex&lt;/font&gt;="0" &lt;font color="#ff0000"&gt;Width&lt;/font&gt;="100"&gt;      &lt;br /&gt;
              &lt;&lt;font color="#993300"&gt;ListBoxItem&lt;/font&gt;&gt;Item 1&lt;/&lt;font color="#993300"&gt;ListBoxItem&lt;/font&gt;&gt;&lt;br /&gt;
              &lt;&lt;font color="#993300"&gt;ListBoxItem&lt;/font&gt;&gt;Item 2&lt;/&lt;font color="#993300"&gt;ListBoxItem&lt;/font&gt;&gt;&lt;br /&gt;
              &lt;&lt;font color="#993300"&gt;ListBoxItem&lt;/font&gt;&gt;Item 3&lt;/&lt;font color="#993300"&gt;ListBoxItem&lt;/font&gt;&gt;&lt;br /&gt;
              &lt;&lt;font color="#993300"&gt;ListBoxItem&lt;/font&gt;&gt;Item 4&lt;/&lt;font color="#993300"&gt;ListBoxItem&lt;/font&gt;&gt;&lt;br /&gt;
              &lt;&lt;font color="#993300"&gt;ListBoxItem&lt;/font&gt;&gt;Item 5&lt;/&lt;font color="#993300"&gt;ListBoxItem&lt;/font&gt;&gt;&lt;br /&gt;
            &lt;/&lt;font color="#993300"&gt;ListBox&lt;/font&gt;&gt;&lt;/font&gt;&lt;/td&gt;
            &lt;td valign="top"&gt;
            &lt;p align="left"&gt;&lt;img height="87" alt="" width="104" src="http://www.drwpf.com/Blog/Portals/0/Images/ListBox.png" /&gt;&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;You may be wondering what the ItemContainerGenerator does in this case, since the item, itself, is already a container.  Will it create a new ListBoxItem as the container for the specified ListBoxItem?  The answer is no, it will not.  Before the ItemContainerGenerator creates a new container, it first checks to see whether the item, itself, is already a container.  It does this by calling the &lt;font face="Courier New" color="#993300"&gt;IsItemItsOwnContainer()&lt;/font&gt; method on the associated ItemsControl.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How can we specify our own container type for a custom ItemsControl?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Suppose we wish to create a custom ForceDirectedItemsControl class that will use a physics-aware items panel to host its children.  To enable this scenario, we decide that each container will need to be an instance of a custom ForceDirectedItem class that will support the extra physics-based properties that control item layout in our custom panel (like friction, spring, repulsion, etc).  How can we ensure that our custom ForceDirectedItemsControl class creates the ForceDirectedItem containers?&lt;/p&gt;
&lt;p&gt;Well, it turns out that this is pretty easy...  we just need to override two virtual methods in our ItemsControl class:  &lt;font face="Courier New" color="#993300"&gt;IsItemItsOwnContainerOverride()&lt;/font&gt; and &lt;font face="Courier New" color="#993300"&gt;GetContainerForItemOverride()&lt;/font&gt;. &lt;/p&gt;
&lt;p&gt;The following is a typical implementation of a custom ItemsControl class with a custom item container:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;  public class &lt;/font&gt;&lt;font color="#008080"&gt;ForceDirectedItemsControl&lt;/font&gt; : &lt;font color="#008080"&gt;ItemsControl&lt;/font&gt;&lt;br /&gt;
  {&lt;br /&gt;
      &lt;font color="#0000ff"&gt;protected override bool&lt;/font&gt; IsItemItsOwnContainerOverride(&lt;font color="#0000ff"&gt;object&lt;/font&gt; item)&lt;br /&gt;
      {&lt;br /&gt;
          &lt;font color="#0000ff"&gt;return&lt;/font&gt; (item &lt;font color="#0000ff"&gt;is&lt;/font&gt; &lt;font color="#008080"&gt;ForceDirectedItem&lt;/font&gt;);&lt;br /&gt;
      }&lt;br /&gt;
&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;br /&gt;
      &lt;font color="#0000ff"&gt;protected override&lt;/font&gt; &lt;font color="#008080"&gt;DependencyObject&lt;/font&gt; GetContainerForItemOverride()&lt;br /&gt;
      {&lt;br /&gt;
          &lt;font color="#0000ff"&gt;return new&lt;/font&gt; &lt;font color="#008080"&gt;ForceDirectedItem&lt;/font&gt;();&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Using the above class, when the ItemContainerGenerator is asked to generate a container for an item, it will first check to see if the item is already a ForceDirectedItem.  If not, it will ask the ItemsControl to create a container by deferring to its &lt;font face="Courier New" color="#993300"&gt;GetContainerForItemOverride()&lt;/font&gt;.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Caution:&lt;/em&gt;  You should never assume there to be a constant tie between an item and its container.  The &lt;font face="Courier New" color="#993300"&gt;GetContainerForItemOverride()&lt;/font&gt; method intentionally does not supply the item that will be hosted within the container.  It is the responsibility of the ItemContainerGenerator to link an item to its container.  When a virtualizing panel is hosting containers, it is common to link one item to a container and later link a completely different item to that same container.  This provides a nice perf optimization.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How does UI virtualization work in an ItemsControl?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I previously promised that this episode would include a high level look at exactly how an ItemsControl supports this concept of UI virtualization.  Feel free to skip ahead if you are not curious about the finer details of virtualization.&lt;/p&gt;
&lt;p&gt;There are really only two noteworthy panel classes in the framework that take control of the ItemContainerGenerator and instruct it to generate containers:  Panel and VirtualizingStackPanel.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Sidenote:&lt;/em&gt;  There is actually a third native class, called ToolBarPanel, that uses the ItemContainerGenerator to generate items for a ToolBar.  We won't really spend any time looking at the ToolBar class in this series, since in many ways it breaks the traditional ItemsControl conventions.  For more, see the note marked &lt;em&gt;"**"&lt;/em&gt; at the end of &lt;a href="http://drwpf.com/blog/Home/tabid/36/EntryID/32/Default.aspx" target="_blank"&gt;&lt;em&gt;'I' is for Item Container&lt;/em&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The abstract &lt;em&gt;Panel&lt;/em&gt; base class contains what we can consider the &lt;em&gt;default&lt;/em&gt; container generation code for panels.  This default logic is simply to generate containers for every item in the collection.  So by default, there is no UI virtualization whatsoever.&lt;/p&gt;
&lt;p&gt;The framework contains an abstract class called &lt;em&gt;VirtualizingPanel&lt;/em&gt; which derives from Panel and then overrides the default container generation code to replace it with...  well...  nothing.  So if you derive a panel from VirtualizingPanel, it will not generate any containers on its own.  Instead, you will be responsible for implementing the code that leverages the ItemContainerGenerator to generate the containers.&lt;/p&gt;
&lt;p&gt;There is only one virtualizing panel included in the early releases of the framework (.NET 3.0 and 3.5).  It is called &lt;em&gt;VirtualizingStackPanel&lt;/em&gt;.  And if you paid attention to the chart at the end of &lt;a href="http://drwpf.com/blog/Home/tabid/36/EntryID/32/Default.aspx" target="_blank"&gt;&lt;em&gt;'I' is for Item Container&lt;/em&gt;&lt;/a&gt;, you know that VirtualizingStackPanel is the default items host for ListBox and ListView.  So we get UI virtualization thrown in for free when we use these controls with their default items panel.&lt;/p&gt;
&lt;p&gt;A VirtualizingStackPanel, like any other panel, is responsible for sizing and positioning its children (the item containers).  As such, it knows exactly which children are visible within the viewport of the ItemsControl at any given time.  It uses this knowledge to create, or &lt;em&gt;"realize"&lt;/em&gt;, only the visible children (plus a few extra on either side of the viewport to enable keyboard navigation to work as expected).  When a realized child is scrolled out of the viewport, the VirtualizingStackPanel queues that container to be removed, or &lt;em&gt;"virtualized"&lt;/em&gt;, so that its resources can be reclaimed.&lt;/p&gt;
&lt;p&gt;If you are writing a custom virtualizing panel, you will also need to implement the logic for realizing visible containers and virtualizing non-visible containers.  Realization consists of generating the container (&lt;font face="Courier New" color="#993300"&gt;generator.GenerateNext()&lt;/font&gt;) and preparing it to host its item (&lt;font face="Courier New" color="#993300"&gt;generator.PrepareItemContainer()&lt;/font&gt;).  Virtualization consists of removing the container (&lt;font face="Courier New" color="#993300"&gt;generator.Remove()&lt;/font&gt;).&lt;/p&gt;
&lt;p&gt;We will go deeper into UI virtualization in a future article entitled &lt;em&gt;'V' is for Virtualization&lt;/em&gt;.  In the meantime, if you are anxious to get started writing a virtualizing panel, you should &lt;a href="http://blogs.msdn.com/dancre/archive/tags/VirtualizingTilePanel/default.aspx" target="_blank"&gt;check out Dan Crevier's series on creating a virtualizing panel&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Tying It All Together&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;We've already covered many different aspects of an ItemsControl in this series and they all come into play in this process of item container generation.  There is a very intricate dance involving an ItemsControl, its items host (&lt;a href="http://drwpf.com/blog/Home/tabid/36/EntryID/28/Default.aspx" target="_blank"&gt;&lt;em&gt;'P' is for Panel&lt;/em&gt;&lt;/a&gt;), its Items collection (&lt;a href="http://drwpf.com/blog/Home/tabid/36/EntryID/18/Default.aspx" target="_blank"&gt;&lt;em&gt;'C' is for Collection&lt;/em&gt;&lt;/a&gt;), and its ItemContainerGenerator (&lt;em&gt;'G' is for Generator&lt;/em&gt;... this article).  This dance results in the creation of item containers (&lt;a href="http://drwpf.com/blog/Home/tabid/36/EntryID/32/Default.aspx" target="_blank"&gt;&lt;em&gt;'I' is for Item Container&lt;/em&gt;&lt;/a&gt;) that will host the items directly or host a visual representation of the items, as specified via an item template (&lt;a href="http://drwpf.com/blog/Home/tabid/36/EntryID/24/Default.aspx" target="_blank"&gt;&lt;em&gt;'D' is for DataTemplate&lt;/em&gt;&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Finding Template Elements by Mapping Items to Containers&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A very common question in the &lt;a target="_blank" href="http://forums.msdn.microsoft.com/en-US/wpf/threads/"&gt;WPF Forum&lt;/a&gt; is, &lt;em&gt;"How can I get a reference to a specific element in my item template at runtime?"&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Sidenote:  &lt;/em&gt;In most scenarios, you shouldn't need to do this.  Typically, the reason a person wants to do this is so they can programmatically change a property on some element in the template.  If one carefully develops their view model (which will contain the data items represented within the ItemsControl), this type of property update can be handled via a binding to a property on a data item.&lt;/p&gt;
&lt;p&gt;There are certainly a few scenarios where it is necessary to drill into an item's visual subtree to get a specific element.  For these scenarios, we can leverage a couple of nifty methods of the ItemContainerGenerator:  &lt;font face="Courier New" color="#993300"&gt;ContainerFromIndex()&lt;/font&gt; and &lt;font face="Courier New" color="#993300"&gt;ContainerFromItem()&lt;/font&gt;.  Once you have the container for the desired item, you can locate the element within its visual tree using a recursive routine like the &lt;font face="Courier New" color="#993300"&gt;GetDescendantByName()&lt;/font&gt; or &lt;font face="Courier New" color="#993300"&gt;GetDescendantByType()&lt;/font&gt; routines shown here:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;  public static&lt;/font&gt; &lt;font color="#008080"&gt;Visual&lt;/font&gt; GetDescendantByName(&lt;font color="#008080"&gt;Visual&lt;/font&gt; element, &lt;font color="#0000ff"&gt;string&lt;/font&gt; name)&lt;br /&gt;
  {&lt;br /&gt;
      &lt;font color="#0000ff"&gt;if&lt;/font&gt; (element == &lt;font color="#0000ff"&gt;null&lt;/font&gt;) &lt;font color="#0000ff"&gt;return null&lt;/font&gt;;&lt;br /&gt;
&lt;br /&gt;
&lt;/font&gt;&lt;font face="Courier New"&gt;      &lt;font color="#0000ff"&gt;if&lt;/font&gt; (element &lt;font color="#0000ff"&gt;is&lt;/font&gt; &lt;font color="#008080"&gt;FrameworkElement&lt;/font&gt;&lt;br /&gt;
          &amp;&amp; (element &lt;font color="#0000ff"&gt;as&lt;/font&gt; &lt;font color="#008080"&gt;FrameworkElement&lt;/font&gt;).Name == name) &lt;font color="#0000ff"&gt;return&lt;/font&gt; element;&lt;br /&gt;
&lt;br /&gt;
&lt;/font&gt;&lt;font face="Courier New"&gt;      &lt;font color="#008080"&gt;Visual&lt;/font&gt; result = &lt;font color="#0000ff"&gt;null&lt;/font&gt;;&lt;br /&gt;
&lt;br /&gt;
&lt;/font&gt;&lt;font face="Courier New"&gt;      if (element &lt;font color="#0000ff"&gt;is&lt;/font&gt; &lt;font color="#008080"&gt;FrameworkElement&lt;/font&gt;)&lt;br /&gt;
          (element &lt;font color="#0000ff"&gt;as&lt;/font&gt; &lt;font color="#008080"&gt;FrameworkElement&lt;/font&gt;).ApplyTemplate();&lt;br /&gt;
&lt;br /&gt;
&lt;/font&gt;&lt;font face="Courier New"&gt;      &lt;font color="#0000ff"&gt;for&lt;/font&gt; (&lt;font color="#0000ff"&gt;int&lt;/font&gt; i = 0; i &lt; &lt;font color="#008080"&gt;VisualTreeHelper&lt;/font&gt;.GetChildrenCount(element); i++)&lt;br /&gt;
      {&lt;br /&gt;
          &lt;font color="#008080"&gt;Visual&lt;/font&gt; visual = &lt;font color="#008080"&gt;VisualTreeHelper&lt;/font&gt;.GetChild(element, i) &lt;font color="#0000ff"&gt;as&lt;/font&gt; &lt;font color="#008080"&gt;Visual&lt;/font&gt;;&lt;br /&gt;
          result = GetDescendantByName(visual, name);&lt;br /&gt;
          &lt;font color="#0000ff"&gt;if&lt;/font&gt; (result != &lt;font color="#0000ff"&gt;null&lt;/font&gt;)&lt;br /&gt;
              &lt;font color="#0000ff"&gt;break&lt;/font&gt;;&lt;br /&gt;
      }&lt;br /&gt;
      &lt;font color="#0000ff"&gt;return&lt;/font&gt; result;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  &lt;font color="#0000ff"&gt;public static&lt;/font&gt; &lt;font color="#008080"&gt;Visual&lt;/font&gt; GetDescendantByType(&lt;font color="#008080"&gt;Visual&lt;/font&gt; element, &lt;font color="#008080"&gt;Type&lt;/font&gt; type)&lt;br /&gt;
  {&lt;br /&gt;
      &lt;font color="#0000ff"&gt;if&lt;/font&gt; (element == &lt;font color="#0000ff"&gt;null&lt;/font&gt;) &lt;font color="#0000ff"&gt;return null&lt;/font&gt;;&lt;br /&gt;
&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;br /&gt;
      &lt;font color="#0000ff"&gt;if&lt;/font&gt; (element.GetType() == type) &lt;font color="#0000ff"&gt;return&lt;/font&gt; element;&lt;br /&gt;
&lt;br /&gt;
&lt;/font&gt;&lt;font face="Courier New"&gt;      &lt;font color="#008080"&gt;Visual&lt;/font&gt; foundElement = &lt;font color="#0000ff"&gt;null&lt;/font&gt;;&lt;br /&gt;
      &lt;font color="#0000ff"&gt;if&lt;/font&gt; (element &lt;font color="#0000ff"&gt;is&lt;/font&gt; &lt;font color="#008080"&gt;FrameworkElement&lt;/font&gt;)&lt;br /&gt;
          (element &lt;font color="#0000ff"&gt;as&lt;/font&gt; &lt;font color="#008080"&gt;FrameworkElement&lt;/font&gt;).ApplyTemplate();&lt;br /&gt;
&lt;br /&gt;
&lt;/font&gt;&lt;font face="Courier New"&gt;      &lt;font color="#0000ff"&gt;for&lt;/font&gt; (&lt;font color="#0000ff"&gt;int&lt;/font&gt; i = 0;&lt;br /&gt;
          i &lt; &lt;font color="#008080"&gt;VisualTreeHelper&lt;/font&gt;.GetChildrenCount(element); i++)&lt;br /&gt;
      {&lt;br /&gt;
          &lt;font color="#008080"&gt;Visual&lt;/font&gt; visual = &lt;font color="#008080"&gt;VisualTreeHelper&lt;/font&gt;.GetChild(element, i) &lt;font color="#0000ff"&gt;as&lt;/font&gt; &lt;font color="#008080"&gt;Visual&lt;/font&gt;;&lt;br /&gt;
          foundElement = GetDescendantByType(visual, type);&lt;br /&gt;
          &lt;font color="#0000ff"&gt;if&lt;/font&gt; (foundElement != &lt;font color="#0000ff"&gt;null&lt;/font&gt;)&lt;br /&gt;
              &lt;font color="#0000ff"&gt;break&lt;/font&gt;;&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
&lt;/font&gt;&lt;font face="Courier New"&gt;      &lt;font color="#0000ff"&gt;return&lt;/font&gt; foundElement;&lt;br /&gt;
  }&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Finding the Container Associated with a Template Element&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Similarly, there are times when you will want to handle an event on a specific template element and then locate the ancestor item container for the element.  One option is to walk the ancestors looking for the element by type with the following &lt;font face="Courier
New" color="#993300"&gt;GetAncestorByType()&lt;/font&gt; routine:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;  &lt;font color="#0000ff"&gt;public static&lt;/font&gt; &lt;font color="#008080"&gt;DependencyObject&lt;/font&gt; GetAncestorByType(&lt;br /&gt;
      &lt;font color="#008080"&gt;DependencyObject&lt;/font&gt; element, &lt;font color="#008080"&gt;Type&lt;/font&gt; type)&lt;br /&gt;
  {&lt;br /&gt;
      &lt;font color="#0000ff"&gt;if&lt;/font&gt; (element == &lt;font color="#0000ff"&gt;null&lt;/font&gt;) &lt;font color="#0000ff"&gt;return null&lt;/font&gt;;&lt;br /&gt;
&lt;br /&gt;
&lt;/font&gt;&lt;font face="Courier New"&gt;      &lt;font color="#0000ff"&gt;if&lt;/font&gt; (element.GetType() == type) &lt;font color="#0000ff"&gt;return&lt;/font&gt; element;&lt;br /&gt;
&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;br /&gt;
      &lt;font color="#0000ff"&gt;return&lt;/font&gt; GetAncestorByType(&lt;font color="#008080"&gt;VisualTreeHelper&lt;/font&gt;.GetParent(element), type);&lt;br /&gt;
  }&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Another perfectly viable approach is to leverage a little fact that we learned in &lt;a href="http://drwpf.com/blog/Home/tabid/36/EntryID/32/Default.aspx" target="_blank"&gt;&lt;em&gt;'I' is for Item Container&lt;/em&gt;&lt;/a&gt;...  namely, that the DataContext of the container is the very item that it contains.  In most scenarios, this same DataContext will be inherited by all framework elements within the item template.  So you can typically cast the original source of the event to a FrameworkElement and use the DataContext property to get the item that is represented by the template.  You can then use the &lt;font face="Courier New" color="#993300"&gt;ContainerFromItem()&lt;/font&gt; method of the ItemContainerGenerator to get the container.&lt;/p&gt;
&lt;p&gt;Most of these tricks work great for a simple ItemsControl, but then get a little tricky with a HeaderedItemsControl like a TreeView or MenuItem.  For these cases, I strongly recommend leveraging the view model, commanding, and bindings to handle property updates within the view and to respond to user actions within the view model.  We'll explore these things further in future episodes.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Dealing with Asynchronous Container Generation&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Alright, clearly the ItemContainerGenerator class provides some very handy, and even essential, methods for mapping items to containers and vice versa.  There is still one very important thing to add to the whole equation... that is the &lt;em&gt;timing&lt;/em&gt; of container generation.&lt;/p&gt;
&lt;p&gt;Suppose you have a ListBox named &lt;em&gt;CharacterListBox&lt;/em&gt; bound to an observable collection named &lt;em&gt;Characters&lt;/em&gt;.  You might be tempted to write code like the following:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;  &lt;font color="#0000ff"&gt;private void&lt;/font&gt; AddScooby()&lt;br /&gt;
  {&lt;br /&gt;
      &lt;font color="#008080"&gt;Character&lt;/font&gt; scooby = &lt;font color="#0000ff"&gt;new&lt;/font&gt; &lt;font color="#008080"&gt;Character&lt;/font&gt;(&lt;font color="#993300"&gt;"Scooby Doo"&lt;/font&gt;);&lt;br /&gt;
      Characters.Add(scooby);&lt;br /&gt;
      &lt;font color="#008080"&gt;ListBoxItem&lt;/font&gt; lbi = CharacterListBox.ItemContainerGenerator&lt;br /&gt;
          .ContainerFromItem(scooby) &lt;font color="#0000ff"&gt;as&lt;/font&gt; &lt;font color="#008080"&gt;ListBoxItem&lt;/font&gt;;&lt;br /&gt;
      lbi.IsSelected = &lt;font color="#0000ff"&gt;true&lt;/font&gt;;&lt;br /&gt;
  }&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;This code will actually result in an exception because the &lt;font face="Courier New" color="#993300"&gt;lbi&lt;/font&gt; member will be null.  The reason is that containers are generated in a separate dispatcher operation.  As a result, simply setting the ItemsSource property or modifying the bound collection does not cause containers to be created immediately.&lt;/p&gt;
&lt;p&gt;The key point here is that we must always think of container generation as an asynchronous operation.  So how can we add an item and then safely locate its container &lt;em&gt;after&lt;/em&gt; it has been generated?  For this very purpose, the ItemContainerGenerator class provides a &lt;font face="Courier New" color="#993300"&gt;Status&lt;/font&gt; property along with change notifications for the status.  If we need to programmatically access containers after they are generated, we can subscribe to the &lt;font face="Courier New" color="#993300"&gt;StatusChanged&lt;/font&gt; event, as shown in the following code:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;  &lt;font color="#0000ff"&gt;private void&lt;/font&gt; AddScooby()&lt;br /&gt;
  {&lt;br /&gt;
      &lt;font color="#008080"&gt;_&lt;/font&gt;scooby = new &lt;font color="#008080"&gt;Character&lt;/font&gt;(&lt;font color="#993300"&gt;"Scooby Doo"&lt;/font&gt;);&lt;br /&gt;
      Characters.Add(_scooby);&lt;br /&gt;
      CharacterListBox.ItemContainerGenerator.StatusChanged &lt;br /&gt;
          += OnStatusChanged;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&lt;/font&gt;&lt;font face="Courier New"&gt;  &lt;font color="#0000ff"&gt;private void&lt;/font&gt; OnStatusChanged(&lt;font color="#0000ff"&gt;object&lt;/font&gt; sender, &lt;font color="#008080"&gt;EventArgs&lt;/font&gt; e)&lt;br /&gt;
  {&lt;br /&gt;
      &lt;font color="#0000ff"&gt;if&lt;/font&gt; (CharacterListBox.ItemContainerGenerator.Status &lt;br /&gt;
          == &lt;font color="#008080"&gt;GeneratorStatus&lt;/font&gt;.ContainersGenerated)&lt;br /&gt;
      {&lt;br /&gt;
          CharacterListBox.ItemContainerGenerator.StatusChanged &lt;br /&gt;
              -= OnStatusChanged;&lt;br /&gt;
          &lt;font color="#008080"&gt;ListBoxItem&lt;/font&gt; lbi = CharacterListBox.ItemContainerGenerator&lt;br /&gt;
              .ContainerFromItem(_scooby) &lt;font color="#0000ff"&gt;as&lt;/font&gt; &lt;font color="#008080"&gt;ListBoxItem&lt;/font&gt;;&lt;br /&gt;
          &lt;font color="#0000ff"&gt;if&lt;/font&gt; (lbi != &lt;font color="#0000ff"&gt;null&lt;/font&gt;)&lt;br /&gt;
          {&lt;br /&gt;
              lbi.IsSelected = &lt;font color="#0000ff"&gt;true&lt;/font&gt;;&lt;br /&gt;
          }&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;There are a couple of important things to notice about this code.  First, the &lt;font face="Courier New" color="#993300"&gt;OnStatusChanged()&lt;/font&gt; method immediately checks the current status of the generator.  This is important because the status could have changed to one of four different possible values:  &lt;font face="Courier New" color="#993300"&gt;NotStarted&lt;/font&gt;, &lt;font face="Courier New" color="#993300"&gt;GeneratingContainers&lt;/font&gt;, &lt;font face="Courier New" color="#993300"&gt;ContainersGenerated&lt;/font&gt;, or &lt;font face="Courier New" color="#993300"&gt;Error&lt;/font&gt;.  We should always specifically check for the status that we care about.&lt;/p&gt;
&lt;p&gt;Second, as soon as the containers have been generated, the handler removes itself so that it will not be called when subsequent changes to the Items collection cause the generator's status to change.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Bonus Tidbit:  &lt;/em&gt;FindAncestor Bindings are Very Handy in Item Templates&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This is really just an extra tidbit that I'm tagging onto this topic because of its usefulness.  It is not really specific to container generation. &lt;/p&gt;
&lt;p&gt;Recall that in &lt;a target="_blank" href="http://drwpf.com/blog/Home/tabid/36/EntryID/24/Default.aspx"&gt;&lt;em&gt;'D' is for DataTemplate&lt;/em&gt;&lt;/a&gt;, we learned how to provide a template of visuals to represent the items in an Items collection.  When dealing with a Selector like ListBox, ListView, TreeView, etc, it is quite common to want to trigger a change in the visuals based on whether an item is selected.  We now know that the concept of selection for these controls is based on the IsSelected property of their respective item containers.  Any element in the item template can be bound to a property of the item container using a FindAncestor binding.&lt;/p&gt;
&lt;p&gt;Below is a typical data trigger that might be found in a DataTemplate for an item within a ListBox:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;&lt;&lt;font color="#993300"&gt;DataTrigger&lt;/font&gt; &lt;font color="#ff0000"&gt;Binding&lt;/font&gt;="{&lt;font color="#993300"&gt;Binding&lt;/font&gt; &lt;font color="#ff0000"&gt;RelativeSource&lt;/font&gt;={&lt;font color="#993300"&gt;RelativeSource&lt;/font&gt; FindAncestor,&lt;br /&gt;
    &lt;font color="#ff0000"&gt;AncestorType&lt;/font&gt;={&lt;font color="#993300"&gt;x:Type&lt;/font&gt; ListBoxItem}}, &lt;font color="#ff0000"&gt;Path&lt;/font&gt;=IsSelected}" &lt;font color="#ff0000"&gt;Value&lt;/font&gt;="True"&gt;&lt;br /&gt;
  &lt;&lt;font color="#993300"&gt;Setter&lt;/font&gt; &lt;font color="#ff0000"&gt;Property&lt;/font&gt;="Foreground" &lt;font color="#ff0000"&gt;Value&lt;/font&gt;="#A1927E" &lt;font color="#ff0000"&gt;TargetName&lt;/font&gt;="tb" /&gt;&lt;br /&gt;
&lt;/&lt;font color="#993300"&gt;DataTrigger&lt;/font&gt;&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Up Next&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In the next episode of this series, I plan to examine the "lookless" nature of ItemsControls.  Please stay tuned!&lt;/p&gt;
&lt;/div&gt;</description>
      <link>http://www.drwpf.com/blog/Home/tabid/36/EntryID/37/Default.aspx</link>
      <author>ask@drwpf.com</author>
      <comments>http://www.drwpf.com/blog/Home/tabid/36/EntryID/37/Default.aspx#Comments</comments>
      <guid isPermaLink="true">http://www.drwpf.com/Blog/Default.aspx?tabid=36&amp;EntryID=37</guid>
      <pubDate>Sun, 20 Jul 2008 13:42:00 GMT</pubDate>
      <slash:comments>7</slash:comments>
      <trackback:ping>http://www.drwpf.com/Blog/DesktopModules/Blog/Trackback.aspx?id=37</trackback:ping>
    </item>
    <item>
      <title>Leveraging Freezables to Provide an Inheritance Context for Bindings</title>
      <description>&lt;div class="jbr"&gt;
&lt;p&gt;Mike Hillberg has some great observations about WPF application architecture as it pertains to model interaction in his &lt;a target="_blank" href="http://blogs.msdn.com/mikehillberg/archive/2008/05/21/Model-see_2C00_-model-do.aspx"&gt;&lt;em&gt;"Model See Model Do"&lt;/em&gt;&lt;/a&gt; post.  I am very much in agreement with Mike on this subject.&lt;/p&gt;
&lt;p&gt;And let me also say that I'm very happy to see Mike posting a bit more on WPF recently.  &lt;img alt="" src="http://www.drwpf.com/Blog/Providers/HtmlEditorProviders/Fck/FCKeditor/editor/images/smiley/msn/regular_smile.gif" /&gt;&lt;/p&gt;
&lt;p&gt;There is another thing definitely worth highlighting in Mike's latest post... In his code sample, he uses a little trick to ensure that bindings on his command and argument objects resolve correctly.  Namely, he derives them from Freezable:&lt;/p&gt;
&lt;div&gt;&lt;font face="Courier New"&gt;&lt;span&gt;
&lt;div&gt;&lt;font face="Courier New"&gt;&lt;span&gt;    &lt;font color="#0000ff"&gt;public class&lt;/font&gt; &lt;font color="#3366ff"&gt;MethodCommand&lt;/font&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span&gt;        : &lt;font color="#3366ff"&gt;Freezable&lt;/font&gt; &lt;font color="#339966"&gt;// Enable ElementName and DataContext bindings&lt;/font&gt;&lt;/span&gt;&lt;/font&gt;&lt;/div&gt;
&lt;/span&gt;&lt;/font&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;&lt;span&gt;    &lt;font color="#0000ff"&gt;public class&lt;/font&gt; &lt;font color="#3366ff"&gt;MethodArgument&lt;/font&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span&gt;        : &lt;font color="#3366ff"&gt;Freezable&lt;/font&gt; &lt;font color="#339966"&gt;// Enable ElementName and DataContext bindings&lt;/font&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;span&gt;Normally, ElementName and DataContext bindings are resolved based on the target dependency object's position within the element tree (or the namescope to which the target dependency object belongs).  But in this case, the target dependency object is not actually in the tree.  Instead, it is just a property value on another object.  That other object may or may not be in the tree.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;span&gt;The reason the Freezable trick works is because a Freezable object has its own notion of &lt;em&gt;"inheritance context"&lt;/em&gt;.  When the property engine sets the effective value of a dependency property, it looks at that new value to determine whether it is a dependency object that would like to be part of a special inheritance tree.  A Freezable is one such object that always wants to be in the inheritance tree when not frozen.  As such, when the Command property on Button is set to a Freezable in Mike's example (below), the framework adds the Button itself as the inheritance context of the Freezable.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;span&gt;&lt;font face="Courier New"&gt;    &lt;&lt;font color="#993300"&gt;Button&lt;/font&gt; &lt;font color="#ff0000"&gt;Content&lt;/font&gt;="Rename"&gt; &lt;br /&gt;
&lt;span&gt;      &lt;/span&gt;&lt;&lt;font color="#993300"&gt;Button.Command&lt;/font&gt;&gt;&lt;br /&gt;
&lt;span&gt;        &lt;/span&gt;&lt;&lt;font color="#993300"&gt;mc:MethodCommand&lt;/font&gt; &lt;font color="#ff0000"&gt;MethodName&lt;/font&gt;="Rename"&gt;&lt;br /&gt;
&lt;span&gt;          &lt;/span&gt;&lt;&lt;font color="#993300"&gt;mc:MethodArgument&lt;/font&gt; &lt;font color="#ff0000"&gt;Value&lt;/font&gt;="{&lt;font color="#993300"&gt;Binding&lt;/font&gt; &lt;font color="#ff0000"&gt;Text&lt;/font&gt;, &lt;font color="#ff0000"&gt;ElementName&lt;/font&gt;=_&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;span&gt;renameTextBox}" /&gt;&lt;br /&gt;
&lt;/span&gt;&lt;span&gt;        &lt;/span&gt;&lt;/&lt;font color="#993300"&gt;mc:MethodCommand&lt;/font&gt;&gt; &lt;br /&gt;
&lt;span&gt;      &lt;/span&gt;&lt;/&lt;font color="#993300"&gt;Button.Command&lt;/font&gt;&gt;&lt;br /&gt;
&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span&gt;&lt;font face="Courier New"&gt;&lt;span&gt;    &lt;/span&gt;&lt;/&lt;font color="#993300"&gt;Button&lt;/font&gt;&gt;&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;span&gt;The MethodCommand object above will now have the Button as its inheritance context. As such, even though MethodCommand is &lt;em&gt;not&lt;/em&gt; a FrameworkElement with a tree-inherited DataContext property, it still effectively inherits the data context of the Button.  A binding on any property of the MethodCommand object will use its inheritance context to arrive at an implicit binding source (which will be the DataContext of the Button in this case).&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;span&gt;The same construct is used to pass the inheritance context from a MethodCommand to its MethodArgument objects, so that their bound properties, too, can be resolved.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;span&gt;(Note that a Freezable can also have more than one inheritance context, but I'll leave the discussion of how multiple inheritance contexts are handled for a future post.) &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;span&gt;In addition to the above scenario (where a freezable is set as a dependency property value on a dependency object), it is this enhanced notion of an inheritance tree and inheritance context that allows bindings on brushes, animations, and other freezable objects to work when those objects are placed within a resource dictionary.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;span&gt;Mike's approach is definitely a cool trick and I've used it myself on occasion.  Of course, there are scenarios where inheriting from Freezable is not really an option.  In such situations, the hack I find most useful for enabling bindings on non-freezable objects is to artificially add such objects to the logical tree.  But I would love to see Microsoft publicly expose the ability to control an object's inheritance context so that all these hacks could just go away.  (Hint, hint! &lt;img alt="" src="http://www.drwpf.com/Blog/Providers/HtmlEditorProviders/Fck/FCKeditor/editor/images/smiley/msn/wink_smile.gif" /&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;/div&gt;</description>
      <link>http://www.drwpf.com/blog/Home/tabid/36/EntryID/36/Default.aspx</link>
      <author>ask@drwpf.com</author>
      <comments>http://www.drwpf.com/blog/Home/tabid/36/EntryID/36/Default.aspx#Comments</comments>
      <guid isPermaLink="true">http://www.drwpf.com/Blog/Default.aspx?tabid=36&amp;EntryID=36</guid>
      <pubDate>Fri, 23 May 2008 04:04:00 GMT</pubDate>
      <slash:comments>3</slash:comments>
      <trackback:ping>http://www.drwpf.com/Blog/DesktopModules/Blog/Trackback.aspx?id=36</trackback:ping>
    </item>
    <item>
      <title>I've been Pixel8ed!</title>
      <description>&lt;div class="jbr"&gt;
&lt;p&gt;If you are tired of reading my occasional ramblings, you now have the option of "&lt;em&gt;listening"&lt;/em&gt; to me expound on the merits of WPF!  &lt;img alt="" src="http://www.drwpf.com/Blog/Providers/HtmlEditorProviders/Fck/FCKeditor/editor/images/smiley/msn/teeth_smile.gif" /&gt;&lt;/p&gt;
&lt;p&gt;Craig Shoemaker has just published &lt;a href="http://pixel8.infragistics.com/shows/drwpf.aspx"&gt;this podcast&lt;/a&gt; on the Infragistics Pixel8 site.  I probably reveal a bit too much about myself in this interview, so if you want to preserve the mystery and romance of our relationship, maybe you shouldn't listen to it!   &lt;/p&gt;
&lt;p&gt;(What do you mean, you're not feeling the romance anymore?)&lt;/p&gt;
&lt;/div&gt;</description>
      <link>http://www.drwpf.com/blog/Home/tabid/36/EntryID/35/Default.aspx</link>
      <author>ask@drwpf.com</author>
      <comments>http://www.drwpf.com/blog/Home/tabid/36/EntryID/35/Default.aspx#Comments</comments>
      <guid isPermaLink="true">http://www.drwpf.com/Blog/Default.aspx?tabid=36&amp;EntryID=35</guid>
      <pubDate>Thu, 15 May 2008 12:00:00 GMT</pubDate>
      <slash:comments>1</slash:comments>
      <trackback:ping>http://www.drwpf.com/Blog/DesktopModules/Blog/Trackback.aspx?id=35</trackback:ping>
    </item>
    <item>
      <title>The Emancipation of Visual Children</title>
      <description>&lt;div class="jbr"&gt;
&lt;p&gt;&lt;font face="Times New Roman" size="3"&gt;&lt;img height="70" alt="" width="96" align="left" src="http://www.drwpf.com/Blog/Portals/0/Images/w.png" /&gt;hen in the course of elemental events it becomes necessary for one collection [of children] to dissolve the visual bands which have connected them with another and to assume among the powers of the framework, the separate and equal station to which the Laws of WPF and of &lt;a target="_blank" href="http://wpfdisciples.wordpress.com/"&gt;WPF's Disciples&lt;/a&gt; entitle them, a decent respect to the opinions of element-kind requires that they should declare the causes which impel them to the separation.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Times New Roman" size="3"&gt;We hold these truths to be self-evident, that all elements are created equal, that they are endowed by their Parser with certain inalienable Rights, that among these are... well, pretty much just &lt;em&gt;Liberty&lt;/em&gt;.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Times New Roman" size="3"&gt;And the same thing goes for &lt;em&gt;Logical&lt;/em&gt; children!&lt;/font&gt;&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;Okay, it's very possible that I'm enjoying the &lt;a target="_blank" href="http://www.hbo.com/films/johnadams/"&gt;HBO miniseries on John Adams&lt;/a&gt; just a little &lt;em&gt;too&lt;/em&gt; much.  &lt;img alt="" src="http://www.drwpf.com/Blog/Providers/HtmlEditorProviders/Fck/FCKeditor/editor/images/smiley/msn/wink_smile.gif" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Why am I declaring independence for visual and logical children?&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Because of the coolness that it enables in WPF!   If you'd like to know more about my epic struggle to free the visuals, please check out the article that I published this past weekend on The Code Project:&lt;/p&gt;
&lt;p dir="ltr" style="margin-right: 0px"&gt;     &lt;a target="_blank" href="http://www.codeproject.com/KB/WPF/ConceptualChildren.aspx"&gt;Conceptual Children:  A powerful new concept in WPF&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;A few people have suggested that I should write a book called &lt;em&gt;"Hacking WPF." &lt;/em&gt;  (Okay, certainly no publishers have suggested such a thing... just some of the folks I work with.)  I'm sure someone is already working on the book and I don't think I could ever reconcile the book title with my view of what I do for a living.  I like to think of myself as a moderately sophisticated developer (rather than a hacker) that embraces and leverages the strengths of the platform.  &lt;em&gt;BUT...&lt;/em&gt;  If I ever were to write such a book, this would definitely be the type of article it would include!&lt;/p&gt;
&lt;p&gt;Warning:  It's pretty geeky and perhaps on the advanced side of intermediate.&lt;/p&gt;
&lt;/div&gt;</description>
      <link>http://www.drwpf.com/blog/Home/tabid/36/EntryID/34/Default.aspx</link>
      <author>ask@drwpf.com</author>
      <comments>http://www.drwpf.com/blog/Home/tabid/36/EntryID/34/Default.aspx#Comments</comments>
      <guid isPermaLink="true">http://www.drwpf.com/Blog/Default.aspx?tabid=36&amp;EntryID=34</guid>
      <pubDate>Mon, 07 Apr 2008 10:37:00 GMT</pubDate>
      <slash:comments>3</slash:comments>
      <trackback:ping>http://www.drwpf.com/Blog/DesktopModules/Blog/Trackback.aspx?id=34</trackback:ping>
    </item>
    <item>
      <title>ItemsControl:  'R' is for Rob has a Customer</title>
      <description>&lt;div class="jbr"&gt;
&lt;p&gt;&lt;a target="_blank" href="http://blogs.windowsclient.net/rob_relyea/archive/2008/03/26/wpf-menu-tricks-anybody-built-a-vista-explorer-view-menu-in-wpf.aspx"&gt;Rob Relyea&lt;/a&gt; has a customer (I wonder if it's &lt;a target="_blank" href="http://work.j832.com/2008/03/rob-relyea-hates-my-mom.html"&gt;Kevin's Mom&lt;/a&gt;?) who is looking for an implementation of &lt;a target="_blank" href="http://blogs.windowsclient.net/rob_relyea/archive/2008/03/26/wpf-menu-tricks-anybody-built-a-vista-explorer-view-menu-in-wpf.aspx"&gt;this Views menu&lt;/a&gt; that is used in Vista's Explorer window. &lt;/p&gt;
&lt;p&gt;Since I haven't seen anyone respond, I put together a little app to demonstrate how you might do this in WPF.  (Yes, I'll use any excuse to knock off another letter of the alphabet in this series!)&lt;/p&gt;
&lt;p&gt;&lt;img height="331" alt="" width="499" src="http://www.drwpf.com/Blog/Portals/0/Images/ItemsControl/R/ViewsMenu.jpg" /&gt;&lt;/p&gt;
&lt;p&gt;This really does qualify for the ItemsControl series.  Not only is a ContextMenu an ItemsControl, but I also demonstrate how to use the menu to control another ItemsControl... a ListView.  In total, this sample demonstrates binding to a collection (&lt;a target="_blank" href="http://drwpf.com/blog/Home/tabid/36/EntryID/18/Default.aspx"&gt;'C' is for Collection&lt;/a&gt;), creating a dynamic item template (&lt;a target="_blank" href="http://drwpf.com/blog/Home/tabid/36/EntryID/24/Default.aspx"&gt;'D' is for DataTemplate&lt;/a&gt;), and using triggers to adjust properties on a custom items panel (&lt;a target="_blank" href="http://drwpf.com/blog/Home/tabid/36/EntryID/28/Default.aspx"&gt;'P' is for Panel&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;&lt;img height="288" alt="" width="475" src="http://www.drwpf.com/Blog/Portals/0/Images/ItemsControl/R/ViewsMenuSample.jpg" /&gt;&lt;/p&gt;
&lt;p&gt;One would typically implement a user control for a menu like this because the control is really a collection of several other controls (in this case, a slider, several menu items, and a popup) with well-defined, static visuals .  However, because I wanted several behaviors that come for free with a context menu, I just took a shortcut and derived directly from ContextMenu.  I used the control's template to define the visuals.  Make no mistake... this is not a &lt;em&gt;"custom control"&lt;/em&gt; in the WPF &lt;em&gt;lookless &lt;/em&gt;sense of the term.  The implementation is very much tied to the visual elements in the template.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Disclaimer:&lt;/em&gt;  This is just an &lt;em&gt;example&lt;/em&gt; of how such a menu could be constructed.  I readily admit that I didn't spend much time on it and there are lots of improvements that could be made.  Also, there are some interesting behaviors within the Views menu in Vista.  For example, the menu is always opened such that the slider's thumb is directly under the mouse.  I implemented this feature and some of the others that I noticed, but I didn't spend a lot of time trying to precisely imitate all the behaviors.  There's certainly still some cleaning up to be done if you require pixel perfection in sizing, iconography, etc.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.drwpf.com/Blog/Portals/0/Samples/VistaViewsMenuSample.zip"&gt;Download the complete sample here.&lt;/a&gt; &lt;/p&gt;
&lt;/div&gt;</description>
      <link>http://www.drwpf.com/blog/Home/tabid/36/EntryID/33/Default.aspx</link>
      <author>ask@drwpf.com</author>
      <comments>http://www.drwpf.com/blog/Home/tabid/36/EntryID/33/Default.aspx#Comments</comments>
      <guid isPermaLink="true">http://www.drwpf.com/Blog/Default.aspx?tabid=36&amp;EntryID=33</guid>
      <pubDate>Fri, 28 Mar 2008 09:15:00 GMT</pubDate>
      <slash:comments>9</slash:comments>
      <trackback:ping>http://www.drwpf.com/Blog/DesktopModules/Blog/Trackback.aspx?id=33</trackback:ping>
    </item>
    <item>
      <title>ItemsControl:  'I' is for Item Container</title>
      <description>&lt;div class="jbr"&gt;
&lt;p&gt;Oh look... I did it again!  I promised to write &lt;em&gt;'G' is for Generator&lt;/em&gt; and then I come out with &lt;em&gt;'I' is for Item Container&lt;/em&gt;.  I'm like a bad TV series that just keeps leading you on... Then just when you think you're about to discover the true identity of the evil mastermind, the plot takes an unexpected turn.  Please tune in next time when we'll unveil... &lt;em&gt;The Generator!&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Okay, this episode isn't really a departure from the plan.  I simply realized that I had too much content for a single post, so I broke our look at item containers and item container generation into two separate issues.  If it makes you feel better, you can think of this as &lt;em&gt;'G' is for Generator, Part I&lt;/em&gt;.  Although we won't actually talk about container &lt;em&gt;“generation”&lt;/em&gt; in this episode, we will lay the groundwork by talking about the containers that get &lt;em&gt;“generated”&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;In our last episode, &lt;a target="_blank" href="http://drwpf.com/blog/Home/tabid/36/EntryID/28/Default.aspx"&gt;&lt;em&gt;'P' is for Panel&lt;/em&gt;&lt;/a&gt;, we discovered that an ItemsControl leverages a &lt;em&gt;panel&lt;/em&gt; to layout its children.  We call this panel the &lt;em&gt;items host&lt;/em&gt; (or the &lt;em&gt;items panel&lt;/em&gt;).  It seems quite appropriate to use a panel to layout the items, since that is exactly the purpose for which a panel is designed... namely, to size and position a collection of visual children.&lt;/p&gt;
&lt;p&gt;In an earlier article, &lt;a target="_blank" href="http://drwpf.com/blog/Home/tabid/36/EntryID/24/Default.aspx"&gt;&lt;em&gt;'D' is for DataTemplate&lt;/em&gt;&lt;/a&gt;, we saw that a data template can be used to specify the visuals that represent an item within the Items collection of an ItemsControl.  And since &lt;em&gt;any&lt;/em&gt; object can belong to the Items collection, this architecture allows for a diverse and disparate collection of visuals within an ItemsControl. &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;A Motley Crew of Items&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Consider the following example:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;  &lt;&lt;font color="#993300"&gt;ItemsControl&lt;/font&gt; &lt;font color="#ff0000"&gt;HorizontalAlignment&lt;/font&gt;="Left"&gt;&lt;br /&gt;
    &lt;&lt;font color="#993300"&gt;TextBox&lt;/font&gt; &lt;font color="#ff0000"&gt;Name&lt;/font&gt;="tb" &lt;font color="#ff0000"&gt;Margin&lt;/font&gt;="2" &lt;font color="#ff0000"&gt;Text&lt;/font&gt;="Test" /&gt;&lt;br /&gt;
    &lt;&lt;font color="#993300"&gt;sys:String&lt;/font&gt;&gt;http://drwpf.com/blog/&lt;/&lt;font color="#993300"&gt;sys:String&lt;/font&gt;&gt;&lt;br /&gt;
    &lt;&lt;font color="#993300"&gt;sys:String&lt;/font&gt;&gt;http://forums.microsoft.com/MSDN/&lt;/&lt;font color="#993300"&gt;sys:String&lt;/font&gt;&gt;&lt;br /&gt;
    &lt;&lt;font color="#993300"&gt;x:Static&lt;/font&gt; &lt;font color="#ff0000"&gt;Member&lt;/font&gt;="ApplicationCommands.Copy" /&gt;&lt;br /&gt;
    &lt;&lt;font color="#993300"&gt;x:Static&lt;/font&gt; &lt;font color="#ff0000"&gt;Member&lt;/font&gt;="ApplicationCommands.Cut" /&gt;&lt;br /&gt;
    &lt;&lt;font color="#993300"&gt;x:Static&lt;/font&gt; &lt;font color="#ff0000"&gt;Member&lt;/font&gt;="ApplicationCommands.Paste" /&gt;&lt;br /&gt;
    &lt;&lt;font color="#993300"&gt;x:Static&lt;/font&gt; &lt;font color="#ff0000"&gt;Member&lt;/font&gt;="ApplicationCommands.SelectAll" /&gt;&lt;br /&gt;
  &lt;/&lt;font color="#993300"&gt;ItemsControl&lt;/font&gt;&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;img height="186" alt="" hspace="5" width="203" align="right" src="http://www.drwpf.com/Blog/Portals/0/Images/ItemsControl/I/StringsAndCommands.jpg" /&gt;This ItemsControl has 7 items explicitly added to its Items collection:  one TextBox, two strings, and four routed commands.  You could easily define a data template for the String type to display the strings as hyperlinks and another data template for the RoutedUICommand type to display the commands as buttons.  Then the ItemsControl might have the visual representation shown here.&lt;/p&gt;
&lt;p&gt;Since a StackPanel is the default items host for an ItemsControl, the children are nicely stacked.  If you'd like to observe this example in &lt;a target="_blank" href="http://kaxaml.com/"&gt;Kaxaml&lt;/a&gt; (or XamlPad if you're old school), the very simple markup is &lt;a href="http://www.drwpf.com/Blog/Portals/0/Code/TemplatedStringsAndCommands.xaml.txt" target="_blank"&gt;available here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Some Common Problems to Consider&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Below are several common problems that need to be considered when working with an ItemsControl in WPF.  We should keep these in mind as we look at item containers in this post and item container generators in the next episode.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Problem 1:  Custom Child Placement&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;A panel is capable of arranging all types of UI elements, so it can certainly handle such a motley crew of children, but imagine that the panel is a Canvas and you want to provide custom placement of the items within your collection.  In this case, you would need to set the attached Canvas properties (Canvas.Left, Canvas.Top, etc) on all of the differing elements in your collection of children.  This could be a real hassle to maintain with so many different types of visuals.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Problem 2:  Mappings between Items and Visuals&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;And remember that the actual items may simply be string or command objects.  These objects have no inherent visual representation without their data templates.  Once a data template has been inflated for an item and the visuals have been added to your ItemsControl, how do you map the visuals back to the items and vice versa?&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Problem 3:  UI Virtualization&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;What if there are thousands of items in your ItemsControl?  Unless the items are very small, they will not all appear within the viewport of the control at the same time.  We definitely do not want to pay a high performance penalty for instantiating visuals for items that are not visible.  How can we make sure that only visuals for the visible items (give or take a few) are in memory at any given moment?&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Problem 4:  Consistent Item Chrome&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Another thing that you might want to do in an ItemsControl is provide a common&lt;em&gt; “chrome”&lt;/em&gt; for each item.  Since the items themselves can be quite diverse and the items panel might not be something as predictable as a StackPanel, an ItemsControl might sometimes appear haphazard.  One way to bring a sense of uniformity to such a collection is to provide a consistent background or chrome for each item.  Is it possible to do this without directly adding the chrome to the item's data template?&lt;br /&gt;
 &lt;br /&gt;
&lt;em&gt;Problem 5:  Visible Selection State&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Finally, if the ItemsControl is a &lt;em&gt;Selector&lt;/em&gt; (e.g., ListBox, ListView, TreeView, ComboBox, etc), how would you go about showing a uniform selection state for all of the differing children?&lt;/p&gt;
&lt;p&gt;It would certainly be a lot easier to deal with all of the above issues if the children of the items panel were all the same type of element.  Enter the &lt;em&gt;item container&lt;/em&gt;...&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is an item container?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;An item container is an automatically generated "wrapper" element for items within an ItemsControl. It is called an item container because it actually “contains” an item from the Items collection.  More specifically, the container is the control which contains the visual representation for an item.  If the item has a data template, the container is the control into which that data template is inflated.&lt;/p&gt;
&lt;p&gt;Let's revisit a simple ListBox example that we saw earlier in &lt;em&gt;&lt;a href="http://drwpf.com/blog/Home/tabid/36/EntryID/24/Default.aspx" target="_blank"&gt;'D' is for DataTemplate&lt;/a&gt;&lt;/em&gt;.  Here is a ListBox that displays a collection of Characters:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;  &lt;&lt;font color="#993300"&gt;ListBox &lt;/font&gt;&lt;font color="#ff0000"&gt;ItemsSource&lt;/font&gt;="{&lt;font color="#993300"&gt;Binding&lt;/font&gt; &lt;font color="#ff0000"&gt;Source&lt;/font&gt;={&lt;font color="#993300"&gt;StaticResource&lt;/font&gt; &lt;font color="#ff0000"&gt;Characters&lt;/font&gt;}}" /&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Note that we're using a ListBox in &lt;em&gt;ItemsSource Mode&lt;/em&gt; (see &lt;em&gt;&lt;a href="http://drwpf.com/blog/Home/tabid/36/EntryID/18/Default.aspx" target="_blank"&gt;'C' is for Collection&lt;/a&gt;&lt;/em&gt;).  The collection of characters is the same as before:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;  &lt;&lt;font color="#993300"&gt;src:CharacterCollection&lt;/font&gt; &lt;font color="#ff0000"&gt;x:Key&lt;/font&gt;="Characters"&gt;&lt;br /&gt;
    &lt;&lt;font color="#993300"&gt;src:Character&lt;/font&gt; &lt;font color="#ff0000"&gt;First&lt;/font&gt;="Bart" &lt;font color="#ff0000"&gt;Last&lt;/font&gt;="Simpson" &lt;font color="#ff0000"&gt;Age&lt;/font&gt;="10" &lt;br /&gt;
        &lt;font color="#ff0000"&gt;Gender&lt;/font&gt;="Male" &lt;font color="#ff0000"&gt;Image&lt;/font&gt;="images/bart.png" /&gt;&lt;br /&gt;
    &lt;&lt;font color="#993300"&gt;src:Character&lt;/font&gt; &lt;font color="#ff0000"&gt;First&lt;/font&gt;="Homer" &lt;font color="#ff0000"&gt;Last&lt;/font&gt;="Simpson" &lt;font color="#ff0000"&gt;Age&lt;/font&gt;="38" &lt;br /&gt;
        &lt;font color="#ff0000"&gt;Gender&lt;/font&gt;="Male" &lt;font color="#ff0000"&gt;Image&lt;/font&gt;="images/homer.png" /&gt;&lt;br /&gt;
    &lt;&lt;font color="#993300"&gt;src:Character&lt;/font&gt; &lt;font color="#ff0000"&gt;First&lt;/font&gt;="Lisa" &lt;font color="#ff0000"&gt;Last&lt;/font&gt;="Bouvier" &lt;font color="#ff0000"&gt;Age&lt;/font&gt;="8" &lt;br /&gt;
        &lt;font color="#ff0000"&gt;Gender&lt;/font&gt;="Female" &lt;font color="#ff0000"&gt;Image&lt;/font&gt;="images/lisa.png" /&gt;&lt;br /&gt;
    &lt;&lt;font color="#993300"&gt;src:Character&lt;/font&gt; &lt;font color="#ff0000"&gt;First&lt;/font&gt;="Maggie" &lt;font color="#ff0000"&gt;Last&lt;/font&gt;="Simpson" &lt;font color="#ff0000"&gt;Age&lt;/font&gt;="0" &lt;br /&gt;
        &lt;font color="#ff0000"&gt;Gender&lt;/font&gt;="Female" &lt;font color="#ff0000"&gt;Image&lt;/font&gt;="images/maggie.png" /&gt;&lt;br /&gt;
    &lt;&lt;font color="#993300"&gt;src:Character&lt;/font&gt; &lt;font color="#ff0000"&gt;First&lt;/font&gt;="Marge" &lt;font color="#ff0000"&gt;Last&lt;/font&gt;="Bouvier" &lt;font color="#ff0000"&gt;Age&lt;/font&gt;="38" &lt;br /&gt;
        &lt;font color="#ff0000"&gt;Gender&lt;/font&gt;="Female" &lt;font color="#ff0000"&gt;Image&lt;/font&gt;="images/marge.png" /&gt;&lt;br /&gt;
  &lt;/&lt;font color="#993300"&gt;src:CharacterCollection&lt;/font&gt;&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;We can define a very simple data template to display the characters:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;  &lt;&lt;font color="#993300"&gt;DataTemplate&lt;/font&gt; &lt;font color="#ff0000"&gt;DataType&lt;/font&gt;=" {&lt;font color="#993300"&gt;x:Type&lt;/font&gt; &lt;font color="#ff0000"&gt;src:Character&lt;/font&gt;} "&gt;&lt;br /&gt;
    &lt;&lt;font color="#993300"&gt;StackPanel&lt;/font&gt; &lt;font color="#ff0000"&gt;Orientation&lt;/font&gt;="Vertical" &lt;font color="#ff0000"&gt;Margin&lt;/font&gt;="5"&gt;&lt;br /&gt;
      &lt;&lt;font color="#993300"&gt;TextBlock&lt;/font&gt; &lt;font color="#ff0000"&gt;FontWeight&lt;/font&gt;="Bold" &lt;font color="#ff0000"&gt;Text&lt;/font&gt;="{&lt;font color="#993300"&gt;Binding&lt;/font&gt; &lt;font color="#ff0000"&gt;First&lt;/font&gt;}"&lt;br /&gt;
          &lt;font color="#ff0000"&gt;TextAlignment&lt;/font&gt;="Center" /&gt;&lt;br /&gt;
      &lt;&lt;font color="#993300"&gt;Image&lt;/font&gt; &lt;font color="#ff0000"&gt;Margin&lt;/font&gt;="0,5,0,0" &lt;font color="#ff0000"&gt;Source&lt;/font&gt;="{&lt;font color="#993300"&gt;Binding&lt;/font&gt; &lt;font color="#ff0000"&gt;Image&lt;/font&gt;}" /&gt;&lt;br /&gt;
    &lt;/&lt;font color="#993300"&gt;StackPanel&lt;/font&gt;&gt;&lt;br /&gt;
  &lt;/&lt;font color="#993300"&gt;DataTemplate&lt;/font&gt;&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;img hspace="10" align="right" alt="" src="http://www.drwpf.com/Blog/Portals/0/Images/ItemsControl/I/CharacterListBox.jpg" /&gt;&lt;/p&gt;
&lt;p&gt;This gives us the ListBox at the right.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Where's the container?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Supposedly, the visuals for each of the characters in this example are wrapped within an item container.  But I don't see a container!  Where is the container?  More importantly, &lt;em&gt;what&lt;/em&gt; is the container?  The answer to that question actually depends on the ItemsControl.  In this case, the ItemsControl is a ListBox.  The item container for a ListBox happens to be a control called ListBoxItem.&lt;/p&gt;
&lt;p&gt;You may not think you see a ListBoxItem in the control, but if you select an item, you will notice that the background of the entire selected item becomes blue and the TextBlock within the selected item shows up with a white Foreground (see the image below).  The blue that you are seeing here is the background of the item container.&lt;/p&gt;
&lt;p&gt;&lt;img hspace="10" align="middle" vspace="5" alt="" src="http://www.drwpf.com/Blog/Portals/0/Images/ItemsControl/I/CharacterListBoxWithSelection.jpg" /&gt;&lt;/p&gt;
&lt;p&gt;These visual changes happen automatically without any changes to our Character data template.  They are the result of the template within the default style for ListBoxItem, (along with some triggers in that template).&lt;br /&gt;
 &lt;br /&gt;
Wow!  The container has a pretty important role in this scenario, especially if you think you might like to alter the visuals used to depict item selection.  Clearly, this merits further investigation...&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Understanding the Item Container and its Style&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;As just mentioned, the selection state for a ListBoxItem is defined within the control's style and template.  Anytime you are working with an ItemsControl, I strongly recommend that you take time to understand the control's item container as well as the default style for that container.  So let's just take a moment to look at some aspects of ListBoxItem and &lt;a onclick="window.open
(this.href,'','resizable=yes,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,fullscreen=no,dependent=no,status'); return false" href="http://www.drwpf.com/Blog/Portals/0/Code/DefaultListBoxItemStyle.xaml.txt"&gt;the default ListBoxItem style&lt;/a&gt;, as defined for the Vista Aero theme (from Aero.NormalColor.xaml).&lt;/p&gt;
&lt;ol&gt;
    &lt;li&gt;The Background of the ListBoxItem is set to Transparent.  This is important.  By using a Transparent brush rather than the default null brush, the ListBoxItem becomes hittable (or visible to hittesting by input devices).  In other words, a mouse hittest will find the item, thereby allowing it to be selected when the transparent portion is clicked. &lt;br /&gt;
     &lt;/li&gt;
    &lt;li&gt;HorizontalContentAlignment and VerticalContentAlignment on the ListBoxItem are data bound to the properties of the same names on ListBox.  As such, if you'd like &lt;em&gt;all&lt;/em&gt; ListBoxItems to left-align their content, you can simply set HorizontalContentAlignment to Left on the ListBox itself.  This is very handy to know and you probably wouldn't know it without looking at the style.  &lt;br /&gt;
     &lt;/li&gt;
    &lt;li&gt;The default template for ListBoxItem consists of nothing more than a ContentPresenter within a Border. &lt;br /&gt;
     &lt;/li&gt;
    &lt;li&gt;ListBoxItem exposes a dependency property called IsSelected.  This is pretty common for the item container of a Selector control.  In fact, the Selector class is where the IsSelected property is originally registered with the property engine.  ListBoxItem and other containers simply add themselves as owners for the property.  As such, Selector.IsSelected provides a useful trigger property for showing that a container is selected.  &lt;br /&gt;
     &lt;/li&gt;
    &lt;li&gt;There are indeed several triggers within the control template that alter the container's appearance based on whether it is selected, active, and/or enabled.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;em&gt;Sidenote:&lt;/em&gt;  If you are new to styling and templating in WPF, recognize that all of the native control styles and templates are available in theme files that ship as part of the framework SDK or with Blend.  There are actually many different ways you can view these styles, as I describe in &lt;a target="_blank" href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2425027&amp;SiteID=1"&gt;this forum post&lt;/a&gt;.  Designers often go straight to a tool like Blend, when they want to explore/modify a control template.  This is certainly fine too, but I prefer going to the theme file so I can see both the style and template declarations together.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The ItemContainerStyle Property&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;That's great!  Now we understand the default style and template.  What can we do with this knowledge?  Well, quite a bit, actually.  It turns out that it's very easy to define our own item container style.  We simply need to set the ItemContainerStyle property of the ItemsControl, as shown here:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;  &lt;&lt;font color="#993300"&gt;ListBox &lt;/font&gt;&lt;font color="#ff0000"&gt;ItemsSource&lt;/font&gt;="{&lt;font color="#993300"&gt;Binding&lt;/font&gt; &lt;font color="#ff0000"&gt;Source&lt;/font&gt;={&lt;font color="#993300"&gt;StaticResource&lt;/font&gt; &lt;font color="#ff0000"&gt;Characters&lt;/font&gt;}}"&lt;br /&gt;
      &lt;font color="#ff0000"&gt;ItemContainerStyle&lt;/font&gt;="{&lt;font color="#993300"&gt;StaticResource&lt;/font&gt; &lt;font color="#ff0000"&gt;CharacterContainerStyle&lt;/font&gt;}" /&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Next, we need to define the style.  We will use the container style to add some standard chrome to the items in our ListBox by redefining the ListBoxItem's template, as shown below.  You don't need to get too wrapped up in the nitty gritty of this style (unless that's your thing).  Just note that there are a handful of properties being set, and one of them happens to be the Template property.&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;  &lt;&lt;font color="#993300"&gt;Style&lt;/font&gt; &lt;font color="#ff0000"&gt;x:Key&lt;/font&gt;="CharacterContainerStyle" &lt;font color="#ff0000"&gt;TargetType&lt;/font&gt;="{&lt;font color="#993300"&gt;x:Type&lt;/font&gt; &lt;font color="#ff0000"&gt;ListBoxItem&lt;/font&gt;}"&gt;&lt;br /&gt;
    &lt;&lt;font color="#993300"&gt;Setter&lt;/font&gt; &lt;font color="#ff0000"&gt;Property&lt;/font&gt;="Background" &lt;font color="#ff0000"&gt;Value&lt;/font&gt;="#FF3B0031" /&gt;&lt;br /&gt;
    &lt;&lt;font color="#993300"&gt;Setter&lt;/font&gt; &lt;font color="#ff0000"&gt;Property&lt;/font&gt;="FocusVisualStyle" &lt;font color="#ff0000"&gt;Value&lt;/font&gt;="{&lt;font color="#993300"&gt;x:Null&lt;/font&gt;}" /&gt;&lt;br /&gt;
    &lt;&lt;font color="#993300"&gt;Setter&lt;/font&gt; &lt;font color="#ff0000"&gt;Property&lt;/font&gt;="Width" &lt;font color="#ff0000"&gt;Value&lt;/font&gt;="75" /&gt;&lt;br /&gt;
    &lt;&lt;font color="#993300"&gt;Setter&lt;/font&gt; &lt;font color="#ff0000"&gt;Property&lt;/font&gt;="Margin" &lt;font color="#ff0000"&gt;Value&lt;/font&gt;="5,2" /&gt;&lt;br /&gt;
    &lt;&lt;font color="#993300"&gt;Setter&lt;/font&gt; &lt;font color="#ff0000"&gt;Property&lt;/font&gt;="Padding" &lt;font color="#ff0000"&gt;Value&lt;/font&gt;="3" /&gt;&lt;br /&gt;
    &lt;&lt;font color="#993300"&gt;Setter&lt;/font&gt; &lt;font color="#ff0000"&gt;Property&lt;/font&gt;="Template"&gt;&lt;br /&gt;
      &lt;&lt;font color="#993300"&gt;Setter.Value&lt;/font&gt;&gt;&lt;br /&gt;
        &lt;&lt;font color="#993300"&gt;ControlTemplate&lt;/font&gt; &lt;font color="#ff0000"&gt;TargetType&lt;/font&gt;="{&lt;font color="#993300"&gt;x:Type&lt;/font&gt; &lt;font color="#ff0000"&gt;ListBoxItem&lt;/font&gt;}"&gt;&lt;br /&gt;
          &lt;&lt;font color="#993300"&gt;Grid&lt;/font&gt;&gt;&lt;br /&gt;
            &lt;&lt;font color="#993300"&gt;Rectangle&lt;/font&gt; &lt;font color="#ff0000"&gt;StrokeThickness&lt;/font&gt;="1" &lt;font color="#ff0000"&gt;Stroke&lt;/font&gt;="Transparent"&lt;br /&gt;
                &lt;font color="#ff0000"&gt;RadiusX&lt;/font&gt;="5" &lt;font color="#ff0000"&gt;RadiusY&lt;/font&gt;="5" &lt;font color="#ff0000"&gt;Fill&lt;/font&gt;="White"  /&gt;&lt;br /&gt;
            &lt;&lt;font color="#993300"&gt;Grid&lt;/font&gt;&gt;&lt;br /&gt;
              &lt;&lt;font color="#993300"&gt;Rectangle&lt;/font&gt; &lt;font color="#ff0000"&gt;x:Name&lt;/font&gt;="BackgroundRect" &lt;font color="#ff0000"&gt;Opacity&lt;/font&gt;="0.5" &lt;font color="#ff0000"&gt;StrokeThickness&lt;/font&gt;="1" &lt;br /&gt;
                  &lt;font color="#ff0000"&gt;Stroke&lt;/font&gt;="Transparent" &lt;font color="#ff0000"&gt;RadiusX&lt;/font&gt;="5" &lt;font color="#ff0000"&gt;RadiusY&lt;/font&gt;="5" &lt;br /&gt;
                  &lt;font color="#ff0000"&gt;Fill&lt;/font&gt;=" {&lt;font color="#993300"&gt;TemplateBinding&lt;/font&gt; &lt;font color="#ff0000"&gt;Background&lt;/font&gt;} " /&gt;&lt;br /&gt;
              &lt;&lt;font color="#993300"&gt;Rectangle&lt;/font&gt; &lt;font color="#ff0000"&gt;StrokeThickness&lt;/font&gt;="1" &lt;font color="#ff0000"&gt;Stroke&lt;/font&gt;="Black" &lt;font color="#ff0000"&gt;RadiusX&lt;/font&gt;="3" &lt;font color="#ff0000"&gt;RadiusY&lt;/font&gt;="3" &gt;&lt;br /&gt;
                &lt;&lt;font color="#993300"&gt;Rectangle.Fill&lt;/font&gt;&gt;&lt;br /&gt;
                  &lt;&lt;font color="#993300"&gt;LinearGradientBrush&lt;/font&gt; &lt;font color="#ff0000"&gt;StartPoint&lt;/font&gt;="-0.51,0.41" &lt;font color="#ff0000"&gt;EndPoint&lt;/font&gt;="1.43,0.41"&gt;&lt;br /&gt;
                    &lt;&lt;font color="#993300"&gt;LinearGradientBrush.GradientStops&lt;/font&gt;&gt;&lt;br /&gt;
                      &lt;&lt;font color="#993300"&gt;GradientStop&lt;/font&gt; &lt;font color="#ff0000"&gt;Color&lt;/font&gt;="Transparent" &lt;font color="#ff0000"&gt;Offset&lt;/font&gt;="0"/&gt;&lt;br /&gt;
                      &lt;&lt;font color="#993300"&gt;GradientStop&lt;/font&gt; &lt;font color="#ff0000"&gt;Color&lt;/font&gt;="#60FFFFFF" &lt;font color="#ff0000"&gt;Offset&lt;/font&gt;="1"/&gt;&lt;br /&gt;
                    &lt;/&lt;font color="#993300"&gt;LinearGradientBrush.GradientStops&lt;/font&gt;&gt;&lt;br /&gt;
                  &lt;/&lt;font color="#993300"&gt;LinearGradientBrush&lt;/font&gt;&gt;&lt;br /&gt;
                &lt;/&lt;font color="#993300"&gt;Rectangle.Fill&lt;/font&gt;&gt;&lt;br /&gt;
              &lt;/&lt;font color="#993300"&gt;Rectangle&lt;/font&gt;&gt;&lt;br /&gt;
              &lt;&lt;font color="#993300"&gt;Grid&lt;/font&gt;&gt;&lt;br /&gt;
                &lt;&lt;font color="#993300"&gt;Grid.RowDefinitions&lt;/font&gt;&gt;&lt;br /&gt;
                  &lt;&lt;font color="#993300"&gt;RowDefinition&lt;/font&gt; &lt;font color="#ff0000"&gt;Height&lt;/font&gt;="0.6*"/&gt;&lt;br /&gt;
                  &lt;&lt;font color="#993300"&gt;RowDefinition&lt;/font&gt; &lt;font color="#ff0000"&gt;Height&lt;/font&gt;="0.4*"/&gt;&lt;br /&gt;
                &lt;/&lt;font color="#993300"&gt;Grid.RowDefinitions&lt;/font&gt;&gt;&lt;br /&gt;
                &lt;&lt;font color="#993300"&gt;Rectangle&lt;/font&gt; &lt;font color="#ff0000"&gt;RadiusX&lt;/font&gt;="3" &lt;font color="#ff0000"&gt;RadiusY&lt;/font&gt;="3" &lt;font color="#ff6600"&gt;Margin&lt;/font&gt;="3" &lt;br /&gt;
                   &lt;font color="#993300"&gt; Grid.RowSpan&lt;/font&gt;="1" &lt;font color="#ff0000"&gt;Grid.Row&lt;/font&gt;="0"  &gt;&lt;br /&gt;
                  &lt;&lt;font color="#993300"&gt;Rectangle.Fill&lt;/font&gt;&gt;&lt;br /&gt;
                    &lt;&lt;font color="#993300"&gt;LinearGradientBrush&lt;/font&gt;  &lt;font color="#ff0000"&gt;EndPoint&lt;/font&gt;="0,0" &lt;font color="#ff0000"&gt;StartPoint&lt;/font&gt;="0,1"&gt;&lt;br /&gt;
                      &lt;&lt;font color="#993300"&gt;GradientStop&lt;/font&gt; &lt;font color="#ff0000"&gt;Color&lt;/font&gt;="#44FFFFFF" &lt;font color="#ff0000"&gt;Offset&lt;/font&gt;="0"/&gt;&lt;br /&gt;
                      &lt;&lt;font color="#993300"&gt;GradientStop&lt;/font&gt; &lt;font color="#ff0000"&gt;Color&lt;/font&gt;="#66FFFFFF" &lt;font color="#ff0000"&gt;Offset&lt;/font&gt;="1"/&gt;&lt;br /&gt;
                    &lt;/&lt;font color="#993300"&gt;LinearGradientBrush&lt;/font&gt;&gt;&lt;br /&gt;
                  &lt;/&lt;font color="#993300"&gt;Rectangle.Fill&lt;/font&gt;&gt;&lt;br /&gt;
                &lt;/&lt;font color="#993300"&gt;Rectangle&lt;/font&gt;&gt;&lt;br /&gt;
              &lt;/&lt;font color="#993300"&gt;Grid&lt;/font&gt;&gt;&lt;br /&gt;
              &lt;&lt;font color="#993300"&gt;ContentPresenter&lt;/font&gt; &lt;font color="#ff0000"&gt;x:Name&lt;/font&gt;="ContentHost" &lt;font color="#ff0000"&gt;Margin&lt;/font&gt;="{&lt;font color="#993300"&gt;TemplateBinding&lt;/font&gt; &lt;font color="#ff0000"&gt;Padding&lt;/font&gt;}" &lt;br /&gt;
                  &lt;font color="#ff0000"&gt;HorizontalAlignment&lt;/font&gt;="{&lt;font color="#993300"&gt;TemplateBinding&lt;/font&gt; &lt;font color="#ff0000"&gt;HorizontalContentAlignment&lt;/font&gt;}" &lt;br /&gt;
                  &lt;font color="#ff0000"&gt;VerticalAlignment&lt;/font&gt;="{&lt;font color="#993300"&gt;TemplateBinding&lt;/font&gt; &lt;font color="#ff0000"&gt;VerticalContentAlignment&lt;/font&gt;}" /&gt;&lt;br /&gt;
              &lt;&lt;font color="#993300"&gt;Rectangle&lt;/font&gt; &lt;font color="#ff0000"&gt;Fill&lt;/font&gt;="{&lt;font color="#993300"&gt;x:Null&lt;/font&gt;}" &lt;font color="#ff0000"&gt;Stroke&lt;/font&gt;="#FFFFFFFF" &lt;br /&gt;
                  &lt;font color="#ff0000"&gt;RadiusX&lt;/font&gt;="3" &lt;font color="#ff0000"&gt;RadiusY&lt;/font&gt;="3" &lt;font color="#ff0000"&gt;Margin&lt;/font&gt;="1" /&gt;&lt;br /&gt;
            &lt;/&lt;font color="#993300"&gt;Grid&lt;/font&gt;&gt;&lt;br /&gt;
          &lt;/&lt;font color="#993300"&gt;Grid&lt;/font&gt;&gt;&lt;br /&gt;
        &lt;/&lt;font color="#993300"&gt;ControlTemplate&lt;/font&gt;&gt;&lt;br /&gt;
      &lt;/&lt;font color="#993300"&gt;Setter.Value&lt;/font&gt;&gt;&lt;br /&gt;
    &lt;/&lt;font color="#993300"&gt;Setter&lt;/font&gt;&gt;&lt;br /&gt;
  &lt;/&lt;font color="#993300"&gt;Style&lt;/font&gt;&gt;&lt;br /&gt;
&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;img hspace="10" align="right" alt="" src="http://www.drwpf.com/Blog/Portals/0/Images/ItemsControl/I/CharacterListBoxWithContainer.jpg" /&gt;With the above item container style, our ListBox now renders as shown here.&lt;/p&gt;
&lt;p&gt;Notice that in this style we have added a setter to explicitly set the Width of the ListBoxItem to 75 device independent pixels.  Previously, the item containers were sizing to their content, which meant that each item would render as big as necessary to display the characters name and the image of that character at its natural size (the size stored in the image file). &lt;/p&gt;
&lt;p&gt;The container style is a great place to apply sizing because it allows us to provide a consistent size for all items in the ListBox.  We could certainly hard code this size into the Character data template, but keep in mind that we may be using the same data template in other places within the application.  By putting a Width setter in the container style, rather than explicitly setting the width in the data template, we keep the data template dynamic.&lt;/p&gt;
&lt;p&gt;So now we have some consistent chrome and it is nicely defined in the container's template rather than in the item's data template.  Unfortunately, there is a big problem with this template.  When I snapped this image of the ListBox, the selected item was Homer.  Of course, you will have to take my word for it, since there is clearly nothing in the visual appearance that can be used to verify I'm telling the truth. &lt;/p&gt;
&lt;p&gt;Recall that the default ListBoxItem template is what gave us visual cues for things like selection state.  Since we have defined our own ListBoxItem template, we need to do likewise in our template.  So let's just add the following Triggers to our control template:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;  &lt;&lt;font color="#993300"&gt;ControlTemplate.Triggers&lt;/font&gt;&gt;&lt;br /&gt;
    &lt;&lt;font color="#993300"&gt;Trigger&lt;/font&gt; &lt;font color="#ff0000"&gt;Property&lt;/font&gt;="Selector.IsSelected" &lt;font color="#ff0000"&gt;Value&lt;/font&gt;="True"&gt;&lt;br /&gt;
      &lt;&lt;font color="#993300"&gt;Setter&lt;/font&gt; &lt;font color="#ff0000"&gt;TargetName&lt;/font&gt;="BackgroundRect" &lt;font color="#ff0000"&gt;Property&lt;/font&gt;="Opacity" &lt;font color="#ff0000"&gt;Value&lt;/font&gt;="1" /&gt;&lt;br /&gt;
      &lt;&lt;font color="#993300"&gt;Setter&lt;/font&gt; &lt;font color="#ff0000"&gt;TargetName&lt;/font&gt;="ContentHost" &lt;font color="#ff0000"&gt;Property&lt;/font&gt;="BitmapEffect"&gt;&lt;br /&gt;
        &lt;&lt;font color="#993300"&gt;Setter.Value&lt;/font&gt;&gt;&lt;br /&gt;
          &lt;&lt;font color="#993300"&gt;OuterGlowBitmapEffect&lt;/font&gt; &lt;font color="#ff0000"&gt;GlowColor&lt;/font&gt;="White" &lt;font color="#ff0000"&gt;GlowSize&lt;/font&gt;="9" /&gt;&lt;br /&gt;
        &lt;/&lt;font color="#993300"&gt;Setter.&lt;/font&gt;&lt;font color="#993300"&gt;Value&lt;/font&gt;&gt;&lt;br /&gt;
      &lt;/&lt;font color="#993300"&gt;Setter&lt;/font&gt;&gt;&lt;br /&gt;
      &lt;&lt;font color="#993300"&gt;Setter&lt;/font&gt; &lt;font color="#ff0000"&gt;TargetName&lt;/font&gt;="BackgroundRect" &lt;font color="#ff0000"&gt;Property&lt;/font&gt;="Opacity" &lt;font color="#ff0000"&gt;Value&lt;/font&gt;="1" /&gt;&lt;br /&gt;
    &lt;/&lt;font color="#993300"&gt;Trigger&lt;/font&gt;&gt;&lt;br /&gt;
  &lt;/&lt;font color="#993300"&gt;ControlTemplate.Triggers&lt;/font&gt;&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;img height="370" alt="" hspace="10" src="http://www.drwpf.com/Blog/Portals/0/Images/ItemsControl/I/CharacterListBoxWithContainerAndSelection.jpg" width="106" align="right" /&gt;Now when we select Homer, the chrome around him darkens and he glows like an angel (or maybe like he's radioactive, which is actually more appropriate given his line of work).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Container's Data Context is the Item&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In &lt;em&gt;&lt;a target="_blank" href="http://drwpf.com/blog/Home/tabid/36/EntryID/24/Default.aspx"&gt;'D' is for DataTemplate&lt;/a&gt;&lt;/em&gt;, we learned that the data context for the root element of the data template is actually the data item that the template represents.  And since the DataContext is inherited through the element tree, each child element in the template also has this same data context.  This makes establishing bindings on elements in the template very easy.  For example, in our Character template, the Text property of the TextBlock is bound to the character's name by simply doing this:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;  &lt;&lt;font color="#993300"&gt;TextBlock &lt;/font&gt;&lt;font color="#ff0000"&gt;Text&lt;/font&gt;="{&lt;font color="#993300"&gt;Binding&lt;/font&gt; &lt;font color="#ff0000"&gt;First&lt;/font&gt;}" /&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Well, now we can explain how this actually works.  When the item container is generated, the framework sets its data context to the item that the container contains.  It then inflates the data template as the content of the container.  The elements in the container then naturally inherit their data contexts from the container.&lt;/p&gt;
&lt;p&gt;Armed with this knowledge that the DataContext of the item container is the item it contains, we might want to add a data trigger to our style to show the female characters with a pink background color.  We do live in a stereotyped world, after all!  The following trigger should work nicely:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;  &lt;&lt;font color="#993300"&gt;Style.Triggers&lt;/font&gt;&gt;&lt;br /&gt;
    &lt;&lt;font color="#993300"&gt;DataTrigger&lt;/font&gt; &lt;font color="#ff0000"&gt;Binding&lt;/font&gt;="{&lt;font color="#993300"&gt;Binding&lt;/font&gt; Gender} " &lt;font color="#ff0000"&gt;Value&lt;/font&gt;="Female"&gt;&lt;br /&gt;
      &lt;&lt;font color="#993300"&gt;Setter&lt;/font&gt; &lt;font color="#ff0000"&gt;Property&lt;/font&gt;="Background" &lt;font color="#ff0000"&gt;Value&lt;/font&gt;="#FFF339CB" /&gt;&lt;br /&gt;
    &lt;/&lt;font color="#993300"&gt;DataTrigger&lt;/font&gt;&gt;&lt;br /&gt;
  &lt;/&lt;font color="#993300"&gt;Style.Triggers&lt;/font&gt;&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Custom Placement of Items within an ItemsControl&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Now let's make just one more change to this sample.  It is actually pretty common to add extra metadata to the view model of a WPF application to help position and visualize data.  Suppose we modify the Character item in our &lt;a href="http://blogs.msdn.com/johngossman/archive/2005/10/08/478683.aspx" target="_blank"&gt;view model&lt;/a&gt; to allow each character to expose its own notion of where it should be positioned in &lt;em&gt;x-y&lt;/em&gt; space.  To do this, we will add the following Location property to the Character class:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;  &lt;font color="#0000ff"&gt;private&lt;/font&gt; &lt;font color="#339966"&gt;Point&lt;/font&gt; _location = &lt;font color="#0000ff"&gt;new&lt;/font&gt; &lt;font color="#339966"&gt;Point&lt;/font&gt;();&lt;br /&gt;
  &lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#339966"&gt;Point&lt;/font&gt; Location&lt;br /&gt;
  {&lt;br /&gt;
      &lt;font color="#0000ff"&gt;get&lt;/font&gt; { &lt;font color="#0000ff"&gt;return&lt;/font&gt; _location; }&lt;br /&gt;
      &lt;font color="#0000ff"&gt;set&lt;/font&gt;&lt;br /&gt;
      {&lt;br /&gt;
          _location = &lt;font color="#0000ff"&gt;value&lt;/font&gt;;&lt;br /&gt;
          RaisePropertyChanged ("&lt;font color="#993300"&gt;Location&lt;/font&gt;");&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Similarly, we'll modify our data collection to set the position of the characters:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;  &lt;&lt;font color="#993300"&gt;src:CharacterCollection&lt;/font&gt; &lt;font color="#ff0000"&gt;x:Key&lt;/font&gt;="Characters"&gt;&lt;br /&gt;
    &lt;&lt;font color="#993300"&gt;src:Character&lt;/font&gt; &lt;font color="#ff0000"&gt;First&lt;/font&gt;="Bart" &lt;font color="#ff0000"&gt;Last&lt;/font&gt;="Simpson" &lt;font color="#ff0000"&gt;Age&lt;/font&gt;="10" &lt;br /&gt;
        &lt;font color="#ff0000"&gt;Gender&lt;/font&gt;="Male" &lt;font color="#ff0000"&gt;Image&lt;/font&gt;="images/bart.png" &lt;strong&gt;&lt;font color="#ff0000"&gt;Location&lt;/font&gt;="25,150" &lt;/strong&gt;/&gt;&lt;br /&gt;
    &lt;&lt;font color="#993300"&gt;src:Character&lt;/font&gt; &lt;font color="#ff0000"&gt;First&lt;/font&gt;="Homer" &lt;font color="#ff0000"&gt;Last&lt;/font&gt;="Simpson" &lt;font color="#ff0000"&gt;Age&lt;/font&gt;="38" &lt;br /&gt;
        &lt;font color="#ff0000"&gt;Gender&lt;/font&gt;="Male" &lt;font color="#ff0000"&gt;Image&lt;/font&gt;="images/homer.png" &lt;font color="#ff0000"&gt;&lt;strong&gt;Location&lt;font color="#000000"&gt;="75,0" &lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;/&gt;&lt;br /&gt;
    &lt;&lt;font color="#993300"&gt;src:Character&lt;/font&gt; &lt;font color="#ff0000"&gt;First&lt;/font&gt;="Lisa" &lt;font color="#ff0000"&gt;Last&lt;/font&gt;="Bouvier" &lt;font color="#ff0000"&gt;Age&lt;/font&gt;="8" &lt;br /&gt;
        &lt;font color="#ff0000"&gt;Gender&lt;/font&gt;="Female" &lt;font color="#ff0000"&gt;Image&lt;/font&gt;="images/lisa.png" &lt;strong&gt;&lt;font color="#ff0000"&gt;Location&lt;/font&gt;="125,150" &lt;/strong&gt;/&gt;&lt;br /&gt;
    &lt;&lt;font color="#993300"&gt;src:Character&lt;/font&gt; &lt;font color="#ff0000"&gt;First&lt;/font&gt;="Maggie" &lt;font color="#ff0000"&gt;Last&lt;/font&gt;="Simpson" &lt;font color="#ff0000"&gt;Age&lt;/font&gt;="0" &lt;br /&gt;
        &lt;font color="#ff0000"&gt;Gender&lt;/font&gt;="Female" &lt;font color="#ff0000"&gt;Image&lt;/font&gt;="images/maggie.png" &lt;strong&gt;&lt;font color="#ff0000"&gt;Location&lt;/font&gt;="225,150" &lt;/strong&gt;/&gt;&lt;br /&gt;
    &lt;&lt;font color="#993300"&gt;src:Character&lt;/font&gt; &lt;font color="#ff0000"&gt;First&lt;/font&gt;="Marge" &lt;font color="#ff0000"&gt;Last&lt;/font&gt;="Bouvier" &lt;font color="#ff0000"&gt;Age&lt;/font&gt;="38" &lt;br /&gt;
        &lt;font color="#ff0000"&gt;Gender&lt;/font&gt;="Female" &lt;font color="#ff0000"&gt;Image&lt;/font&gt;="images/marge.png" &lt;strong&gt;&lt;font color="#ff0000"&gt;Location&lt;/font&gt;="175,0" &lt;/strong&gt;/&gt;&lt;br /&gt;
  &lt;/&lt;font color="#993300"&gt;src:CharacterCollection&lt;/font&gt;&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;The default items host for a ListBox is a panel called VirtualizingStackPanel.  This works great when you want a traditionally stacked layout with the added benefits of UI virtualization, but what if you want a custom layout?  In &lt;em&gt;&lt;a href="http://drwpf.com/blog/Home/tabid/36/EntryID/28/Default.aspx" target="_blank"&gt;'P' is for Panel&lt;/a&gt;&lt;/em&gt;, we learned that we can actually choose &lt;em&gt;any&lt;/em&gt; panel to serve as the items host for our data items.&lt;/p&gt;
&lt;p&gt;Since our Character item now provides its own &lt;em&gt;(x, y)&lt;/em&gt; location, a Canvas is the logical choice for an items panel:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;  &lt;&lt;font color="#993300"&gt;ListBox &lt;/font&gt;&lt;font color="#ff0000"&gt;ItemsSource&lt;/font&gt;="{&lt;font color="#993300"&gt;Binding&lt;/font&gt; &lt;font color="#ff0000"&gt;Source&lt;/font&gt;={&lt;font color="#993300"&gt;StaticResource&lt;/font&gt; &lt;font color="#ff0000"&gt;Characters&lt;/font&gt;}}" &lt;br /&gt;
      &lt;font color="#ff0000"&gt;ItemContainerStyle&lt;/font&gt;="{&lt;font color="#993300"&gt;StaticResource&lt;/font&gt; &lt;font color="#ff0000"&gt;CharacterContainerStyle&lt;/font&gt;}"&gt;&lt;br /&gt;
    &lt;&lt;font color="#993300"&gt;ListBox.ItemsPanel&lt;/font&gt;&gt;&lt;br /&gt;
&lt;strong&gt;      &lt;&lt;font color="#993300"&gt;ItemsPanelTemplate&lt;/font&gt;&gt;&lt;br /&gt;
        &lt;&lt;font color="#993300"&gt;Canvas&lt;/font&gt; /&gt;&lt;br /&gt;
      &lt;/&lt;font color="#993300"&gt;ItemsPanelTemplate&lt;/font&gt;&gt;&lt;br /&gt;
&lt;/strong&gt;    &lt;/&lt;font color="#993300"&gt;ListBox.ItemsPanel&lt;/font&gt;&gt;&lt;br /&gt;
  &lt;/&lt;font color="#993300"&gt;ListBox&lt;/font&gt;&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;img height="176" alt="" hspace="10" src="http://www.drwpf.com/Blog/Portals/0/Images/ItemsControl/I/CharacterListBoxWithCanvasAndLocationUnbound.jpg" width="125" align="right" /&gt;Hmmm...  Now when we run this sample, we see just Marge.  Oops!  There are still 5 items, but they are all positioned in the same &lt;em&gt;(0, 0)&lt;/em&gt; location, so we only see the topmost item.  We really want each item to be positioned according to its Location property.  That is, we want the Canvas.Left and Canvas.Top properties of each item to be bound to Location.X and Location.Y on each character.&lt;/p&gt;
&lt;p&gt;This is where knowing that each item is wrapped in a ListBoxItem container comes in very handy!  Since the items panel actually hosts these containers, we just need to modify the container style to bind the Canvas attached properties to the Location properties on the contained item.  This can be done by adding the following setters to our style.&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;  &lt;&lt;font color="#993300"&gt;Setter &lt;/font&gt;&lt;font color="#ff0000"&gt;Property&lt;/font&gt;="Canvas.Left" &lt;font color="#ff0000"&gt;Value&lt;/font&gt;="{&lt;font color="#993300"&gt;Binding&lt;/font&gt; &lt;font color="#ff0000"&gt;Location.X&lt;/font&gt;}" /&gt;&lt;br /&gt;
  &lt;&lt;font color="#993300"&gt;Setter&lt;/font&gt; &lt;font color="#ff0000"&gt;Property&lt;/font&gt;="Canvas.Top" &lt;font color="#ff0000"&gt;Value&lt;/font&gt;="{&lt;font color="#993300"&gt;Binding&lt;/font&gt; &lt;font color="#ff0000"&gt;Location.Y&lt;/font&gt;} " /&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Voîla!  Now when we run the code, we see the expected result (now with Lisa selected):&lt;/p&gt;
&lt;p&gt; &lt;img height="300" width="340" align="absMiddle" alt="" src="http://www.drwpf.com/Blog/Portals/0/Images/ItemsControl/I/CharacterListBoxWithCanvasAndLocationBound.jpg" /&gt; &lt;/p&gt;
&lt;p&gt;Okay, you might have noticed that I modified a couple of other style properties to provide consistent heights and vertical alignment for the children.  The complete sample can be &lt;a href="http://www.drwpf.com/Blog/Portals/0/Samples/HomersContainer.zip"&gt;downloaded here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Common Problems (Revisited)&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Remember the common problems we talked about toward the beginning of this article?&lt;/p&gt;
&lt;ol&gt;
    &lt;li&gt;&lt;em&gt;Custom Child Placement&lt;/em&gt;&lt;/li&gt;
    &lt;li&gt;&lt;em&gt;Mappings between Items and Visuals&lt;/em&gt;&lt;/li&gt;
    &lt;li&gt;&lt;em&gt;UI Virtualization&lt;/em&gt;&lt;/li&gt;
    &lt;li&gt;&lt;em&gt;Consistent Item Chrome&lt;/em&gt;&lt;/li&gt;
    &lt;li&gt;&lt;em&gt;Visible Selection State&lt;/em&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We have actually tackled items 1, 4, and 5 already.  Our items have custom placement due to bindings on the Canvas attached properties on the item container.  We have also defined a custom template within the item container style to give the items a consistent chrome.  And finally, we added triggers to that template to show the selected item.&lt;/p&gt;
&lt;p&gt;In the next episode, we will talk about how the item container &lt;em&gt;generator&lt;/em&gt; can be used to tackle the remaining issues.  (No, we will not implement UI virtualization in that article... that will be a separate post later in the series.  But we will talk about how the generator enables this virtualization.)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Default Items Hosts and Containers&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For your convenience, here is a list of the native ItemsControl classes in WPF, along with their default items hosts and item container types:&lt;/p&gt;
&lt;p&gt;
&lt;table cellspacing="2" cellpadding="4" width="600" border="1"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;&lt;font size="2"&gt;&lt;strong&gt;ItemsControl Type&lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;
            &lt;td&gt;&lt;font size="2"&gt;&lt;strong&gt;Default Items Host&lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;
            &lt;td&gt;&lt;font size="2"&gt;&lt;strong&gt;Default Item Container&lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;&lt;font size="2"&gt; ComboBox&lt;/font&gt;&lt;/td&gt;
            &lt;td&gt;&lt;font size="2"&gt; StackPanel&lt;/font&gt;&lt;/td&gt;
            &lt;td&gt;&lt;font size="2"&gt; ComboBoxItem&lt;/font&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;&lt;font size="2"&gt; ContextMenu&lt;/font&gt;&lt;/td&gt;
            &lt;td&gt;&lt;font size="2"&gt; StackPanel&lt;/font&gt;&lt;/td&gt;
            &lt;td&gt;&lt;font size="2"&gt; MenuItem&lt;/font&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;&lt;font size="2"&gt; HeaderedItemsControl&lt;/font&gt;&lt;/td&gt;
            &lt;td&gt;&lt;font size="2"&gt; StackPanel&lt;/font&gt;&lt;/td&gt;
            &lt;td&gt;&lt;font size="2"&gt; ContentPresenter&lt;/font&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;&lt;font size="2"&gt; ItemsControl&lt;/font&gt;&lt;/td&gt;
            &lt;td&gt;&lt;font size="2"&gt; StackPanel&lt;/font&gt;&lt;/td&gt;
            &lt;td&gt;&lt;font size="2"&gt; ContentPresenter or &lt;em&gt;any&lt;/em&gt; UIElement*&lt;/font&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;&lt;font size="2"&gt; ListBox&lt;/font&gt;&lt;/td&gt;
            &lt;td&gt;&lt;font size="2"&gt; VirtualizingStackPanel&lt;/font&gt;&lt;/td&gt;
            &lt;td&gt;&lt;font size="2"&gt; ListBoxItem&lt;/font&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;&lt;font size="2"&gt; ListView&lt;/font&gt;&lt;/td&gt;
            &lt;td&gt;&lt;font size="2"&gt; VirtualizingStackPanel&lt;/font&gt;&lt;/td&gt;
            &lt;td&gt;&lt;font size="2"&gt; ListViewItem&lt;/font&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;&lt;font size="2"&gt; Menu&lt;/font&gt;&lt;/td&gt;
            &lt;td&gt;&lt;font size="2"&gt; WrapPanel&lt;/font&gt;&lt;/td&gt;
            &lt;td&gt;&lt;font size="2"&gt; MenuItem&lt;/font&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;&lt;font size="2"&gt; MenuItem&lt;/font&gt;&lt;/td&gt;
            &lt;td&gt;&lt;font size="2"&gt; StackPanel&lt;/font&gt;&lt;/td&gt;
            &lt;td&gt;&lt;font size="2"&gt; MenuItem&lt;/font&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;&lt;font size="2"&gt; StatusBar&lt;/font&gt;&lt;/td&gt;
            &lt;td&gt;&lt;font size="2"&gt; DockPanel&lt;/font&gt;&lt;/td&gt;
            &lt;td&gt;&lt;font size="2"&gt; StatusBarItem&lt;/font&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;&lt;font size="2"&gt; TabControl&lt;/font&gt;&lt;/td&gt;
            &lt;td&gt;&lt;font size="2"&gt; TabPanel&lt;/font&gt;&lt;/td&gt;
            &lt;td&gt;&lt;font size="2"&gt; TabItem&lt;/font&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;&lt;font size="2"&gt; ToolBar**&lt;/font&gt;&lt;/td&gt;
            &lt;td&gt;&lt;font size="2"&gt; &lt;em&gt;not used&lt;/em&gt;&lt;/font&gt;&lt;/td&gt;
            &lt;td&gt;&lt;font size="2"&gt; &lt;em&gt;none&lt;/em&gt;&lt;/font&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;&lt;font size="2"&gt; TreeView&lt;/font&gt;&lt;/td&gt;
            &lt;td&gt;&lt;font size="2"&gt; StackPanel&lt;/font&gt;&lt;/td&gt;
            &lt;td&gt;&lt;font size="2"&gt; TreeViewItem&lt;/font&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;&lt;font size="2"&gt; TreeViewItem&lt;/font&gt;&lt;/td&gt;
            &lt;td&gt;&lt;font size="2"&gt; StackPanel&lt;/font&gt;&lt;/td&gt;
            &lt;td&gt;&lt;font size="2"&gt; TreeViewItem&lt;/font&gt;&lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;/p&gt;
&lt;p&gt;&lt;font size="1"&gt;* If a UIElement is added to the Items collection of an explicit ItemsControl instance (as opposed to an instance of a derived class like ListBox), it will become a direct child of the items panel. If a non-UIElement is added, it will be wrapped within a ContentPresenter.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font size="1"&gt;** Note that I've included the ToolBar control in this list because technically, it &lt;em&gt;is&lt;/em&gt; an ItemsControl. However, it should be noted that it has certain hardcoded behaviors that diverge from the other ItemsControl classes. It does not wrap its items in an item container and it is hard coded to layout its items in a special ToolBarPanel class. Setting the ItemsPanel property on a ToolBar will not change this behavior. The control template for a ToolBar must include a ToolBarPanel within its visual tree. If the panel is not present, the framework will throw an exception. (Bad form, framework!  Shame on you!)&lt;/font&gt;&lt;/p&gt;
&lt;/div&gt;</description>
      <link>http://www.drwpf.com/blog/Home/tabid/36/EntryID/32/Default.aspx</link>
      <author>ask@drwpf.com</author>
      <comments>http://www.drwpf.com/blog/Home/tabid/36/EntryID/32/Default.aspx#Comments</comments>
      <guid isPermaLink="true">http://www.drwpf.com/Blog/Default.aspx?tabid=36&amp;EntryID=32</guid>
      <pubDate>Wed, 26 Mar 2008 03:10:00 GMT</pubDate>
      <slash:comments>3</slash:comments>
      <trackback:ping>http://www.drwpf.com/Blog/DesktopModules/Blog/Trackback.aspx?id=32</trackback:ping>
    </item>
    <item>
      <title>The BIG Problem with Silverlight's Control Templating Model</title>
      <description>&lt;div class="jbr"&gt;
&lt;p&gt;Robby has just fired &lt;a target="_blank" href="http://notstatic.com/archives/141"&gt;this shot&lt;/a&gt; in&lt;em&gt; The Great Templating &lt;strike&gt;War&lt;/strike&gt; Debate of 2008&lt;/em&gt;.  I don't often disagree with Robby on platform issues, &lt;em&gt;but&lt;/em&gt;...  I am now compelled to reply with an opposing viewpoint on this issue.&lt;/p&gt;
&lt;p&gt;In my opinion, there are several problems with the templating model in Silverlight 2.0. I'm not going to focus on some of the more obvious ones (like the fact that WPF and Silverlight are using completely different models) because I think &lt;a target="_blank" href="http://devlicio.us/blogs/rob_eisenberg/archive/2008/03/13/there-s-some-darkness-in-your-silver-light.aspx"&gt;they have been covered quite well by others&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Instead, I'm going to focus on what I consider to be the &lt;em&gt;&lt;strong&gt;BIG&lt;/strong&gt;&lt;/em&gt; problem... Silverlight's templating model breaks the developer/designer workflow.&lt;/p&gt;
&lt;p&gt;The biggest value proposition for both Silverlight and WPF is &lt;a target="_blank" href="http://windowsclient.net/wpf/white-papers/thenewiteration.aspx"&gt;the workflow that they enable between developers and designers&lt;/a&gt;. As a left-brained developer, you definitely do &lt;em&gt;not&lt;/em&gt; want me involved in an application's UI design. (Trust me on that!)  The good news is that in WPF, you don't need to worry.  I can focus on the application's functionality and the design work can be handled by a truly capable UX designer.&lt;/p&gt;
&lt;p&gt;You may reasonably be wondering, "What does application development and design have to do with control development and design?"  Control development in WPF is very much just a &lt;em&gt;microcosm&lt;/em&gt; of application development in WPF. I tend to write my application objects (user controls, windows, pages, the application itself) in the exact same manner as custom controls. This enables designers to create a UI that simply reflects the state of the application objects. If you think about it, this is a little &lt;a target="_blank" href="http://blogs.msdn.com/johngossman/archive/2005/10/08/478683.aspx"&gt;M-V-VM pattern&lt;/a&gt; built right into WPF's control and application models.&lt;/p&gt;
&lt;p&gt;To fully appreciate the workflow that this model enables for both designers and developers, you just need to experience it once... You turn over a really drab, form-looking UI to your designer and watch what they do with it... Wow!  It's an amazing feeling just to think that you had some part in the creation of such a thing of beauty! &lt;/p&gt;
&lt;p&gt;Perhaps you think this is just hyperbole.  Well, I know &lt;a target="_blank" href="http://joshsmithonwpf.wordpress.com/2008/02/10/podder-v2-beta/"&gt;I'm not the only developer who feels this way&lt;/a&gt;!  And you, too, can feel this exuberance enabled by the developer/designer workflow.  Simply &lt;a target="_blank" href="http://joshsmithonwpf.wordpress.com/2008/03/05/podder-v2-has-been-released/"&gt;download Podder&lt;/a&gt; and toggle between Josh's skin and Grant's skin!  (Sorry Josh. &lt;img alt="" src="http://www.drwpf.com/Blog/Providers/HtmlEditorProviders/Fck/FCKeditor/editor/images/smiley/msn/tounge_smile.gif" /&gt;)&lt;/p&gt;
&lt;p&gt;Back to the issue at hand...  I happen to be very good at analyzing a control to determine the states that it will need to support. I'm also quite adept at implementing the functionality of a control and making sure that the functionality is exposed via the necessary methods and commands. Moreover, I'm very &lt;em&gt;passionate&lt;/em&gt; about this type of control development.  I think the designers I work with will validate that I always strive to provide them with all the hooks they could possibly want when it comes to styling and templating my controls.&lt;/p&gt;
&lt;p&gt;The term that WPF uses for this control model is &lt;em&gt;"lookless"&lt;/em&gt;. It basically means that the code for a control (or application) is written in a manner that makes no assumptions about the presentation layer. The control simply exposes state information via properties and events along with execution entry points via methods and commands.&lt;/p&gt;
&lt;p&gt;Silverlight's templating model completely breaks this lookless control model. It requires that the developer meddle in areas where they don't belong (such as looking for and executing storyboards in the UI layer). Furthermore, it takes away the ability of designers to think outside the box when they define UX interactions because they must conform to whatever hooks have been provided by (a.k.a., hardcoded into) the control.  Or worse, it encourages designers to actually meddle in the control's code where &lt;em&gt;they &lt;/em&gt;don't belong.&lt;/p&gt;
&lt;p&gt;Without the lookless model, you cannot achieve what Nathan describes as &lt;a target="_blank" href="http://designerslove.net/?p=168"&gt;the "holy grail" for developer/designer workflow&lt;/a&gt;. I completely agree. (Nathan's blog, by the way, is another excellent resource for anyone who would like a designer's view of the WPF and Silverlight platforms.)&lt;/p&gt;
&lt;p&gt;The process of creating great controls will always involve designers and developers working together to meet each other's needs. However, they each have their own strengths. As a developer, I DON'T WANT TO THINK ABOUT DESIGN!&lt;/p&gt;
&lt;p&gt;The reason I am so passionate about the WPF platform is that it frees me to do what I do best... write code. At the same time, it frees designers to do what they do best... create compelling user experiences by designing the look and feel of an application and its controls. This is all enabled by WPF's styling and templating model.&lt;/p&gt;
&lt;p&gt;The arguments I keep hearing for the decisions being made with respect to Silverlight's templating model all seem to revolve around &lt;em&gt;toolability&lt;/em&gt;. Namely, applications like Blend need a more intuitive way to support styling and templating.&lt;/p&gt;
&lt;p&gt;My stance is that the architecture of the platform &lt;em&gt;should not &lt;/em&gt;be driven by the needs of the tools. The platform should definitely accomodate those needs, but this can be done through helper classes. The platform's architecture needs to be driven by decisions that best enable the next generation of killer software experiences. I fully believe these experiences can best be enabled using a truly lookless model. I also fully believe that this model enables the most ideal workflow between developers and designers.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;So how can Silverlight fix this?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Simple. Give us property triggers, data triggers, and event triggers in both styles and templates.&lt;/p&gt;
&lt;/div&gt;</description>
      <link>http://www.drwpf.com/blog/Home/tabid/36/EntryID/31/Default.aspx</link>
      <author>ask@drwpf.com</author>
      <comments>http://www.drwpf.com/blog/Home/tabid/36/EntryID/31/Default.aspx#Comments</comments>
      <guid isPermaLink="true">http://www.drwpf.com/Blog/Default.aspx?tabid=36&amp;EntryID=31</guid>
      <pubDate>Mon, 24 Mar 2008 10:48:00 GMT</pubDate>
      <slash:comments>12</slash:comments>
      <trackback:ping>http://www.drwpf.com/Blog/DesktopModules/Blog/Trackback.aspx?id=31</trackback:ping>
    </item>
    <item>
      <title>Mix08:  The Good, the Bad, and the Ugly</title>
      <description>&lt;p class="jbr"&gt;Having recently returned from Mix 08 (yes, I stayed a few days beyond the conference), I thought I should follow up with my thoughts on this year's event:&lt;/p&gt;
&lt;p class="jbr"&gt;&lt;strong&gt;The Good&lt;/strong&gt;&lt;/p&gt;
&lt;div&gt;
&lt;ul&gt;
    &lt;li&gt;&lt;a target="_blank" href="http://silverlight.net/GetStarted/#betajump"&gt;Silverlight 2 Beta&lt;/a&gt;  (We've come a long way since JavaScript!)&lt;/li&gt;
    &lt;li&gt;&lt;a target="_blank" href="http://sessions.visitmix.com/?selectedSearch=KYN0802"&gt;The Balmer / Kawasaki Interview&lt;/a&gt;  (Very entertaining on both sides)&lt;/li&gt;
    &lt;li&gt;&lt;a target="_blank" href="http://memorabilia.hardrock.com/"&gt;Deep Zoom&lt;/a&gt;  (No, it's not one of those pay-per-view shows that they are promoting for $14.95 in your hotel room... you know... the ones where they feel it is important to stress that movie titles do &lt;em&gt;not&lt;/em&gt; appear on your hotel bill.)&lt;/li&gt;
    &lt;li&gt;Meeting some community cohorts (like &lt;a target="_blank" href="http://karlshifflett.wordpress.com/"&gt;Karl&lt;/a&gt; and &lt;a target="_blank" href="http://jmorrill.hjtcentral.com/"&gt;Jeremiah&lt;/a&gt;)&lt;/li&gt;
    &lt;li&gt;Shaving off that &lt;a target="_blank" href="http://designerslove.net/?p=167"&gt;ridiculous mustache&lt;/a&gt; (huh? a woman with a mustache?)&lt;/li&gt;
    &lt;li&gt;Open Bar for the Mix party at &lt;a target="_blank" href="http://www.taorestaurant.com/lasvegas/"&gt;Tau&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;Me (after my 2nd Sapphire and Tonic at Tau)&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;The Mediocre&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;The binding support in Silverlight 2 Beta  (&lt;font color="#993300"&gt;Alright, I originally had this under &lt;em&gt;The Bad&lt;/em&gt;, but it was brought to my attention (thanks Nick) that the issues around data context inheritance have been fixed in the final Mix release.  I updated my code and sure enough, it works!  Nice!!  Still...  &lt;/font&gt;I shouldn't need to write code to establish the source of a binding.  Once there is support for ElementName and RelativeSource, I will move this up to &lt;em&gt;The Good&lt;/em&gt; section.  And if you give me support for data validation and support for binding to XML data via XPath expressions (along with an XmlDataSource, of course), then I'll create &lt;em&gt;The Excellent&lt;/em&gt; section just for Silverlight binding!)&lt;/li&gt;
&lt;/ul&gt;
&lt;p style="margin-right: 0px"&gt;&lt;strong&gt;The Bad&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;The price of just about anything within the confines of &lt;a target="_blank" href="http://www.venetian.com/"&gt;The Venetian&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;The air quality anywhere near a Vegas casino&lt;/li&gt;
    &lt;li&gt;The faux vinyl, burlapesque manpurse given to each attendee&lt;/li&gt;
    &lt;li&gt;The scenery at &lt;a target="_blank" href="http://www.taolasvegas.com/taobeach/"&gt;Tau Beach&lt;/a&gt; during the Mix party&lt;/li&gt;
    &lt;li&gt;Me (after my 5th Sapphire and Tonic at Tau)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;The Ugly&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;The templating support in Silverlight 2 Beta  (Have we just given up on the notion of a &lt;em&gt;lookless&lt;/em&gt; control model?)&lt;/li&gt;
    &lt;li&gt;The T-shirt in the aforementioned manpurse  (Come on guys... this is a designer-heavy event!  I recognize that last year's T-shirt design set a pretty high bar, but did you just give up and not even try this year?  Even setting the design aside, the souvenir quality alone is enough to automatically relegate it to the Good Will box.  For those unable to attend, I'm sure you'll be able to pick one up at your nearest second hand store under a sign that says &lt;em&gt;"7$ each or 2 for $10"&lt;/em&gt;.)&lt;/li&gt;
    &lt;li&gt;Me (after my 7th Sapphire and Tonic at Tau)&lt;/li&gt;
&lt;/ul&gt;</description>
      <link>http://www.drwpf.com/blog/Home/tabid/36/EntryID/30/Default.aspx</link>
      <author>ask@drwpf.com</author>
      <comments>http://www.drwpf.com/blog/Home/tabid/36/EntryID/30/Default.aspx#Comments</comments>
      <guid isPermaLink="true">http://www.drwpf.com/Blog/Default.aspx?tabid=36&amp;EntryID=30</guid>
      <pubDate>Wed, 12 Mar 2008 13:40:00 GMT</pubDate>
      <slash:comments>1</slash:comments>
      <trackback:ping>http://www.drwpf.com/Blog/DesktopModules/Blog/Trackback.aspx?id=30</trackback:ping>
    </item>
    <item>
      <title>up all mixed am I</title>
      <description>&lt;p&gt;&lt;a target="_blank" href="http://www.visitmix.com/"&gt;&lt;img height="216" alt="" hspace="3" width="223" align="right" vspace="3" border="0" src="http://www.drwpf.com/Blog/Portals/0/Images/Mix08.png" /&gt;&lt;/a&gt;As usual, it's been a crazy few weeks leading up to Mix, but I swear I can hear Phantom music everytime I step into an elevator, so we must be really close now!&lt;/p&gt;
&lt;p&gt;It should be another good conference this year and I hope to see a lot of you there.  You will be able to recognize me because I'll be the geeky looking one wearing jeans and a t-shirt.  In fact, I'd encourage you to approach anyone you see fitting this description and simply ask, &lt;em&gt;"Are you him/her?"&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Hopefully things will slow down a bit in March and I'll be able to increase my posts here and in the forums.  (Although I'm happy to say that 6 months after joining the MSDN forums, I've &lt;em&gt;finally&lt;/em&gt; acquired my &lt;a target="_blank" href="http://drwpf.com/blog/Home/tabid/36/EntryId/16/Default.aspx"&gt;4th star&lt;/a&gt;. Yes, it took much longer than anticipated...  but oh well...  when life gives you lemons, throw them at someone!)&lt;/p&gt;
&lt;p&gt;See you in Vegas!  (But will &lt;em&gt;you&lt;/em&gt; see &lt;em&gt;me&lt;/em&gt;?)&lt;/p&gt;</description>
      <link>http://www.drwpf.com/blog/Home/tabid/36/EntryID/29/Default.aspx</link>
      <author>ask@drwpf.com</author>
      <comments>http://www.drwpf.com/blog/Home/tabid/36/EntryID/29/Default.aspx#Comments</comments>
      <guid isPermaLink="true">http://www.drwpf.com/Blog/Default.aspx?tabid=36&amp;EntryID=29</guid>
      <pubDate>Fri, 29 Feb 2008 03:13:00 GMT</pubDate>
      <slash:comments>4</slash:comments>
      <trackback:ping>http://www.drwpf.com/Blog/DesktopModules/Blog/Trackback.aspx?id=29</trackback:ping>
    </item>
    <item>
      <title>ItemsControl:  'P' is for Panel</title>
      <description>&lt;div class="jbr"&gt;
&lt;p&gt;Let's continue our exploration of WPF through the medium of the ItemsControl class.  I know I promised to write &lt;em&gt;'G' is for Generator&lt;/em&gt; next, but after giving it more consideration, I've decided that it makes more sense to introduce the concept of an &lt;em&gt;"items panel"&lt;/em&gt; first.  This should give us more context when we finally do look at item containers and container generators.&lt;/p&gt;
&lt;p&gt;To support this diversion, I'm giving this "ItemsControl: A to Z" series a new subtitle of "(but not necessarily in &lt;em&gt;that&lt;/em&gt; order)".  &lt;img alt="" src="http://www.drwpf.comhttp://www.drwpf.com/Blog/Providers/HtmlEditorProviders/Fck/FCKeditor/editor/images/smiley/msn/wink_smile.gif" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How did we get here?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Let's just recap a couple of things before we get started...  In &lt;a target="_blank" href="http://drwpf.com/blog/Home/tabid/36/EntryID/18/Default.aspx"&gt;'C' is for Collection&lt;/a&gt;, we learned that an ItemsControl surfaces a collection of items in a very predictable way (namely, as a CollectionView).  Then in &lt;a target="_blank" href="http://drwpf.com/blog/Home/tabid/36/EntryID/24/Default.aspx"&gt;'D' is for DataTemplate&lt;/a&gt;, we learned that an item within the collection can be &lt;em&gt;any&lt;/em&gt; CLR object and the visual representation of the item is defined using a template of visual elements (called a DataTemplate).&lt;/p&gt;
&lt;p&gt;The next question that logically arises is, &lt;em&gt;"where do we put these visuals?"&lt;/em&gt;  More specifically, once the data template for an item has been inflated, where should its visuals be positioned?  To answer this question, we will now examine how &lt;em&gt;"layout"&lt;/em&gt; is handled for items within an ItemsControl.&lt;/p&gt;
&lt;p&gt;This particular episode begins by covering a few WPF concepts that are only indirectly related to the ItemsControl class.  Some of the material is of a more technical nature.  I have clearly marked these sections as "200 Level" material.  Feel free to skip over these sections if you are only interested in ItemsControl or if you just aren't in the mood to get your geek on.  &lt;img alt="" src="http://www.drwpf.comhttp://www.drwpf.com/Blog/Providers/HtmlEditorProviders/Fck/FCKeditor/editor/images/smiley/msn/wink_smile.gif" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is layout?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In WPF, the term &lt;em&gt;"layout"&lt;/em&gt; refers to the sizing and positioning of visual elements within the user interface. &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How does layout work?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In some cases, an element may know exactly what size it should be (because it's Width and Height properties have been explicitly set).  But very often, the size of an element is determined by its content.  To enable this &lt;em&gt;"size to content"&lt;/em&gt; feature, the WPF layout engine uses a 2-pass layout cycle to size and position visual elements:&lt;/p&gt;
&lt;p&gt;1. First a &lt;em&gt;measure pass&lt;/em&gt; is used to determine the desired size of each element.&lt;br /&gt;
2. Then an &lt;em&gt;arrange pass&lt;/em&gt; is used to explicitly size and position each element.&lt;/p&gt;
&lt;p&gt;The measure pass involves a recursive drilldown into the UI's visual tree to &lt;em&gt;measure&lt;/em&gt; each element.  During this pass, an element is basically asked what size it &lt;em&gt;wants&lt;/em&gt; to be.  To determine an answer to this question, the element turns around and measures each of its own children by asking them what size they want to be.  This recursion continues until all visual children in the subtree have been measured.  At this point, each element can answer this question regarding its &lt;em&gt;desired size&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The arrange pass involves another recursive drilldown into the visual tree to &lt;em&gt;arrange&lt;/em&gt; each element.  During this pass, the element is basically told what size it &lt;em&gt;gets&lt;/em&gt; to be.  In an ideal world, each element would &lt;em&gt;get&lt;/em&gt; to be the size that it &lt;em&gt;wants&lt;/em&gt; to be... but we all know life doesn't work that way!  The parent Panel has ultimate control over how much real estate each child gets and where that real estate is located.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Nitty Gritty of Measure (200 Level)&lt;br /&gt;
&lt;/strong&gt;  &lt;br /&gt;
During the measure pass, the question of &lt;em&gt;"What size do you want to be?"&lt;/em&gt; is posed to an element in the form of a method named MeasureOverride(), so named because you will override this method on a framework element whenever you wish to implement custom sizing logic for the element.  The size parameter received within MeasureOverride() represents a constraint for the element.  It is the parent's way of saying, &lt;em&gt;"You have this much space to work with... with that in mind, what size do you want to be?"&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Before answering this question, the element first asks its children what size they want to be by executing the Measure() method of each child.  When you call Measure() on a child, this indirectly executes the MeasureOverride() of that child... hence the recursion for the measure pass.&lt;/p&gt;
&lt;p&gt;After measuring its children, an element should be able to determine its desired size.  The value returned from MeasureOverride() becomes the value of the element's DesiredSize property.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;The Nitty Gritty of Arrange (200 Level)&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The sequence is very much the same during the arrange pass.  In this case, the &lt;em&gt;"Here's what size you get to be"&lt;/em&gt; message is delivered in the form of a method named ArrangeOverride().  You will override this method on a framework element anytime you need to provide custom positioning logic for child elements.  The size parameter received within ArrangeOverride() represents the real estate allotted for the element and its children. &lt;/p&gt;
&lt;p&gt;Note that a position is not supplied to an element within ArrangeOverride().  This is because an element does not get to decide where it will be positioned.  It can provide hints by setting some of its layout properties (HorizontalAlignment, VerticalAlignment, etc), but ultimately, the parent is responsible for respecting those properties and positioning the child.&lt;/p&gt;
&lt;p&gt;Although the element cannot control its own position, it &lt;em&gt;does&lt;/em&gt; get to control the position of each of its children, relative to itself.  This process is called &lt;em&gt;arranging&lt;/em&gt; the children and it happens when the element calls the Arrange() method on each child.  The Arrange() method takes a Rect as a parameter.  The position of the Rect represents the position of the child relative to the parent.  The size of the Rect represents the size of the child within the coordinate space of the parent. &lt;/p&gt;
&lt;p&gt;As with measuring, when you call Arrange() on a child, this indirectly executes the ArrangeOverride() of that child...  hence the recursion for the arrange pass. &lt;/p&gt;
&lt;p&gt;After arranging its children, an element should know its actual size.  The value returned from ArrangeOverride() becomes the value of the element's RenderSize property (and consequently, the values of the ActualWidth and ActualHeight properties).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Dispatcher Priority for Layout and Rendering (200 Level)&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The WPF threading model dictates that all code execution will occur within a succinct execution block.  We call these blocks &lt;em&gt;dispatcher operations&lt;/em&gt;.  Each dispatcher operation is queued for execution at a specific priority.  The queue is continuously processed by executing the highest priority operations first.  The available dispatcher priorities are given by the following enum:&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New"&gt;    &lt;font color="#0000ff"&gt;public enum&lt;/font&gt; DispatcherPriority&lt;br /&gt;
    {&lt;br /&gt;
        Invalid          = -1,&lt;br /&gt;
        Inactive         = 0,&lt;br /&gt;
        SystemIdle       = 1,&lt;br /&gt;
        ApplicationIdle  = 2,&lt;br /&gt;
        ContextIdle      = 3,&lt;br /&gt;
        Background       = 4,&lt;br /&gt;
        Input            = 5,&lt;br /&gt;
        Loaded           = 6,&lt;br /&gt;
        Render           = 7,&lt;br /&gt;
        DataBind         = 8,&lt;br /&gt;
        Normal           = 9,&lt;br /&gt;
        Send             = 10&lt;br /&gt;
    }&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Layout and rendering go hand in hand.  After the 2-pass layout cycle, the element tree is rendered.  As a result, you may hear the terms "render pass" and "layout pass" used interchangeably.  And indeed, the layout cycle and UI rendering actually occur within the same dispatcher operation. This operation typically occurs at Render priority.  The exception to this rule is that the initial layout cycle and rendering (when a Page or Window is first loaded) actually occur at Loaded priority. &lt;/p&gt;
&lt;p&gt;When a render operation executes, the visual tree is first walked to size any elements that need to be measured (IsMeasureValid == false).  The tree is then walked again to position any elements that need to be arranged (IsArrangeValid == false).  Finally, the updated scene is rendered. &lt;/p&gt;
&lt;p&gt;Keeping this in mind, if you ever change a property that affects layout and you want to delay some processing until after the layout has been updated, you can use BeginInvoke() to queue that additional work at Loaded priority.  This will typically cause it to execute within the next dispatcher operation &lt;em&gt;after&lt;/em&gt; the render pass.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;What is a panel?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Typically, when we talk about layout in WPF, we tend to focus on a particular category of elements called &lt;em&gt;panels&lt;/em&gt; (so named because they descend from an abstract Panel class).  You may recall from &lt;a target="_blank" href="http://drwpf.com/blog/Home/tabid/36/EntryID/24/Default.aspx"&gt;our earlier look at different WPF content models&lt;/a&gt; that a panel is a special element whose visual children are UIElements. &lt;/p&gt;
&lt;p&gt;The reason we tend to focus on panels so much when talking about layout is because &lt;em&gt;layout is really all a panel does&lt;/em&gt;.  Its sole purpose is to arrange its children at their proper sizes and positions. &lt;/p&gt;
&lt;p&gt;Specifically, a panel does three things:&lt;/p&gt;
&lt;ol&gt;
    &lt;li&gt;It maintains a collection of child elements (UIElements)&lt;/li&gt;
    &lt;li&gt;It sizes those elements&lt;/li&gt;
    &lt;li&gt;It positions those elements&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;It is important to note that layout in WPF is certainly not restricted to panels.  In fact, every framework element actively participates in the layout system.  More specifically, every framework element has a MeasureOverride() implementation to measure itself and its children and an ArrangeOverride() implementation to arrange itself and its children. &lt;/p&gt;
&lt;p&gt;Non-panel elements typically have no more than one child, and often they have no children at all.  The non-panel elements that &lt;em&gt;do&lt;/em&gt; have a child rarely do anything interesting with respect to the placement of that child.  Typically, the child is simply arranged within the entire rectangular area of the parent.&lt;/p&gt;
&lt;p&gt;Panels, on the other hand, almost always do something interesting with their children.  A Canvas, for example, positions its children precisely where they want to be according to the Canvas-related attached properties on each child (Canvas.Top, Canvas.Left, etc). &lt;/p&gt;
&lt;p&gt;A Grid positions its children within conceptual rows and columns according to the Grid-related attached properties on each child (Grid.Row, Grid.Column, Grid.RowSpan, Grid.ColumnSpan, etc). &lt;/p&gt;
&lt;p&gt;A StackPanel stacks its children vertically or horizontally, based on the Orientation property of the StackPanel.&lt;/p&gt;
&lt;p&gt;A WrapPanel stacks its children vertically or horizontally until it runs out of room and then it starts a new stack adjacent to or below the previous stack, again depending on the Orientation property of the WrapPanel.&lt;/p&gt;
&lt;p&gt;A lot more time could be spent explaining how the native panels implement their respective layout algorithms, but before we get too far off track...&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Why are we talking about panels in this ItemsControl series?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Oh yeah... because an ItemsControl is a control that manages a &lt;em&gt;collection&lt;/em&gt; of logical children (its "Items") and a panel is an element 