title: Binding property to dynamicaly created item of context menu
body: <p>I'm trying to make Context Menu, which will have items depending on some data in code.
  So, i have simple class, determining single item of menu</p>
  
  <pre><code>class ContextMenuItem
  {
      public string ItemHeader {get; set;}
      public Command ItemAction {get; set;
  }
  </code></pre>
  
  <p>where Command is implementation of ICommand, and stores action, which will be fired once this item is selected. Then i have class, serving as DataContext</p>
  
  <pre><code>class SomeClass
  {
      public List&lt;ContextMenuItem&gt; ContextMenuItems {get; set;}
      public string SomeProperty {get; set;}
      public string SomeAnotherProperty {get; set;}
  }
  </code></pre>
  
  <p>So, ContextMenuItems is list of actions I need in my context menu, which can be generated using different approaches.</p>
  
  <p>And I'm creating dynamic context menu, using this <a href="https://stackoverflow.com/questions/898852/specify-command-for-menuitem-in-a-datatemplate">approach</a>.</p>
  
  <pre><code>&lt;ContextMenu ItemsSource="{Binding ContextMenuItems}"&gt;
      &lt;ContextMenu.ItemContainerStyle&gt;
          &lt;Style TargetType="{x:Type MenuItem}"&gt;
              &lt;Setter Property="Command" Value="{Binding ItemAction}"/&gt;
              &lt;Setter Property="Header" Value="{Binding ItemHeader}"/&gt;
          &lt;/Style&gt;
      &lt;/ContextMenu.ItemContainerStyle&gt;
  &lt;/ContextMenu&gt;
  </code></pre>
  
  <p>So, i was suspecting this to work well. But, for some reason, binding works not the way I want it to.</p>
  
  <pre><code>&lt;Setter Property="Command" Value="{Binding ItemAction}"/&gt;
  &lt;Setter Property="Header" Value="{Binding ItemHeader}"/&gt;
  </code></pre>
  
  <p>Somehow, data context for this lines is not <code>ContextMenuItem</code>, but <code>SomeClass</code> itself. So, i can bind SomeProperty and SomeAnotherProperty here, but not ItemHeader or ItemAction. And this ruins whole idea of dynamicaly created context menu.</p>
  
  <p>So, how can i make this template recognize ContextMenuItem as its DataContext?</p>
  
  <p>What i want to do can be accomplished using DataTemplate, but it gives us MenuItem inside MenuItem, and this is not good.</p>
  
  <p><strong>Update</strong></p>
  
  <p>Full xaml code involving ListBox</p>
  
  <pre><code>&lt;ListBox Margin="5, 5" Background="White" ItemsSource="{Binding SwitchAgents, UpdateSourceTrigger=PropertyChanged}" HorizontalContentAlignment="Stretch"&gt;
      &lt;ListBox.ItemTemplate&gt;
          &lt;DataTemplate&gt;
              &lt;Grid Margin="3,1"&gt;
                  &lt;Grid.ContextMenu&gt;
                      &lt;ContextMenu ItemsSource="{Binding ContextMenuItems}"&gt;
                          &lt;ContextMenu.ItemContainerStyle&gt;
                              &lt;Style TargetType="MenuItem"&gt;
                                  &lt;Setter Property="Command" Value="{Binding ItemAction}"/&gt;
                                  &lt;Setter Property="Header" Value="{Binding ItemHeader}"/&gt;
                              &lt;/Style&gt;
                          &lt;/ContextMenu.ItemContainerStyle&gt;
                      &lt;/ContextMenu&gt;
  
                  &lt;/Grid.ContextMenu&gt;
                  &lt;Grid.ColumnDefinitions&gt;
                      &lt;ColumnDefinition Width="1*"/&gt;
                      &lt;ColumnDefinition Width="7*"/&gt;
                  &lt;/Grid.ColumnDefinitions&gt;
                  &lt;CheckBox IsChecked="{Binding Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="0,3"/&gt;
                  &lt;TextBlock Text="{Binding ObjectName}" Grid.Column="1" Margin="0,2"/&gt;
              &lt;/Grid&gt;
          &lt;/DataTemplate&gt;
      &lt;/ListBox.ItemTemplate&gt;
  &lt;/ListBox&gt;
  </code></pre>