RibbonApplicationMenu:摆脱 AuxiliaryPane [英] RibbonApplicationMenu: getting rid of the AuxiliaryPane

查看:21
本文介绍了RibbonApplicationMenu:摆脱 AuxiliaryPane的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

碰巧我正在开发的应用程序不对文档进行操作,所以没有必要在应用程序菜单中显示最近打开的文档列表.
但是 - 令人讨厌的是 - RibbonApplicationMenu 类中没有现成的属性来隐藏未使用的 AuxiliaryPane(奇怪的是,该属性确实存在,但被标记为内部").
当然,我可以把它留在那里——但那……不整洁.

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 :-)

总体思路是将 RibbonApplicationMenu 子类化,找到与菜单的 Popup 对应的模板子项,并否决其 Width(经过多次令人沮丧的实验后,它变得明显为 PART_AuxiliaryPaneContentPresenterPART_FooterPaneContentPresenter 这样做 - 也没有为两者 - 都可以实现任何目标).

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);
    }
}

作为旁注,我试图找到任何方法来根据 ApplicationMenu 项目的最大宽度(而不是通过 XAML 中的 DependencyProperty 明确设置它)来解决所需的宽度 - 但无济于事.
鉴于我对神奇数字"的鄙视,任何关于那个的建议将不胜感激.

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.

推荐答案

我知道这已经有一段时间了,但我有另一个解决方案.这个不提供 Popup width 属性,而是一个 ShowAuxilaryPanel 布尔值.然后将 Popup 的宽度绑定到菜单的菜单项区域的宽度.

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.

public class SlimRibbonApplicationMenu : RibbonApplicationMenu
{
    public bool ShowAuxilaryPanel
    {
        get { return (bool)GetValue(ShowAuxilaryPanelProperty); }
        set { SetValue(ShowAuxilaryPanelProperty, value); }
    }

    public static readonly DependencyProperty ShowAuxilaryPanelProperty =
        DependencyProperty.Register("ShowAuxilaryPanel", typeof(bool),
        typeof(SlimRibbonApplicationMenu), new UIPropertyMetadata(true));

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        this.DropDownOpened += SlimRibbonApplicationMenu_DropDownOpened;
    }

    void SlimRibbonApplicationMenu_DropDownOpened(object sender, EventArgs e)
    {
        DependencyObject popupObj = base.GetTemplateChild("PART_Popup");
        Popup panel = (Popup)popupObj;
        var exp = panel.GetBindingExpression(Popup.WidthProperty);

        if (!this.ShowAuxilaryPanel && exp == null)
        {
            DependencyObject panelArea = base.GetTemplateChild("PART_SubMenuScrollViewer");

            var panelBinding = new Binding("ActualWidth")
            {
                Source = panelArea,
                Mode = BindingMode.OneWay
            };
            panel.SetBinding(Popup.WidthProperty, panelBinding);
        }
        else if (this.ShowAuxilaryPanel && exp != null)
        {
            BindingOperations.ClearBinding(panel, Popup.WidthProperty);
        }
    }
}

这篇关于RibbonApplicationMenu:摆脱 AuxiliaryPane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆