如何在 RibbonApplicationMenu 的顶部设置文本
我试图在 RibbonApplicationMenu 的顶层添加文本(尝试在此处获取单词 File
,类似于 Word 或 Outlook)。似乎是 Microsoft.Windows.Controls.Ribbon.RibbonApplicationMenu
支持 SmallImageSource
但不支持文本属性。设置 Label
属性无法解决此问题。
xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
<ribbon:RibbonApplicationMenu Label="File"><!--doesn't set the label -->
</ribbon:RibbonApplicationMenu>
目标是让“文件”一词出现在下面的圆圈区域中。
I'm trying to have text in the top level of a RibbonApplicationMenu (trying to the get the word File
there similar to Word or Outlook). It seems the Microsoft.Windows.Controls.Ribbon.RibbonApplicationMenu
supports a SmallImageSource
but no text property. Setting the Label
property doesn't work for this problem.
xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
<ribbon:RibbonApplicationMenu Label="File"><!--doesn't set the label -->
</ribbon:RibbonApplicationMenu>
The goal is to have the word "File" appear in the circled area below.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
最简单的解决方案(对我来说)是插入 DrawingImage 带有 GlyphRun 内。在单独的帖子上询问如何获取 GlyphRun 的 AdvanceWidths 和 GlyphIndicies 。结果位于
结果功能区下方:
The simplest solution (to me) was to insert a DrawingImage with a GlyphRun inside. On a separate post is asked how to get the AdvanceWidths and GlyphIndicies for the GlyphRun. The result is below
Resulting Ribbon:
删除可视化树中不需要的元素,并将它们替换为从 Label 属性获取文本的 TextBlock。您必须对主视觉树上的按钮和弹出窗口的视觉树上的按钮执行此操作。最后,由于文本比典型图像更复杂,因此减少航空突出效果会很有帮助。
要使用以下代码,请在 XAML 中为应用程序菜单分配一个名称,并从窗口的 Loaded 事件处理程序中使用该名称调用
ReplaceRibbonApplicationMenuButtonContent
。Remove the unwanted elements for the visual tree, and replace them with a TextBlock that takes the text from the Label property. You have to do this for both the button on the main visual tree and on the popup's visual tree. Finally, since text is more intricate than the typical image, it is helpful to back off on the aero highlighting effects.
To use the following code, assign a name to the application menu in the XAML and call
ReplaceRibbonApplicationMenuButtonContent
with it from the Loaded event handler of the window.正确的。如果您不需要隐藏代码且不需要复杂的字形计算,请使用以下 XAML:
此方法的优点:
Right. If you want no code-behind and no complex glyph computations, use the following XAML:
Advantages of this approach:
棘手!您可能必须将模板的 PART_ToggleButton 替换为您自己的版本才能设置文本。
使用WPF Vizualizer显示模板包含一个带有图像和路径(DownArrow)的StackPanel,但没有TextBlock,所以我怀疑当前控件中有一个地方可以指定标签文本。
当然,您也可以创建包含所需文本的图像。
Tricky! You might have to replace the PART_ToggleButton of the template with your own version to be able to set the text.
Using the WPF Vizualizer shows that the template contains a StackPanel with an Image and a Path (DownArrow), but no TextBlock, so I doubt that there is a place in the current control to specify the label text.
Of course, you could also create an image that contains the desired text.
另一种方法是使用网格并在正确的位置绘制 TextBlock。请务必使 TextBlock 不为 HitTestVisible。
优点:
缺点:
- 在 Windows XP 上看起来不太好
Another way to do so is just using a grid and paint a TextBlock at the right place. Be sure to make the TextBlock NOT HitTestVisible.
Advantages:
Disadvantage:
- looks less nice on Windows XP
发布了以下解决方案 MSDN 论坛。它涉及更改默认(?)主题中使用的样式。
论坛帖子确实包含修改后的源的 ZIP,但链接不再有效。
The following solution was posted on an MSDN forum. It involves altering the style used in the default (?) theme.
The forum post did include a ZIP of the modified sources, but the link no longer works.