代码之家  ›  专栏  ›  技术社区  ›  juharr

WPF键手势向下翻页在菜单项中显示为下一个

  •  2
  • juharr  · 技术社区  · 15 年前

    我已经设置了一个自定义命令,其中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>
    

    我的菜单像这样

    • 视图
      • 首页首页
      • 最后一页翻页
      • 下一页下一页
      • 最后一页结束
    1 回复  |  直到 15 年前
        1
  •  3
  •   juharr    15 年前

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

    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中设置显示名称超出我的范围。