title: WPF Listbox w/ DataTemplate Binding Issue
body: <p>I'm trying to create a <code>ListBox</code> with a custom data template but the bindings seem to be wrong. When I try to add items to the list with the <em>PacketItem</em> template they are added, but the text in the three <em>HexItem</em> textboxes remains blank. When I set the item template of the listbox directly to the <em>HexItem</em> data template that works fine. It only has problems when nested in the <em>PacketItem</em> data template.</p>
  
  <p>I'm sure this is a simple fix, it seems like when the template is nested it doesn't know where to look for the text bindings. I'm thinking  binding to a relative source to get the data context from a parent would work, but that seems like the wrong way to do it.</p>
  
  <p>Revised XAML</p>
  
  <pre><code>&lt;UserControl x:Class="wpfPacketBox.ctlPacketBox"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
               xmlns:local="clr-namespace:wpfPacketBox"
               mc:Ignorable="d" 
               d:DesignHeight="300" d:DesignWidth="600"&gt;
      &lt;UserControl.Resources&gt;
          &lt;DataTemplate x:Key="PacketItem"&gt;
              &lt;Expander IsExpanded="True"
                        d:DesignWidth="600"&gt;
                  &lt;Expander.HeaderTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;StackPanel Orientation="Horizontal"&gt;
                              &lt;TextBlock Text="PacketNum" /&gt;
                              &lt;TextBlock Text="Source" /&gt;
                              &lt;TextBlock Text="Dest" /&gt;
                              &lt;TextBlock Text="PacketID (FF0A)" /&gt;
                          &lt;/StackPanel&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/Expander.HeaderTemplate&gt;
                  &lt;Expander.ContentTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;Grid&gt;
                              &lt;Grid.ColumnDefinitions&gt;
                                  &lt;ColumnDefinition Width="10*" /&gt;
                                  &lt;ColumnDefinition Width="65*" /&gt;
                                  &lt;ColumnDefinition Width="25*" /&gt;
                              &lt;/Grid.ColumnDefinitions&gt;
  
                              &lt;Grid Grid.Column="0"&gt;
                                  &lt;TextBox Text="{Binding Offset}"
                                           FontFamily="Courier New"
                                           Foreground="Blue"
                                           Background="#FFCCCCCC"
                                           TextWrapping="Wrap"
                                           BorderThickness="0" /&gt;
                              &lt;/Grid&gt;
  
                              &lt;Grid Grid.Column="1"&gt;
                                  &lt;TextBox Text="{Binding Hex}"
                                           FontFamily="Courier New"
                                           TextWrapping="Wrap"
                                           BorderThickness="0" /&gt;
                              &lt;/Grid&gt;
  
                              &lt;Grid Grid.Column="2"&gt;
                                  &lt;TextBox Text="{Binding Ascii}"
                                           FontFamily="Courier New"
                                           Background="#FFCCCCCC"
                                           TextWrapping="Wrap"
                                           BorderThickness="0" /&gt;
                              &lt;/Grid&gt;
                          &lt;/Grid&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/Expander.ContentTemplate&gt;
              &lt;/Expander&gt;
          &lt;/DataTemplate&gt;
      &lt;/UserControl.Resources&gt;
  
      &lt;Grid&gt;
          &lt;ListBox ItemsSource="{Binding Path=Packets}"
                   ItemTemplate="{StaticResource PacketItem}"
                   Grid.Row="1"
                   ScrollViewer.HorizontalScrollBarVisibility="Disabled" /&gt;
      &lt;/Grid&gt;
  
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>Code</p>
  
  <pre><code>using System;
  using System.Windows.Controls;
  using System.Collections.ObjectModel;
  
  namespace wpfTester.UserControls
  {
      public partial class test : UserControl
      {
          public ObservableCollection&lt;clsPacket&gt; Packets;
  
          public test()
          {
              InitializeComponent();
              Packets = new ObservableCollection&lt;clsPacket&gt;();
              PacketListBox.ItemsSource = Packets;
  
              for (int i = 0; i &lt;= 100; i++)
              {
                  Packets.Add(new clsPacket() { Offset = i, Hex = "00 11 22 33 44 55 66 77", Ascii = "ABCDEFGH" });
              }
          }
      }
  
      public class clsPacket
      {
          public long Offset { get; set; }
          public String Hex { get; set; }
          public String Ascii { get; set; }
      }
  }
  </code></pre>
  
  <p>Solution</p>
  
  <p>After some digging I found the solution <a href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/634fffa3-7e70-4cc3-9a1c-5405e4747e2a/" rel="nofollow">here</a>. The problem was the expander. I was setting the header/content templates of the expander. But never specified the Header/Content it self. Here's the modified expander tag that fixed my problem.</p>
  
  <pre><code>&lt;Expander IsExpanded="True" Header="{Binding}" Content="{Binding}"&gt;
  </code></pre>