在 WPF 中,如何在保留键盘快捷键的同时右对齐菜单项?
我最近更改了我的 XAML,以获得右对齐菜单项的功能,例如在下图中设置字体大小:
File Configure Help +-------------+ +----+ | Font size > |->| 8 | | Speed > | | 10 | +-------------+ | 12 | +----+
显然,基于该图形,我不会很快从技术角色转向图形设计: -)这只是为了说明我的意思。
下面的原始 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>
而是给了我:
File Configure Help +-------------+ +----+ | Font size > |->| 8 | | Speed > | | 10 | +-------------+ | 12 | +----+
因此,为了获得右对齐,我将其更改为:
<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>
但是,我发现我已经失去了执行 AltC、F、0 用于选择字体大小 10(只是 0
位不再起作用,前两位仍然没问题)。
相反,它在菜单本身中为我提供了文字文本 1_0
,而不是允许我使用 0
作为选择项目的快速方法:
File Configure Help +-------------+ +-----+ | Font size > |->| _8 | | Speed > | | 1_0 | +-------------+ | 1_2 | +-----+
How do I keep the right justification菜单文本但仍然允许加速器?
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>
</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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您向 MenuItem(和 Button)添加加速器时,WPF 会自动向您的 MenuItem 添加 TextBlock,这可能会阻止您的代码工作。如果您查看回答此问题。
如果您的资源中没有覆盖默认 TextBlock 行为的通用 TextBlock 样式,则此代码可以解决该问题。
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.