2011-11-22 33 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