WPF 键手势 PageDown 在菜单项中显示为“Next”

发布于 2024-09-05 18:33:56 字数 2697 浏览 10 评论 0原文

我已经设置了一个使用 PageDown 作为关键手势的自定义命令,但对于绑定到该命令的 MenuItem,手势显示为“下一步”。问题是我有一些命令使用 PageUp、Home 和 End 来执行相关任务,并且它们都按预期的菜单项显示。有没有办法让 MenuItem 显示“PageDown”而不是“Next”以保持一致性?

这是定义命令的 xaml。

<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>

这是我在菜单中使用它们的地方

<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>

我的菜单看起来像这样

  • View
    • First Page Home
    • Last PageUp
    • Next Page Next
    • Last Page End

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

  • View
    • First Page Home
    • Last Page PageUp
    • Next Page Next
    • Last Page End

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初熏 2024-09-12 18:33:56

显然,只有当您在这样的代码中定义命令时,这才有可能。

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"));
    }
}

以下是如何连接它们。

<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>

为什么 KeyGesture 不允许您在 xaml 中设置显示名称,这超出了我的理解范围。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文