C# 如何在XAML中显示菜单项旁边的键盘快捷键
我在XAML中使用C# 如何在XAML中显示菜单项旁边的键盘快捷键,c#,xaml,C#,Xaml,我在XAML中使用MenuItem,使用InputGestureText我在菜单项中添加了键盘快捷键。我所困惑的是如何在实际菜单中,在每个具有键盘快捷键的项目旁边显示键盘快捷键 我不想将文本添加到标题,而是想显示InputGestureText的值,因此可以在不修改标题的情况下对其进行更改 我正在使用 菜单的代码 <Menu Grid.Row="0" Height="30" > <MenuItem Header="{lex:Loc Project75:lang:
MenuItem
,使用InputGestureText
我在菜单项中添加了键盘快捷键。我所困惑的是如何在实际菜单中,在每个具有键盘快捷键的项目旁边显示键盘快捷键
我不想将文本添加到标题
,而是想显示InputGestureText的值
,因此可以在不修改标题的情况下对其进行更改
我正在使用
菜单的代码
<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>
试试这个:
<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>
我使用copy命令作为示例,但您可以使用任何其他命令。标题可以按您的操作进行本地化。我假设您也要考虑修改器(Ctrl、Shift、Alt)?执行此操作时,它会在标题旁边显示快捷方式。有什么问题?如果我将命令分配给定义了Input手势的菜单项,则同样适用。您是否可能为MenuItem定义了一个覆盖默认样式的自定义模板?您几乎拥有了它,只需对其进行一些小的更改<代码>路径第二个
文本块
必须更改为Path=InputGestureText
,它就像魔术一样工作。非常感谢。你可以更新你的答案来回答弗拉基米尔的问题。