title: WPF Dynamic menu associated with the object?
body: <p>I'd like to be able to create dynamic menus associated with certain object. Let’s say, I will have 3 listview container with one style where I also have a Menu. I need to generate different menu items from collection of the RoutetUICommands in relation on each listview. I was trying to solve this puzzle but took me a while and still have trouble making it work. I need to generate object specific menus, an unique menu for each listview. Any ideas are highly appreciated. Thank you!</p>
  
  <p>XAML:</p>
  
  <pre><code>&lt;Window
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
  x:Class="DynamicMenu.MainWindow"
  x:Name="Window"
  Title="MainWindow"
  Width="640" Height="480"&gt;
  &lt;Window.Resources&gt;
  
      &lt;Style x:Key="ListViewStyleTask" TargetType="{x:Type ListView}"&gt;
          &lt;Setter Property="Template"&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate TargetType="{x:Type ListView}"&gt;
                      &lt;Grid&gt;
                          &lt;Menu x:Name="mainMenu"&gt;
                              &lt;MenuItem x:Name="menuItem" Header="Tasks" ItemsSource="{Binding Commands}" &gt;
                                  &lt;MenuItem.ItemContainerStyle&gt;
                                      &lt;Style TargetType="{x:Type MenuItem}"&gt;
                                          &lt;Setter Property="Command" Value="{Binding}" /&gt;
                                          &lt;Setter Property="Header" Value="{Binding Path=Text}" /&gt;
                                          &lt;Setter Property="CommandParameter" Value="{Binding Path=Parameter}" /&gt;
                                      &lt;/Style&gt;
                                  &lt;/MenuItem.ItemContainerStyle&gt;
                              &lt;/MenuItem&gt;
                          &lt;/Menu&gt;
                      &lt;/Grid&gt;
  
                  &lt;/ControlTemplate&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
      &lt;/Style&gt;
  
  &lt;/Window.Resources&gt;
  
  &lt;Grid x:Name="LayoutRoot"&gt;
      &lt;ListView x:Name="Container1" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" Style="{DynamicResource ListViewStyleTask}"&gt;
          &lt;ListView.View&gt;
              &lt;GridView&gt;
                  &lt;GridViewColumn/&gt;
              &lt;/GridView&gt;
          &lt;/ListView.View&gt;
      &lt;/ListView&gt;
      &lt;ListView x:Name="Container2" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" Style="{DynamicResource ListViewStyleTask}"&gt;
          &lt;ListView.View&gt;
              &lt;GridView&gt;
                  &lt;GridViewColumn/&gt;
              &lt;/GridView&gt;
          &lt;/ListView.View&gt;
      &lt;/ListView&gt;
      &lt;ListView x:Name="Container3" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" Style="{DynamicResource ListViewStyleTask}"&gt;
          &lt;ListView.View&gt;
              &lt;GridView&gt;
                  &lt;GridViewColumn/&gt;
              &lt;/GridView&gt;
          &lt;/ListView.View&gt;
      &lt;/ListView&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>I have added some commands which I need to associate to 3 different Listviews:
  
      public partial class MainWindow : Window
  {
  
      // Container 1
      public static RoutedUICommand NameCommand = new RoutedUICommand("Name", "NameCommand", typeof(MainWindow));
      public static RoutedUICommand StreetCommand = new RoutedUICommand("Street", "StreetCommand", typeof(MainWindow));
      public static RoutedUICommand GroupCommand = new RoutedUICommand("Add to Group", "AddGroup", typeof(MainWindow));
  
      // Container 2
      public static RoutedUICommand ViewDetailsCommand = new RoutedUICommand("View Details", "ViewDetailsCommand", typeof(MainWindow));
  
      // Container 3
      public static RoutedUICommand StartCommand = new RoutedUICommand("Start", "StartCommand", typeof(MainWindow));
      public static RoutedUICommand StopCommand = new RoutedUICommand("Stop", "StopCommand", typeof(MainWindow));
      public static RoutedUICommand LoadCommand = new RoutedUICommand("Load", "LoadCommand", typeof(MainWindow));
  
      public MainWindow()
      {
          this.InitializeComponent();
  
          // Insert code required on object creation below this point.
      }
  }
  </code></pre>
  
  <p>}</p>