开发者

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:

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

<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>
     开发者_StackOverflow中文版       </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 AltC, F, 0 for selecting font size 10 (it's just the 0 bit that no longer works, the first two bits are still fine).

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:

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. 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.

<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> 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜