How to disable dropdown button in WPF RibbonSplitButton control

2.5k Views Asked by At

I am using WPF Ribbon 4. I have a RibbonSplitButton control with dropdown menu of menu items. When I set IsEnabled property of RibbonSplitButton to false only top button becomes disabled, not the button which opens dropdown menu.

Thanks in advance.

2

There are 2 best solutions below

0
On

You can simply add a DropDownOpened="RibbonMenuButton_OnDropDownOpened" to the WPF and then

    private void RibbonMenuButton_OnDropDownOpened(object sender, EventArgs e)
    {
        var rsb = sender as RibbonSplitButton;
        if (rsb == null) return;
        if (DataContext is GameCardViewModel vm)
        {
            rsb.IsDropDownOpen = vm.EasyInputMode;
        }
    }
1
On

I solved this problem by creating my own split button, inheriting from RibbonSplitButton and adding an dependency property that I can bind to for enabling or disabling the split button alone.

public class MyRibbonSplitButton : RibbonSplitButton
{
    public MyRibbonSplitButton()
        : base()
    {
    }

    /// <summary>
    /// Gets or sets a value indicating whether the toggle button is enabled.
    /// </summary>
    /// <value><c>true</c> if the toggle button should be  enabled; otherwise, <c>false</c>.</value>
    public bool IsToggleButtonEnabled
    {
        get { return (bool)GetValue(IsToggleButtonEnabledProperty); }
        set { SetValue(IsToggleButtonEnabledProperty, value); }
    }

    /// <summary>
    /// Identifies the <see cref="IsToggleButtonEnabled"/> dependency property
    /// </summary>
    public static readonly DependencyProperty IsToggleButtonEnabledProperty =
        DependencyProperty.Register(
            "IsToggleButtonEnabled", 
            typeof(bool), 
            typeof(MyRibbonSplitButton), 
            new UIPropertyMetadata(true, new PropertyChangedCallback(MyRibbonSplitButton.ToggleButton_OnIsEnabledChanged)));

    /// <summary>
    /// Handles the PropertyChanged event for the IsToggleButtonEnabledProperty dependency property
    /// </summary>
    private static void ToggleButton_OnIsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        var button = sender as MyRibbonSplitButton;

        var toggleButton = button.GetTemplateChild("PART_ToggleButton") as RibbonToggleButton;
        toggleButton.IsEnabled = (bool)e.NewValue;
    }
}

and in XAML:

   <local:MyRibbonSplitButton Label="New" Command="{Binding SomeCommand}" 
                          LargeImageSource="Images/Large/New.png"
                          ItemsSource="{Binding Templates}"
                          IsToggleButtonEnabled="{Binding HasTemplates}"/>