How can I bind an ObservableCollection of ViewModels to a MenuItem?

19,265

Solution 1

I found using MVVM with MenuItems to be very challenging. The rest of my application uses DataTemplates to pair the View with the ViewModel, but that just doesn't seem to work with Menus because of exactly the reasons you've described. Here's how I eventually solved it. My View looks like this:

<DockPanel>
<Menu DockPanel.Dock="Top" ItemsSource="{Binding Path=(local:MainViewModel.MainMenu)}">
    <Menu.ItemContainerStyle>
        <Style>
            <Setter Property="MenuItem.Header" Value="{Binding Path=(contracts:IMenuItem.Header)}"/>
            <Setter Property="MenuItem.ItemsSource" Value="{Binding Path=(contracts:IMenuItem.Items)}"/>
            <Setter Property="MenuItem.Icon" Value="{Binding Path=(contracts:IMenuItem.Icon)}"/>
            <Setter Property="MenuItem.IsCheckable" Value="{Binding Path=(contracts:IMenuItem.IsCheckable)}"/>
            <Setter Property="MenuItem.IsChecked" Value="{Binding Path=(contracts:IMenuItem.IsChecked)}"/>
            <Setter Property="MenuItem.Command" Value="{Binding}"/>
            <Setter Property="MenuItem.Visibility" Value="{Binding Path=(contracts:IMenuItem.Visible), 
                Converter={StaticResource BooleanToVisibilityConverter}}"/>
            <Setter Property="MenuItem.ToolTip" Value="{Binding Path=(contracts:IMenuItem.ToolTip)}"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=(contracts:IMenuItem.IsSeparator)}" Value="true">
                    <Setter Property="MenuItem.Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type MenuItem}">
                                <Separator Style="{DynamicResource {x:Static MenuItem.SeparatorStyleKey}}"/>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Menu.ItemContainerStyle>
</Menu>
</DockPanel>

If you notice, I defined an interface called IMenuItem, which is the ViewModel for a MenuItem. Here's the code for that:

public interface IMenuItem : ICommand
{
    string Header { get; }
    IEnumerable<IMenuItem> Items { get; }
    object Icon { get; }
    bool IsCheckable { get; }
    bool IsChecked { get; set; }
    bool Visible { get; }
    bool IsSeparator { get; }
    string ToolTip { get; }
}

Notice that the IMenuItem defines IEnumerable Items, which is how you get sub-menus. Also, the IsSeparator is a way to define separators in the menu (another tough little trick). You can see in the xaml how it uses a DataTrigger to change the style to the existing separator style if IsSeparator is true. Here's how MainViewModel defines the MainMenu property (that the view binds to):

public IEnumerable<IMenuItem> MainMenu { get; set; }

This seems to work well. I assume you could use an ObservableCollection for the MainMenu. I'm actually using MEF to compose the menu out of parts, but after that the items themselves are static (even though the properties of each menu item are not). I also use an AbstractMenuItem class that implements IMenuItem and is a helper class to instantiate menu items in the various parts.

UPDATE:

Regarding your color problem, does this thread help?

Solution 2

Don't put the MenuItem in the DataTemplate. The DataTemplate defines the content of the MenuItem. Instead, specify extraneous properties for the MenuItem via the ItemContainerStyle:

<Menu>
    <Menu.ItemContainerStyle>
        <Style TargetType="MenuItem">
            <Setter Property="Header" Value="{Binding Title}"/>
            ...
        </Style>
    </Menu.ItemContainerStyle>
    <MenuItem 
        Header="Options" ItemsSource="{Binding ManageMenuPageItemViewModels}"
              ItemTemplate="{StaticResource MainMenuTemplate}"/>
</Menu>

Also, take a look at HierarchicalDataTemplates.

Solution 3

Just make your DataTemplate to be a TextBlock (or maybe a stack panel with an icon and a TextBlock).

Solution 4

Here is how I have done my menus. It may not be precisely what you need, but I think it is pretty close.

  <Style x:Key="SubmenuItemStyle" TargetType="MenuItem">
    <Setter Property="Header" Value="{Binding MenuName}"></Setter>
    <Setter Property="Command" Value="{Binding Path=MenuCommand}"/>
    <Setter Property="ItemsSource" Value="{Binding SubmenuItems}"></Setter>
  </Style>

  <DataTemplate DataType="{x:Type systemVM:TopMenuViewModel}" >
    <Menu>
      <MenuItem Header="{Binding MenuName}"         
                    ItemsSource="{Binding SubmenuItems}" 
                    ItemContainerStyle="{DynamicResource SubmenuItemStyle}" />
    </Menu>
  </DataTemplate>

    <Menu DockPanel.Dock="Top" ItemsSource="{Binding Menus}" />

TopMenuViewModel is a collection of the menus that will appear on the menu bar. They each contain the MenuName that will be displayed and a collection called SubMenuItems that I set to be the ItemsSource.

I control the way the SubMenuItems are displayed by way of the style SumMenuItemStyle. Each SubMenuItem has its own MenuName property, Command property of type ICommand, and possibly another collection of SubMenuItems.

The result is that I am able to store all my menu information in a database and dynamically switch what menus are displayed at runtime. The entire menuitem area is clickable and displays correctly.

Hope this helps.

Share:
19,265
Angry Dan
Author by

Angry Dan

web/software developer, .NET, C#, WPF, PHP, software trainer, English teacher, have philosophy degree, love languages, run marathons my tweets: http://www.twitter.com/edward_tanguay my runs: http://www.tanguay.info/run my code: http://www.tanguay.info/web my publications: PHP 5.3 training video (8 hours, video2brain) my projects: http://www.tanguay.info

Updated on June 12, 2022

Comments

  • Angry Dan
    Angry Dan almost 2 years

    When I bind Menu Items with an ObservableCollection, only the "inner" area of the MenuItem is clickable:

    alt text http://tanguay.info/web/external/mvvmMenuItems.png

    In my View I have this menu:

    <Menu>
        <MenuItem 
            Header="Options" ItemsSource="{Binding ManageMenuPageItemViewModels}"
                  ItemTemplate="{StaticResource MainMenuTemplate}"/>
    </Menu>
    

    Then I bind it with this DataTemplate:

    <DataTemplate x:Key="MainMenuTemplate">
        <MenuItem
            Header="{Binding Title}" 
            Command="{Binding DataContext.SwitchPageCommand,
            RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Menu}}}" 
            Background="Red"
            CommandParameter="{Binding IdCode}"/>
    </DataTemplate>
    

    Since each ViewModel in the ObservableCollection ManageMenuPageItemViewModels has a property Title and IdCode, the above code works fine at first sight.

    HOWEVER, the problem is that the MenuItem in the DataTemplate is actually inside another MenuItem (as if it is being bound twice) so that in the above DataTemplate with Background="Red" there is a Red box inside each menu item and only this area can be clicked, not the whole menu item area itself (e.g. if the user clicks on the area where the checkmark is or to the right or left of the inner clickable area, then nothing happens, which, if you don't have a separate color is very confusing.)

    What is the correct way to bind MenuItems to an ObservableCollection of ViewModels so that the whole area inside each MenuItem is clickable?

    UPDATE:

    So I made the following changes based on advice below and now have this:

    alt text http://tanguay.info/web/external/mvvmMenuItemsYellow.png

    I have only a TextBlock inside my DataTemplate, but I still can't "color the whole MenuItem" but just the TextBlock:

    <DataTemplate x:Key="MainMenuTemplate">
        <TextBlock Text="{Binding Title}"/>
    </DataTemplate>
    

    And I put the Command binding into Menu.ItemContainerStyle but they don't fire now:

    <Menu DockPanel.Dock="Top">
        <Menu.ItemContainerStyle>
            <Style TargetType="MenuItem">
                <Setter Property="Background" Value="Yellow"/>
                <Setter Property="Command" Value="{Binding DataContext.SwitchPageCommand,
            RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Menu}}}"/>
                <Setter Property="CommandParameter" Value="{Binding IdCode}"/>
            </Style>
        </Menu.ItemContainerStyle>
        <MenuItem 
            Header="MVVM" ItemsSource="{Binding MvvmMenuPageItemViewModels}"
                  ItemTemplate="{StaticResource MainMenuTemplate}"/>
        <MenuItem 
            Header="Application" ItemsSource="{Binding ApplicationMenuPageItemViewModels}"
                  ItemTemplate="{StaticResource MainMenuTemplate}"/>
        <MenuItem 
            Header="Manage" ItemsSource="{Binding ManageMenuPageItemViewModels}"
                  ItemTemplate="{StaticResource MainMenuTemplate}"/>
    </Menu>
    
  • Angry Dan
    Angry Dan almost 15 years
    do you mean define the header/color in the Menu.ItemContainerStyle and then inside the DataTemplate put a HierarchicalDataTemplate that defines the Command and CommandParameter?
  • Angry Dan
    Angry Dan almost 15 years
    ok that is great, it works (I thought I had tried that), but now I have to hook up the command somehow to the TextBlock, it not having a Command attribute, I can't use my DelegateCommand, what did you use AttachedBehaviors or something else?
  • Angry Dan
    Angry Dan almost 15 years
    edited it accordingly above, posted screenshot, still not working :-(
  • cwhisperer
    cwhisperer almost 15 years
    Hmmm, you might have to bind it to the tag of the TextBlock, then write an OnApplyTemplate handler or something and walk the tree back up to the MenuItem. Super hacky and non-WPF'y, but sometimes you have to.
  • Judah Gabriel Himango
    Judah Gabriel Himango about 14 years
    Thanks, the editing the ItemContainerStyle worked for me. (Used Kent Boogaart's example.)
  • Judah Gabriel Himango
    Judah Gabriel Himango about 14 years
    Thanks, this is exactly was I was looking for. Works great. Thanks!
  • Matt DeKrey
    Matt DeKrey over 13 years
    +1 - very nice for the full example with Separators and everything.
  • Diuter
    Diuter over 13 years
    +1 - HierarchicalDataTemplates make this whole problem nearly trivial.
  • Dina
    Dina over 9 years
    I have a very similar design and everything works except for showing the separator. If I change the template to <Label>Separator</Label> then I see "Separator" where a separator should be. But when I try to use the template like in your answer nothing is shown. I tried <Separator BorderThickness="100" BorderBrush="Black"></Separator> and then the separator is visible but I want the default style with the width dynamically set by the menu width. Am I supposed to define SeparatorStyleKey somewhere? I searched online but couldn't find anything which helped... Thanks!