1

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.

ninjalj
  • 42,493
  • 9
  • 106
  • 148
user991288
  • 11
  • 4

2 Answers2

1

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}"/>
eilef
  • 73
  • 5
  • Just a heads up; a snag with this approach is that any text on the button will render as disabled (grayed) when IsToggleButtonEnabled is false. – eilef Mar 28 '12 at 10:14
0

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;
        }
    }