2011-11-22 129 views

回答

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}"/> 
+0

只是抬起头来;这种方法的一个障碍是,当IsToggleButtonEnabled为false时,按钮上的任何文本都会呈现为禁用(灰色)。 – eilef