如何在WPF RibbonSplitButton控件中禁用下拉按钮

时间:2011-11-22 14:34:17

标签: wpf ribbon split-button

我正在使用WPF功能区4.我有一个RibbonSplitButton控件,其中包含菜单项的下拉菜单。 当我将IsEnabled的{​​{1}}属性设置为false时,只有顶部按钮被禁用,而不是打开下拉菜单的按钮。

提前致谢。

1 个答案:

答案 0 :(得分:1)

我通过创建自己的拆分按钮解决了这个问题,继承了RibbonSplitButton并添加了一个我可以绑定的依赖项属性,以便单独启用或禁用拆分按钮。

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

并在XAML中:

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