This is the behaviour of MenuItem
, Item having Child MenuItem
won't fire Command
and it also should not as it does not make sense. But if you still want to fire a command on Parent Item click,there are two options
You can use Interactivity Triggers on your MenuItem to call command on MouseDown event like
<MenuItem DisplayMemberPath="Name" Header="Teams" ItemsSource="{Binding Teams, Source={StaticResource Container}}"> <i:Interaction.Triggers> <i:EventTrigger EventName="MouseDown"> <cmd:EventToCommand Command="{Binding OpenTeamPage}" /> </i:EventTrigger> </i:Interaction.Triggers> </MenuItem>
you can define a Attached Property for command and define the MenuItem MouseDown behaviour like
public static class MouseCommandBehavior { public static readonly DependencyProperty MouseDownCommandProperty = DependencyProperty.RegisterAttached("MouseDownCommand", typeof(ICommand), typeof(MouseCommandBehavior), new FrameworkPropertyMetadata(null, (obj, e) => OnMouseCommandChanged(obj, (ICommand)e.NewValue, false))); public static ICommand GetMouseDownCommand(DependencyObject d) { return (ICommand)d.GetValue(MouseDownCommandProperty); } public static void SetMouseDownCommand(DependencyObject d, ICommand value) { d.SetValue(MouseDownCommandProperty, value); } private static void OnMouseCommandChanged(DependencyObject d, ICommand command) { if (command == null) return; var element = (FrameworkElement)d; element.PreviewMouseDown += (obj, e) => command.Execute(null); } } }
and you can set this Property value on your menuItem
<MenuItem local:MouseCommandBehavior.MouseDownCommand="{Binding OpenTeamPage}"
DisplayMemberPath="Name"
Header="Teams"
ItemsSource="{Binding Teams,
Source={StaticResource Container}}">