0

Have a code like:

<MenuItem ItemSource="SOURCE">
   ..sub MenuItems
</MenuItem>

How to create the right template that allows to bind the each subitem to the save command.

Baranovskiy Dmitry
  • 463
  • 2
  • 5
  • 23

2 Answers2

1

This is how I did it in a project:

In the Window definition i "define" the commands workspace.

   <Window x:Class="WorkForce.Views.MainWindow"
    ...
    xmlns:commands="clr-namespace:WorkForce.Commands"
    ...
    >

After that I connect them to each menu-item.

        <MenuItem Header="_File">
            <MenuItem Header="_New..." Command="commands:MainWindowCommands.NewFile"/>
            <MenuItem Header="_Open..."  Command="commands:MainWindowCommands.OpenFile"/>
            <MenuItem Header="_Save..."  Command="commands:MainWindowCommands.SaveFile"/>
        </MenuItem>

I hope that is enough

I saw that you want to add it dynamically. Please look at this: WPF: How can you add a new menuitem to a menu at runtime?

Community
  • 1
  • 1
Lucian
  • 3,407
  • 2
  • 21
  • 18
1

Something like that:

<MenuItem ItemsSource="{Binding SubItems}">
  <MenuItem.ItemContainerStyle>
    <Style TargetType="MenuItem">
      <Setter Property="Command" Value="{Binding SaveCommand}"/>
    </Style>
  </MenuItem.ItemContainerStyle>
</MenuItem>
Nicolas Repiquet
  • 9,097
  • 2
  • 31
  • 53