juharr
juharr

Reputation: 32266

WPF key gesture PageDown shows up as Next in Menu Item

I've setup a custom command with PageDown as the key gesture, but for the MenuItem that is bound to that command the gesture shows up as "Next". The problem is that I have commands that use PageUp, Home, and End for related tasks and they all show up as expected for their MenuItems. Is there some way to make the MenuItem show "PageDown" instead of "Next" for consistency?

Here's the xaml that defines the commands.

<Window.Resources>
    <RoutedUICommand x:Key="FirstPageCommand" 
                     Text="FirstPage">
        <RoutedUICommand.InputGestures>
            <KeyGesture>Home</KeyGesture>
        </RoutedUICommand.InputGestures>
    </RoutedUICommand>
    <RoutedUICommand x:Key="LastPageCommand" 
                     Text="LastPage">
        <RoutedUICommand.InputGestures>
            <KeyGesture>End</KeyGesture>
        </RoutedUICommand.InputGestures>
    </RoutedUICommand>
    <RoutedUICommand x:Key="PreviousPageCommand" 
                     Text="PreviousPage">
        <RoutedUICommand.InputGestures>
            <KeyGesture>PageUp</KeyGesture>
        </RoutedUICommand.InputGestures>
    </RoutedUICommand>
    <RoutedUICommand x:Key="NextPageCommand" 
                     Text="NextPage">
        <RoutedUICommand.InputGestures>
            <KeyGesture>PageDown</KeyGesture>
        </RoutedUICommand.InputGestures>
    </RoutedUICommand>
</Window.Resources>

And here is where I use them in a Menu

<MenuItem Header="_View">
    <MenuItem Header="_First Page" 
              Command="{StaticResource FirstPageCommand}">
        <MenuItem.Icon>
            <Image Source="Images\Backward_01.png" 
               Stretch="Uniform"/>
        </MenuItem.Icon>
    </MenuItem>
    <MenuItem Header="_Previous Page" 
              Command="{StaticResource PreviousPageCommand}">
        <MenuItem.Icon>
            <Image Source="Images\Backward.png"
               Stretch="Uniform"/>
        </MenuItem.Icon>
    </MenuItem>
    <MenuItem Header="_Next Page" 
              Command="{StaticResource NextPageCommand}">
        <MenuItem.Icon>
            <Image Source="Images\Forward.png"
               Stretch="Uniform"/>
        </MenuItem.Icon>
    </MenuItem>
    <MenuItem Header="_Last Page" 
              Command="{StaticResource LastPageCommand}">
        <MenuItem.Icon>
            <Image Source="Images\Forward_01.png"
               Stretch="Uniform"/>
        </MenuItem.Icon>
    </MenuItem>
</MenuItem>

My menu looks like this

Upvotes: 2

Views: 2378

Answers (1)

juharr
juharr

Reputation: 32266

Apparently this is only possible if you define your commands in code like this.

public class MyCommands
{
    public static RoutedUICommand NextPage { get; private set; }
    public static RoutedUICommand PreviousPage { get; private set; }

    static OCRopingCommands()
    {
        NextPage = new RoutedUICommand("NextPage", "NextPage", typeof(MyCommands));
        NextPage.InputGestures.Add(
            new KeyGesture(Key.PageDown, ModifierKeys.None, "PageDn"));
        PreviousPage = new RoutedUICommand("PreviousPage", "PreviousPage", typeof(MyCommands));
        PreviousPage.InputGestures.Add(
            new KeyGesture(Key.PageUp, ModifierKeys.None, "PageUp"));
    }
}

And here's how you'd wire them up

<MenuItem Header="_Previous Page" 
          Command="local:MyCommands.PreviousPage">
    <MenuItem.Icon>
        <Image Source="Images\Backward.png"
           Stretch="Uniform"/>
    </MenuItem.Icon>
</MenuItem>
<MenuItem Header="_Next Page" 
          Command="local:MyCommands.NextPage">
    <MenuItem.Icon>
        <Image Source="Images\Forward.png"
           Stretch="Uniform"/>
    </MenuItem.Icon>
</MenuItem>

Why the KeyGesture doesn't allow you to set the display name in xaml is beyond me.

Upvotes: 3

Related Questions