RibbonControlsLibrary - how to disable minimizing?

2019-04-11 15:59发布

问题:

How to disable minimizing of Ribbon control from RibbonControlsLibrary?

回答1:

The following disabled both the double click on the tab header and 'Minimize the Ribbon' on the context menu for me:

public class ExRibbon : Ribbon
{
    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        IsMinimizedProperty.OverrideMetadata(typeof(ExRibbon),
                new FrameworkPropertyMetadata(false, (o, e) => { }, (o, e) => false));

        Type ownerType = typeof(ExRibbon);
        CommandManager.RegisterClassCommandBinding(ownerType,
            new CommandBinding(RibbonCommands.MinimizeRibbonCommand, null, MinimizeRibbonCanExecute));
    }

    private static void MinimizeRibbonCanExecute(object sender, CanExecuteRoutedEventArgs args)
    {
        args.CanExecute = false;
        args.Handled = true;
    }
}


回答2:

public class ExRibbon : Ribbon
{
    public override void OnApplyTemplate()
    {
         base.OnApplyTemplate();

         if (!IsMinimizable)
         {
              IsMinimizedProperty.OverrideMetadata(typeof(ExRibbon), 
                   new FrameworkPropertyMetadata(false, (o, e) => { }, (o,e) => false));
         }
    }
    public bool IsMinimizable { get; set; }
}


回答3:

The only way that minimices the control and can't be disabled is a double click on a Tab header, in fact a triple click or more than 2 clicks also minimices the control, this is why my first idea failed (I tryed to cancel the double click event, but the control minimized on the third click).

SO this solution isn't too prety but it works fine, when more than two clicks are detected on the TabHeaderItemsControl (this is the control that holds the tabs) then the control is maximized

public class MinimizableRibbon : Ribbon
{
    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        RibbonTabHeaderItemsControl tabItems = this.FindName("TabHeaderItemsControl") as RibbonTabHeaderItemsControl;

        int lastClickTime = 0;
        if (tabItems != null)
            tabItems.PreviewMouseDown += (_, e) =>
                {
                    // A continuous click was made (>= 2 clicks minimizes the control)
                    if (Environment.TickCount - lastClickTime < 300)
                        // here the control should be minimized
                        if (!IsMinimizable)
                            IsMinimized = false;

                    lastClickTime=Environment.TickCount;
                };
    }

    public bool IsMinimizable { get; set; }
}