RibbonApplicationMenu:摆脱 AuxiliaryPane

2023-12-15

碰巧我正在开发的应用程序不对文档进行操作,因此不需要在应用程序菜单中显示最近打开的文档列表。
但是 - 令人烦恼的是 - 没有现成的房产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 显式设置它) - 但无济于事。
鉴于我对“神奇数字”的鄙视,任何建议that将受到深深的赞赏。


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

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);
        }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

RibbonApplicationMenu:摆脱 AuxiliaryPane 的相关文章

随机推荐