3

How to disable minimizing of Ribbon control from RibbonControlsLibrary?

H.B.
  • 166,899
  • 29
  • 327
  • 400
Rover
  • 2,203
  • 3
  • 24
  • 44

3 Answers3

2

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;
    }
}
Boris Hurinek
  • 81
  • 1
  • 3
  • This works great except [according to MSDN](http://msdn.microsoft.com/en-us/library/ms597491%28v=vs.110%29.aspx), OverrideMetadata must be called in a static constructor. Otherwise the XAML designer wouldn't load my Ribbon. – Rotsiser Mho Dec 27 '14 at 03:43
1
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; }
}
Rover
  • 2,203
  • 3
  • 24
  • 44
  • I don't think this particular implementation is viable. [According to MSDN](http://msdn.microsoft.com/en-us/library/ms597491%28v=vs.110%29.aspx), OverrideMetadata must be called in a static constructor. Otherwise the XAML designer wouldn't load my Ribbon. – Rotsiser Mho Dec 27 '14 at 03:44
0

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; }
}
Ariel
  • 1,641
  • 1
  • 18
  • 27