简体   繁体   English

在WPF中,如何在保持键盘快捷键的同时右对齐菜单项?

[英]In WPF, how do I right justify menu items while keeping keyboard shortcuts?

I recently changed my XAML in order to gain the capability to right-justify menu items such as setting font sizes in the below graphic: 我最近更改了我的XAML,以获得右对齐菜单项的功能,例如在下图中设置字体大小:

File  Configure  Help
      +-------------+  +----+
      | Font size > |->|  8 |
      | Speed     > |  | 10 |
      +-------------+  | 12 |
                       +----+

Obviously, based on that graphic, I won't be moving from a technical role into graphic design anytime soon :-) It's more just to illustrate what I meant. 显然,基于该图形,我不会很快从技术角色转变为平面设计:-)更多只是为了说明我的意思。

The original XAML below did not right-justify the values 下面的原始XAML没有对这些值进行右对齐

<MenuItem Header="_Configure">
    <MenuItem Header="_Font size   ">
        <MenuItem Header="_8" Click="menuConfigFontSz8" />
        <MenuItem Header="1_0" Click="menuConfigFontSz10" />
        <MenuItem Header="1_2" Click="menuConfigFontSz12" />
    </MenuItem>
    :
</MenuItem>

Instead, it gave me: 相反,它给了我:

File  Configure  Help
      +-------------+  +----+
      | Font size > |->| 8  |
      | Speed     > |  | 10 |
      +-------------+  | 12 |
                       +----+

So, to get right-justification, I changed it to: 因此,为了获得正确的理由,我将其更改为:

<MenuItem Header="_Configure">
    <MenuItem Header="_Font size   ">
        <MenuItem Click="menuConfigFontSz8">
            <MenuItem.Header>
                <TextBlock HorizontalAlignment="Right">_8</TextBlock>
            </MenuItem.Header>
        </MenuItem>
        <MenuItem Click="menuConfigFontSz10">
            <MenuItem.Header>
                <TextBlock HorizontalAlignment="Right">1_0</TextBlock>
            </MenuItem.Header>
        </MenuItem>
        <MenuItem Click="menuConfigFontSz12">
            <MenuItem.Header>
                <TextBlock HorizontalAlignment="Right">1_2</TextBlock>
            </MenuItem.Header>
        </MenuItem>
    </MenuItem>
    :
</MenuItem>

However, I find I've lost the shortcut capability of doing Alt C , F , 0 for selecting font size 10 (it's just the 0 bit that no longer works, the first two bits are still fine). 但是,我发现我已经失去了使用Alt CF0来选择字体大小10的快捷功能(它只是0位不再有效,前两位仍然很好)。

Instead it gives me the literal text 1_0 in the menu itself rather than allowing me to use 0 as a quick way of selecting the item: 相反,它给了我菜单本身的文字文本1_0 ,而不是允许我使用0作为选择项目的快捷方式:

File  Configure  Help
      +-------------+  +-----+
      | Font size > |->|  _8 |
      | Speed     > |  | 1_0 |
      +-------------+  | 1_2 |
                       +-----+

How do I keep the right justification of the menu text but still allow for an accelerator? 如何保持菜单文本的正确理由,但仍允许使用加速器?

When you add an accelerator to a MenuItem (and Button) WPF automatically adds a TextBlock to your MenuItem, and this probably prevents your code from working. 当您向MenuItem(和Button)添加加速器时,WPF会自动将TextBlock添加到您的MenuItem,这可能会阻止您的代码工作。 You can see this effect if you take a look at the answer to this question . 如果你看看这个问题答案 ,你可以看到这种效果。

This code solves the problem if you do not have a generic TextBlock style in your resources that overrides the default TextBlock behaviour. 如果您的资源中没有覆盖默认TextBlock行为的通用TextBlock样式,则此代码可以解决此问题。

<MenuItem Header="_Configure"> 
  <MenuItem Header="_Font size"> 
    <MenuItem Click="menuConfigFontSz8" Header="_8" HorizontalAlignment="Right"/>
    <MenuItem Click="menuConfigFontSz10" Header="1_0" HorizontalAlignment="Right"/>
    <MenuItem Click="menuConfigFontSz12" Header="1_2" HorizontalAlignment="Right"/>
</MenuItem> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM