0
votes

I'm building a WPF application using MVVM Light. I've added a menu to my window that includes the standard File and Edit menus:

<Window x:Class="ParserEditor.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ParserEditor"
        xmlns:ignore="http://www.galasoft.ch/ignore"
        xmlns:i="clr namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
        xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
        mc:Ignorable="d ignore"
        Closing="Window_Closing"
        DataContext="{Binding Main, Source={StaticResource Locator}}"
        Height="{Binding Height, Mode=TwoWay}"
        MaxHeight="{Binding MaxHeight, Mode=TwoWay}"
        MinHeight="{Binding MinHeight, Mode=TwoWay}"
        MinWidth="450"
        Width="450"
        Title="{Binding WindowTitle}">

    <Window.InputBindings>
        <KeyBinding Command="{Binding NewParserCommand}" Gesture="CTRL+N" />
        <KeyBinding Command="{Binding OpenParserCommand}" Gesture="CTRL+O" />
        <KeyBinding Command="{Binding SaveParserCommand}" Gesture="CTRL+S" />
        <KeyBinding Command="{Binding SaveParserAsCommand}" Gesture="ALT+A" />
        <KeyBinding Command="{Binding ExitCommand}" Gesture="ALT+F4" />
        <KeyBinding Command="{Binding ApplicationCommands.Undo}" Gesture="CTRL+Z" />
        <KeyBinding Command="{Binding ApplicationCommands.Redo}" Gesture="CTRL+Y" />
        <KeyBinding Command="{Binding ApplicationCommands.Cut}" Gesture="CTRL+X" />
        <KeyBinding Command="{Binding ApplicationCommands.Copy}" Gesture="CTRL+C" />
        <KeyBinding Command="{Binding ApplicationCommands.Paste}" Gesture="CTRL+V" />
        <KeyBinding Command="{Binding ApplicationCommands.Delete}" Gesture="DEL" />
        <KeyBinding Command="{Binding ApplicationCommands.SelectAll}" Gesture="CTRL+A" />
    </Window.InputBindings>

    <DockPanel Name="Dock">
        <Menu IsMainMenu="true" DockPanel.Dock="Top">
            <MenuItem Header="_File">
                <MenuItem Header="_New..." InputGestureText="Ctrl-N" Command="{Binding NewParserCommand}" />
                <MenuItem Header="_Open..." InputGestureText="Ctrl-O" Command="{Binding OpenParserCommand}" />
                <MenuItem Header="_Save..." InputGestureText="Ctrl-S" Command="{Binding SaveParserCommand}" />
                <MenuItem Header="Save _As..." InputGestureText="Alt-A" Command="{Binding SaveParserAsCommand}" />
                <Separator />
                <MenuItem Header="E_xit" InputGestureText="Alt-F4" Command="{Binding ExitCommand}" />
            </MenuItem>
            <MenuItem Header="_Edit">
                <MenuItem Header="Undo" InputGestureText="Ctrl-Z" />
                <MenuItem Header="Redo" InputGestureText="Ctrl-Y" />
                <Separator/>
                <MenuItem Header="Cut" InputGestureText="Ctrl-X" />
                <MenuItem Header="Copy" InputGestureText="Ctrl-C" />
                <MenuItem Header="Paste" InputGestureText="Ctrl-V" />
                <MenuItem Header="Delete" InputGestureText="Del" />
                <MenuItem Header="Select All" InputGestureText="Ctrl-A" />
            </MenuItem>
        </Menu>

        <Grid x:Name="LayoutRoot">
            <!-- Some controls, including TextBlocks & TextBoxes here -->            
        </Grid>
    </DockPanel>
</Window>

Now, if I run the application and make some edits in one of the TextBox controls, I can use the usual keyboard short cuts to cut, copy & paste text, undo & redo actions while in focus is the control. But if I click on my Edit menu and use one of my options there, nothing happens.

How do I get my menu items to work?

1

1 Answers

0
votes

I figured this out. After posting the question & reviewing the XAML, I noticed that the Edit menu items didn't have any Commands bound to them. I added the required Command="ApplicationCommands.xxx" statements to the XAML & now the Edit menu works.