title: Binding Menu ItemsSource to a recursive data structure
body: <p>I have a simple data structure (the properties are of course Dependency Properties):</p>
  
  <pre><code>class MenuData{
      public string Header {get; protected set;}
      public ObservableCollection&lt;MenuData&gt; Items { get; protected set; }
  }
  
  ObservableCollection&lt;MenuData&gt; Menus {get; protected set;}
  </code></pre>
  
  <p>I want to bind the Menus property as Menu element ItemsSource. The result should look as standard Windows menu bar: First level of items will be lined horizontally with sub-items being hidden is sub-menus.</p>
  
  <p>I experimented with XAML to create the first level for now (the horizontal menu bar):</p>
  
  <pre><code>&lt;my:MenuElement ItemsSource="{Binding Menus}"&gt;
      &lt;my:MenuElement.ItemTemplate&gt;
          &lt;DataTemplate&gt;                
              &lt;MenuItem Header="{Binding Header}"&gt;                    
                  &lt;!-- Try to add some sub-menus to see what it is going to look like. --&gt;
                  &lt;MenuItem Header="A" /&gt;
                  &lt;MenuItem Header="A" /&gt;
                  &lt;MenuItem Header="A" /&gt;
                  &lt;MenuItem Header="A" /&gt;
              &lt;/MenuItem&gt;
          &lt;/DataTemplate&gt;
      &lt;/my:MenuElement.ItemTemplate&gt;
  &lt;/my:MenuElement&gt;
  </code></pre>
  
  <p>I used my custom overload of MenuItem, because the ItemTemplate is already wrapped in MenuItem tag for Menu element, so I had to get rid of that:</p>
  
  <pre><code>class MenuElement: System.Windows.Controls.Menu 
  {
      protected override DependencyObject GetContainerForItemOverride()
      {
          return new System.Windows.Controls.ContentPresenter();
      }
  }
  </code></pre>
  
  <p>But the result looks all wrong (notice the highlight being too large and the sub-menu arrows)</p>
  
  <p><img src="https://i.stack.imgur.com/jUvNp.png" alt="alt text"></p>
  
  <p><strong>Could you please point me into a right direction so I could make this work?</strong></p>
  
  <p>Thanks for any help.</p>