如何在XAML中在菜单项旁显示键盘快捷方式

3
我正在使用XAML中的MenuItem,通过InputGestureText添加了快捷键到菜单项中。我困惑的是如何在实际菜单中显示每个具有键盘快捷键的项目旁边的键盘快捷键。

我不想将文本添加到Header中,而是要显示InputGestureText的值,因此可以在不修改标题的情况下更改。

我正在使用WPF Localization Extension

Menu的代码:
<Menu Grid.Row="0" Height="30" >
        <MenuItem Header="{lex:Loc Project75:lang:mnuFileMenu}">
            <MenuItem Header="{lex:Loc Project75:lang:mnuFileMenuNew}">
                <MenuItem Header="{lex:Loc Project75:lang:mnuFileMenuNew}" Command="{Binding Path=NewCommand}" InputGestureText="Ctrl+N"/>
                <MenuItem Header="{lex:Loc Project75:lang:mnuFileMenuNewEmailTemplate}" Command="{Binding Path=NewEmailTemplateCommand}" />
                <MenuItem Header="{lex:Loc Project75:lang:mnuFileMenuNewContact}" Command="{Binding Path=NewContactCommand}" />
            </MenuItem>
            <MenuItem Header="{lex:Loc Project75:lang:mnuFileMenuImport}" Command="{Binding Path=ImportCommand}" InputGestureText="Ctrl+I"/>
            <MenuItem Header="{lex:Loc Project75:lang:mnuFileMenuExport}" Command="{Binding Path=ExportCommand}" InputGestureText="Ctrl+E"/>
            <Separator/>
            <MenuItem Header="{lex:Loc Project75:lang:mnuFileMenuSave}" Command="{Binding Path=SaveDocumentCommand}" InputGestureText="Ctrl+S"/>
            <MenuItem Header="{lex:Loc Project75:lang:mnuFileMenuSaveAll}" Command="{Binding Path=SaveAllDocumentsCommand}" InputGestureText="Ctrl+Shift+S" ToolTip="Closes all open Windows"/>
            <Separator/>
            <MenuItem Header="{lex:Loc Project75:lang:mnuFileMenuClose}" Command="{Binding Path=CloseDocumentCommand}" InputGestureText="Ctrl+F4"/>
            <MenuItem Header="{lex:Loc Project75:lang:mnuFileMenuCloseAll}" Command="{Binding Path=CloseAllDocumentsCommand}" InputGestureText="Ctrl+Shift+F4"/>
            <Separator/>
            <MenuItem Header="{lex:Loc Project75:lang:mnuFileMenuExit}" Command="{Binding Path=ExitCommand}" InputGestureText="Alt+F4"/>
        </MenuItem>
        <MenuItem Header="{lex:Loc Project75:lang:mnuHelpMenu}">
            <MenuItem Header="{lex:Loc Project75:lang:mnuHelpMenuAbout}" Command="{Binding Path=AboutCommand}"/>
        </MenuItem>
</Menu>

我假设您同时也想考虑到修饰键 (Ctrl、Shift、Alt)? - jamesthollowell
当我执行这个操作 <MenuItem Header="打开" InputGestureText="Ctrl+I" /> 它会在标题旁边显示快捷键。问题出在哪里?如果我给菜单项分配一个已定义了输入手势的命令,情况也是一样的。你可能定义了一个自定义模板来覆盖默认样式吗? - floele
1个回答

2

试试这个:

<Window.Resources>
    <ControlTemplate TargetType="MenuItem" x:Key="controlTemplate">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Header}"></TextBlock>
            <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=InputGestureText}" Margin="5,0,0,0"></TextBlock>
        </StackPanel>
    </ControlTemplate>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <Menu>
        <MenuItem Header="Foo">
            <MenuItem Header="Bar" Command="Copy" Template="{StaticResource controlTemplate}"></MenuItem>
        </MenuItem>
    </Menu>
</Grid>

我以复制命令为例,但你可以使用任何其他命令。标题可以本地化,就像你现在正在做的一样。


你差不多就成功了,只需要做出一些小改动。第二个TextBlock中的Path只需要更改为Path=InputGestureText,它就像魔术一样奏效了。非常感谢。 - user275683
你可以根据Vladimir的问题更新你的回答。 - Bill Woodger

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接