Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to define a structure to group the data as you need in your control template. Something like this,</p> <pre><code>public class CommandCollection { public ObservableCollection&lt;Command&gt; Commands { get; set; } } public class Command { public ICommand Action { get; set; } public string Text { get; set; } public string Parameter { get; set; } } </code></pre> <p>Have 3 members of <code>CommandCollection</code> each one having its commands and then assign those as datacontext to the ListViews</p> <p>Updated,</p> <p>After declaring the above structure you declare 3 members,</p> <pre><code>public CommandCollection Container1Commands { get; set; } public CommandCollection Container2Commands { get; set; } public CommandCollection Container3Commands { get; set; } </code></pre> <p>Fill these members,</p> <pre><code> Container1Commands = new CommandCollection (); Container1Commands.Commands = new ObservableCollection&lt;CommandParameters&gt; (); Container1Commands.Commands.Add (new CommandParameters () { Action = NameCommand, Text = "Name" }); Container1Commands.Commands.Add (new CommandParameters () { Action = StreetCommand, Text = "Street" }); Container1Commands.Commands.Add (new CommandParameters () { Action = GroupCommand, Text = "Group" }); Container2Commands = new CommandCollection (); Container2Commands.Commands = new ObservableCollection&lt;CommandParameters&gt; (); Container2Commands.Commands.Add (new CommandParameters () { Action = ViewDetailsCommand, Text = "ViewDetails" }); Container3Commands = new CommandCollection (); Container3Commands.Commands = new ObservableCollection&lt;CommandParameters&gt; (); Container3Commands.Commands.Add (new CommandParameters () { Action = StartCommand, Text = "Start" }); Container3Commands.Commands.Add (new CommandParameters () { Action = StopCommand, Text = "Stop" }); Container3Commands.Commands.Add (new CommandParameters () { Action = LoadCommand, Text = "Load" }); </code></pre> <p>Set data context,</p> <pre><code> this.DataContext = this; this.Container1.DataContext = Container1Commands; this.Container2.DataContext = Container2Commands; this.Container3.DataContext = Container3Commands; </code></pre> <p>Update your control template to specify menu item command binding,</p> <pre><code>&lt;Setter Property="Command" Value="{Binding Action}" /&gt; </code></pre> <p><strong>Updated</strong> XAML</p> <pre><code> &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 Action}" /&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;Grid.RowDefinitions&gt; &lt;RowDefinition /&gt; &lt;RowDefinition /&gt; &lt;RowDefinition /&gt; &lt;/Grid.RowDefinitions&gt; &lt;ListView Grid.Row="0" 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 Grid.Row="1" 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 Grid.Row="2" 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>Code behind</p> <pre><code>// Container 1 public static RoutedUICommand NameCommand = new RoutedUICommand ("Name", "NameCommand", typeof (Window1)); public static RoutedUICommand StreetCommand = new RoutedUICommand ("Street", "StreetCommand", typeof (Window1)); public static RoutedUICommand GroupCommand = new RoutedUICommand ("Add to Group", "AddGroup", typeof (Window1)); // Container 2 public static RoutedUICommand ViewDetailsCommand = new RoutedUICommand ("View Details", "ViewDetailsCommand", typeof (Window1)); // Container 3 public static RoutedUICommand StartCommand = new RoutedUICommand ("Start", "StartCommand", typeof (Window1)); public static RoutedUICommand StopCommand = new RoutedUICommand ("Stop", "StopCommand", typeof (Window1)); public static RoutedUICommand LoadCommand = new RoutedUICommand ("Load", "LoadCommand", typeof (Window1)); public Window1 () { InitializeComponent (); this.Loaded += new RoutedEventHandler (Window1_Loaded); } public CommandCollection Container1Commands { get; set; } public CommandCollection Container2Commands { get; set; } public CommandCollection Container3Commands { get; set; } void Window1_Loaded (object sender, RoutedEventArgs e) { Container1Commands = new CommandCollection (); Container1Commands.Commands = new ObservableCollection&lt;Command&gt; (); Container1Commands.Commands.Add (new Command () { Action = NameCommand, Text = "Name" }); Container1Commands.Commands.Add (new Command () { Action = StreetCommand, Text = "Street" }); Container1Commands.Commands.Add (new Command () { Action = GroupCommand, Text = "Group" }); Container2Commands = new CommandCollection (); Container2Commands.Commands = new ObservableCollection&lt;Command&gt; (); Container2Commands.Commands.Add (new Command () { Action = ViewDetailsCommand, Text = "ViewDetails" }); Container3Commands = new CommandCollection (); Container3Commands.Commands = new ObservableCollection&lt;Command&gt; (); Container3Commands.Commands.Add (new Command () { Action = StartCommand, Text = "Start" }); Container3Commands.Commands.Add (new Command () { Action = StopCommand, Text = "Stop" }); Container3Commands.Commands.Add (new Command () { Action = LoadCommand, Text = "Load" }); this.CommandBindings.Add (new CommandBinding (NameCommand, ExecuteNameCommand, CanExecuteNameCommand)); this.CommandBindings.Add (new CommandBinding (StreetCommand, ExecuteStreetCommand, CanExecuteStreetCommand)); this.CommandBindings.Add (new CommandBinding (GroupCommand, ExecuteGroupCommand, CanExecuteGroupCommand)); this.DataContext = this; this.Container1.DataContext = Container1Commands; this.Container2.DataContext = Container2Commands; this.Container3.DataContext = Container3Commands; } private void ExecuteNameCommand (object inSender, RoutedEventArgs inE) { MessageBox.Show ("Name command Executed"); } private void CanExecuteNameCommand (object inSender, CanExecuteRoutedEventArgs inE) { inE.CanExecute = true; } private void ExecuteStreetCommand (object inSender, RoutedEventArgs inE) { MessageBox.Show ("Street command Executed"); } private void CanExecuteStreetCommand (object inSender, CanExecuteRoutedEventArgs inE) { inE.CanExecute = true; } private void ExecuteGroupCommand (object inSender, RoutedEventArgs inE) { MessageBox.Show ("Group command Executed"); } private void CanExecuteGroupCommand (object inSender, CanExecuteRoutedEventArgs inE) { inE.CanExecute = true; } </code></pre> <p>Other classes</p> <pre><code>public class CommandCollection { public ObservableCollection&lt;Command&gt; Commands { get; set; } } public class Command { public ICommand Action { get; set; } public string Text { get; set; } public string Parameter { get; set; } } </code></pre> <p>I Hope now you get it working.</p> <p>Updated for RoutedUICommand description, The idea should be to have these menu items in the outer container (like shell) which will have other pages in it (like a frame/canvas), like for example if you see MS Visual Studio the menu items (Save) are part of the application shell and the files openeed are within the shell (shell has a container tabcontrol maybe, where the files are loaded as they are opened). So the routed commands (Save) are defined by the application shell and all the other pages inside the shell's container add those commands in there command binding collection (<code>this.CommandBindings.Add(cmdname, actionname, predicatename)</code>) so each page performs its own respective action and the command is invoked for them only when they are in focus.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload