RibbonApplicationMenu:摆脱 AuxiliaryPane

发布于 2024-12-14 03:53:27 字数 1637 浏览 7 评论 0原文

碰巧我正在开发的应用程序不对文档进行操作,因此不需要在应用程序菜单中显示最近打开的文档列表。
但是 - 令人烦恼的是 - 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风流物 2024-12-21 03:53:27

我知道这已经有一段时间了,但我有另一个解决方案。这个不提供 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);
        }
    }
}

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);
        }
    }
}
遗忘曾经 2024-12-21 03:53:27

为我工作

<telerik:ApplicationMenu RightPaneVisibility="Collapsed" >

worked for me

<telerik:ApplicationMenu RightPaneVisibility="Collapsed" >
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文