1

I've got next makrup:

<Style TargetType="{x:Type MenuItem}">
<Setter Property="MinWidth" Value="65" />
<Setter Property="MinHeight" Value="30" />
<Setter Property="IsTabStop" Value="False" />
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type MenuItem}">
            <Border x:Name="MainBorder" BorderThickness="1" Background="Black">                 
                <TextBlock Margin="5" Text="{TemplateBinding Header}" Foreground="{TemplateBinding Foreground}" />

                <Popup x:Name="SubMenuPopup" IsOpen="{Binding Path=IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" Placement="Right"
                            AllowsTransparency="True" Focusable="False">
                    <Border Background="Gray">
                        <Grid x:Name="SubMenu" Grid.IsSharedSizeScope="True" Background="Transparent">
                            <StackPanel Margin="0" IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle" Background="Gray" />
                        </Grid>
                    </Border>
                </Popup>
            </Border>
        </ControlTemplate>
    </Setter.Value>
</Setter>

When I create MenuItem somewhere and set it's Header property with "_" symbol - it doesn't create shortcut for this menu item. Example - letter 'F' is not underlined and shortcut doesn't work.

How to support shortcuts in ControlTemplates in MenuItems?

Thanks.

4

2 回答 2

3

它不是完整的模板,而是放置一个可以识别访问密钥的 ContentPresenter 而不是 TextBlock:

<ContentPresenter Margin="5" Content="{TemplateBinding Header}" TextBlock.Foreground="{TemplateBinding Foreground}" RecognizesAccessKey="True" />

我想您在此处粘贴的 xaml 只是您的实现的一部分,所以我的解决方案只是您继续使用访问密钥工作......

您可以像这样找到整个模板:http: //msdn.microsoft.com/en-us/library/ms747082%28v=vs.85%29.aspx

于 2013-09-10T14:32:47.727 回答
0

如果你想让你的菜单项字母在按键本身下划线,那么你必须像这样在 menuitem 上设置 InputGesture:

      <MenuItem Header="_File" 
          InputGestureText="Ctrl+F"
          Commmand={Binding NewFileCommand}/>

但是,如果您想为 menuitem 命令创建快捷方式,则必须在窗口中创建如下命令绑定:

      <Window.CommandBindings>
                <CommandBinding Command="local:MyCommands.NewFile" Executed="NewFile_Executed" />
        </Window.CommandBindings>
        <Window.InputBindings>
                <KeyBinding Key="F" Modifiers="Control" Command="local:MyCommands.NewFile"/>
        </Window.InputBindings>
于 2013-09-10T14:47:04.657 回答