I'm beginner in MVVM and I've been trying to display MenuItems in a ContextMenu, but something seems to be wrong. I've already made a working Menu which has Itemssource bound to a collection in the viewmodel, but it doesn't work in case of the ContextMenu. Could you guys take a look and say if I'm missing something?
Here's the Menu, which works well:
<Menu DockPanel.Dock="right" ItemsSource="{Binding Path=modes}" Background="#FF303030" Foreground="White" Height="21">
<Menu.Resources>
<Style TargetType="{x:Type MenuItem}">
<Setter Property="Background" Value="#FF303030"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="Height" Value="21" />
<Setter Property="Visibility" Value="{Binding visibility}" />
<Setter Property="Header" Value="{Binding header}" />
<Setter Property="ItemsSource" Value="{Binding children}"/>
<Setter Property="Command" Value="{Binding action}"/>
<Setter Property="CommandParameter" Value="{Binding attachedData}"/>
</Style>
</Menu.Resources>
</Menu>
And here's the XAML of the ContextMenu, which, for some reason, doesn't display any MenuItems:
<Grid x:Name="btnMode" PreviewMouseDown="OnBtnModeMouseDown" Width="19" Height="19" Margin="0,0" DockPanel.Dock="Right">
<Canvas>
<Path Data="M0,0 L19,0 L19,19 L0,19" Margin="0,0" Fill="#FF303030"/>
<Ellipse Height="12" Margin="4,3,0,0" Width="12" Fill="#FFFDFDFD"/>
<Path Data="M1.5,1L18.5,1 L10,9.5" Margin="0,0,0,8.875" Fill="#FF303030"/>
</Canvas>
<Grid.ContextMenu>
<ContextMenu ItemsSource="{Binding Path=modes}">
<ContextMenu.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="Background" Value="#FF303030"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="Height" Value="21" />
<Setter Property="Visibility" Value="{Binding visibility}" />
<Setter Property="Header" Value="{Binding header}" />
<Setter Property="ItemsSource" Value="{Binding children}"/>
<Setter Property="Command" Value="{Binding action}"/>
<Setter Property="CommandParameter" Value="{Binding attachedData}"/>
</Style>
</ContextMenu.ItemContainerStyle>
</ContextMenu>
</Grid.ContextMenu>
</Grid>
I also have a ContextMenu that has hard-coded menu items:
<Grid x:Name="btnMenu" PreviewMouseDown="OnBtnMenuMouseDown" Width="19" Height="19" Margin="0,1" DockPanel.Dock="Right">
<Canvas>
<Path Data="M0,0 L19,0 L19,19 L0,19" Margin="0,0" Fill="#FF303030"/>
<Path Data="M0,0L7,0 L3.5,7" Margin="6,7" Fill="White"/>
</Canvas>
<Grid.ContextMenu >
<ContextMenu IsVisibleChanged="OnBtnMenuPopup">
<MenuItem Name="menuFloatingWindow" Header="Floating window" Click="OnDockingMenu"></MenuItem>
<MenuItem Name="menuDockedWindow" Header="Docked window" Click="OnDockingMenu"></MenuItem>
<MenuItem Name="menuTabbedDocument" Header="Tabbed document" Click="OnDockingMenu"></MenuItem>
<MenuItem Name="menuAutoHide" Header="Auto hide" Click="OnDockingMenu"></MenuItem>
<MenuItem Name="menuClose" Header="Close" Click="OnDockingMenu"></MenuItem>
</ContextMenu>
</Grid.ContextMenu>
</Grid>
The collection of MenuItems is an ObservableCollection, defined as follows:
_menus = new ObservableCollection<MenuItem>()
{
new MenuItem(
"File", // string header
Visibility.Visible, // Visibility visibility
null, // ICommand action
null, // object attachedData (CommandParameters)
new ObservableCollection<MenuItem>() // children
{
new MenuItem(
"Exit",
Visibility.Visible,
MainWindow.exitCommand,
null,
null
)
}
),
..., // more nested MenuItems here
};
Both the working Menu and the ContextMenu have the same ObservableCollection as an ItemSource.
Here's an image showing how the controls look in the application:
Did I mess something up in XAML?
P.S. I tried debugging the XAML at runtime using this: http://www.wpftutorial.net/DebugDataBinding.html but I couldn't see any helpful information when the breakpoint occured.