RibbonApplicationMenu:摆脱 AuxiliaryPane
碰巧我正在开发的应用程序不对文档进行操作,因此不需要在应用程序菜单中显示最近打开的文档列表。
但是 - 令人烦恼的是 - RibbonApplicationMenu 类中没有现成的属性来隐藏未使用的 AuxiliaryPane (奇怪的是,该属性确实存在,但被标记为“内部”) ”)。
当然,我可以把它留在那里——但那样……不整洁。
所以,这是我想出的解决方案。
希望它对其他人有帮助:-)
总体思路是对 RibbonApplicationMenu
进行子类化,找到与菜单的 Popup 相对应的模板子项,并推翻其 Width
(之后经过一系列令人沮丧的实验,很明显,无论是为 PART_AuxiliaryPaneContentPresenter
还是为 PART_FooterPaneContentPresenter
都这样做 - 也不为两者 - 都可以实现任何目标)。
好吧,言归正传,代码如下:
public class SlimRibbonApplicationMenu : RibbonApplicationMenu
{
private const double DefaultPopupWidth = 180;
public double PopupWidth
{
get { return (double)GetValue(PopupWidthProperty); }
set { SetValue(PopupWidthProperty, value); }
}
public static readonly DependencyProperty PopupWidthProperty =
DependencyProperty.Register("PopupWidth", typeof(double),
typeof(SlimRibbonApplicationMenu), new UIPropertyMetadata(DefaultPopupWidth));
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
this.DropDownOpened +=
new System.EventHandler(SlimRibbonApplicationMenu_DropDownOpened);
}
void SlimRibbonApplicationMenu_DropDownOpened(object sender, System.EventArgs e)
{
DependencyObject popupObj = base.GetTemplateChild("PART_Popup");
Popup popupPanel = (Popup)popupObj;
popupPanel.Width = (double)GetValue(PopupWidthProperty);
}
}
作为旁注,我试图找到任何方法来根据 ApplicationMenu 项目的最大宽度解析所需的宽度(而不是通过 XAML 中的 DependencyProperty 显式设置它) - 但要没用。
鉴于我对“神奇数字”的鄙视,任何对此的建议都将受到深深的赞赏。
It so happened that the application I'm working on doesn't operate on documents, so there's no need in displaying the recently opened documents list in the application menu.
But - annoyingly - there are no properties readily available in the RibbonApplicationMenu
class to hide the unused AuxiliaryPane
(for which, curiously, the property does exist, but is marked as "internal").
Of course, I can just leave it there - but that's... untidy.
So, here's the solution I came up with.
Hope it will be helpful for anyone else :-)
The general idea is to subclass the RibbonApplicationMenu
, find the template child corresponding to the menu's Popup, and overrule its Width
(after a number of frustrating experiments it became evident that doing that neither for PART_AuxiliaryPaneContentPresenter
nor for PART_FooterPaneContentPresenter
- nor for the both - could achieve anything).
Well, without further ado, here's the code:
public class SlimRibbonApplicationMenu : RibbonApplicationMenu
{
private const double DefaultPopupWidth = 180;
public double PopupWidth
{
get { return (double)GetValue(PopupWidthProperty); }
set { SetValue(PopupWidthProperty, value); }
}
public static readonly DependencyProperty PopupWidthProperty =
DependencyProperty.Register("PopupWidth", typeof(double),
typeof(SlimRibbonApplicationMenu), new UIPropertyMetadata(DefaultPopupWidth));
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
this.DropDownOpened +=
new System.EventHandler(SlimRibbonApplicationMenu_DropDownOpened);
}
void SlimRibbonApplicationMenu_DropDownOpened(object sender, System.EventArgs e)
{
DependencyObject popupObj = base.GetTemplateChild("PART_Popup");
Popup popupPanel = (Popup)popupObj;
popupPanel.Width = (double)GetValue(PopupWidthProperty);
}
}
As a side note, I tried to find any way to resolve the desired width based on the max width of the ApplicationMenu's Items (rather than setting it explicitly through the DependencyProperty in XAML) - but to no avail.
Given my despise to "magic numbers", any suggestion on that will be deeply appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道这已经有一段时间了,但我有另一个解决方案。这个不提供 Popup 宽度属性,而是提供 ShowAuxilaryPanel 布尔值。然后将弹出窗口的宽度绑定到菜单的菜单项区域的宽度。
I know this has been a while, but I've got another solution to this. This one does not provide the Popup width property, instead a ShowAuxilaryPanel boolean. It then goes to Bind the width of the Popup, to the width of the menu item area of the menu.
为我工作
worked for me