title: WPF - How can I create menu and submenus using binding
body: <p>I am trying to create a dynamic menu using binding. I my viewmodel I have a list of objects which contains an header and a command. However, it is not working. I think the problem is in the data template. See my code below:</p>
  
  <pre><code>&lt;Menu Background="{x:Null}" Grid.Row="0" Grid.Column="1" Panel.ZIndex="2" Width="865" Height="85" HorizontalAlignment="Left" ItemsSource="{Binding Path=MenuItems}"&gt;
  
          &lt;Menu.ItemTemplate&gt;
              &lt;HierarchicalDataTemplate DataType="MenuItemViewModel" ItemsSource="{Binding Path=MenuItems}"&gt;
                  &lt;MenuItem Header="{Binding Header}" Style="{DynamicResource MenuItemStyle1}" ItemsSource="{Binding Path=MenuItems}" Padding="10,12,10,0" Height="44.1" Margin="30,0,0,0" FontWeight="Bold"&gt;
                      &lt;MenuItem.ItemsPanel&gt;
                          &lt;ItemsPanelTemplate&gt;
                              &lt;VirtualizingStackPanel Orientation="Horizontal"/&gt;
                          &lt;/ItemsPanelTemplate&gt;
                      &lt;/MenuItem.ItemsPanel&gt;
                  &lt;/MenuItem&gt;
                  &lt;HierarchicalDataTemplate.ItemTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;MenuItem Header="{Binding Header}" Style="{DynamicResource MenuItemStyle1}" Padding="0,8,0,0" Height="38"&gt;
                          &lt;/MenuItem&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/HierarchicalDataTemplate.ItemTemplate&gt;
              &lt;/HierarchicalDataTemplate&gt;
          &lt;/Menu.ItemTemplate&gt;            
      &lt;/Menu&gt;
  </code></pre>
  
  <p>The result only shows the first menu. The submenus are not shown but they are there since the menus which have children, the arrow is print after menu header.</p>
  
  <p>Could anyone find something wrong on the binding? Or any suggestion?</p>
  
  <p>Just for information, MenuItems is a list of MenuItemViewModel objects which has an header and a list of MenuItemViewModel objects (submenus) called MenuItems too.</p>