title: Command Binding in ControlTemplate with Attached Property
body: <p>I'm trying to set up a system that would allow me to attach a string value to a ComboBoxItem and display it next to the ComboBoxItem's content, without having to explicitly nest a StackPanel inside every individual ComboBoxItem or use a custom control.</p>
  
  <p>So what I've done is created a DependencyProperty called "Header" and attach it to the ComboBoxItem, and I've overridden the ComboBoxItem template to contain the stack panel with a TextBlock that binds its Text to the attached Header property of the ComboBoxItem.</p>
  
  <p>The problem I am having is that the only text that ever appears in the TextBlock at runtime is whatever I set the default value of the dependency property to in metadata. Any changes to the attached property on the ComboBoxItems thereafter is not reflected in the TextBlock.</p>
  
  <p>Here is my DependencyProperty definition:</p>
  
  <pre><code>public class AttHeader : DependencyObject
  {
      public static readonly DependencyProperty HeaderProperty = DependencyProperty.RegisterAttached("Header", typeof(string), typeof(AttHeader));
  
      public static void SetHeader(DependencyObject d, string value)
      {
          d.SetValue(HeaderProperty, value);
      }
  
      public static string GetHeader(DependencyObject d)
      {
          return (string)d.GetValue(HeaderProperty);
      }
  }
  </code></pre>
  
  <p>Here is my style and template:</p>
  
  <pre><code>&lt;Style TargetType="ComboBoxItem"&gt;
      &lt;Setter Property="OverridesDefaultStyle" Value="True"/&gt;
      &lt;Setter Property="SnapsToDevicePixels" Value="True"/&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="ComboBoxItem"&gt;
                  &lt;StackPanel Orientation="Horizontal"&gt;
                      &lt;ContentPresenter /&gt;
                      &lt;TextBlock Name="HeaderHost" Text="{Binding Path=(local:AttHeader.Header), RelativeSource={RelativeSource Self}}" /&gt;
                  &lt;/StackPanel&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>And here is where I create a few ComboBoxItems:</p>
  
  <pre><code>&lt;ComboBox SelectedIndex="0"&gt;
      &lt;ComboBoxItem local:AttHeader.Header="Isometric"&gt;
          &lt;Image Source="../images/viewTypeIso.png" Stretch="None"/&gt;
      &lt;/ComboBoxItem&gt;
      &lt;ComboBoxItem local:AttHeader.Header="Top"&gt;
          &lt;Image Source="../images/ViewTypeTop.png" Stretch="None"/&gt;
      &lt;/ComboBoxItem&gt;
  &lt;/ComboBox&gt;
  </code></pre>
  
  <p>Even setting the value of the attached properties of these ComboBoxItems when they are created has no effect on the TextBlocks within.</p>
  
  <p>I'm assuming the binding is valid, because I can set a default value for the dependency property and each ComboBoxItem will always display that value next to its image.</p>
  
  <p>I've set a breakpoint in SetHeader and it is indeed called when those ComboBoxItems are constructed.</p>
  
  <p>Is there something I am doing wrong or is there some limitation to Binding that I'm unaware of?</p>