title: Mixing dynamic and static XAML menu items
body: <p>I have a scenario in which I need to have both static and dynamic menu items.  The static items will be defined in XAML and the dynamic ones supplied by a View Model.  Each dynamic item will itself be represented by a VieModel, lets call it a CommandViewModel.
  A CommandViewModel has amongst other things a display name, it can also contain other CommandViewModels.</p>
  
  <p>The MainViewModel that gets used as the datacontext for the menu is as follows:</p>
  
  <pre><code>public class MainMenuViewModel : INotifyPropertyChanged
  {
  
    private ObservableCollection&lt;CommandViewModel&gt; m_CommandVMList;
  
  
    public MainMenuViewModel()
    {
      m_ CommandVMList = new ObservableCollection&lt;CommandViewModel&gt;();
  
      CommandViewModel cmv = new CommandViewModel();
      cmv.DisplayName = "Dynamic Menu 1";
      m_CommandVMList.Add(cmv);
  
      cmv = new CommandViewModel();
      cmv.DisplayName = "Dynamic Menu 2";
      m_CommandVMList.Add(cmv);
  
      cmv = new CommandViewModel();
      cmv.DisplayName = "Dynamic Menu 3";
      m_CommandVMList.Add(cmv);
  
    }
  
    public ObservableCollection&lt;CommandViewModel&gt; CommandList
    {
      get { return m_CommandVMList; }
      set
      {
        m_CommandVMList = value;
        OnPropertyChanged("CommandList");
      }
    }
  </code></pre>
  
  <p>…
  …
  …</p>
  
  <p>The Menu XAML:</p>
  
  <pre><code>&lt;Grid&gt;
    &lt;Grid.Resources&gt;
      &lt;HierarchicalDataTemplate DataType="{x:Type Fwf:CommandViewModel}" ItemsSource="{Binding Path=CommandViewModels}"&gt;
        &lt;MenuItem Header="{Binding Path=DisplayName}"/&gt;
      &lt;/HierarchicalDataTemplate&gt;
    &lt;/Grid.Resources&gt;
  
    &lt;Menu VerticalAlignment="Top" HorizontalAlignment="Stretch"&gt;
      &lt;MenuItem Header="Static Top Menu Item 1"&gt;
        &lt;MenuItem Header="Static Menu Item 1"/&gt;
          &lt;MenuItem Header="Static Menu Item 2"/&gt;
          &lt;MenuItem Header="Static Menu Item 3"/&gt;
          &lt;ItemsControl ItemsSource="{Binding Path= CommandList}"/&gt;
          &lt;MenuItem Header="Static Menu Item 4"/&gt;
        &lt;/MenuItem&gt;
    &lt;/Menu&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>All works fine except that whatever I try to represent the list of dynamic menus, in this case an ItemsControl it is being shown on the UI as ONE Menu Item conatining more menu items, so the entire collection of dynamic  menu items get selected when you click on the item.  The collection gets represented correctly in that each dynamic menu item is show as a menu item itself but within this bigger menu item.  I think I see why as the Menu is simply creating a menu item for each of the contained items, static or dynamic it does not care.  Is there a way to have each dynamic menu item be created on the same level and belonging to the parent menu item as the static ones on the example does ?</p>