1

我正在使用 WPF Ribbon 4。我有一个RibbonSplitButton带有菜单项下拉菜单的控件。当我将IsEnabled属性设置RibbonSplitButton为 false 时,只有顶部按钮被禁用,而不是打开下拉菜单的按钮。

提前致谢。

4

1 回答 1

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}"/>
于 2012-02-29T07:45:44.517 回答