1
我正在使用WPF功能區4.我有一個RibbonSplitButton
控制下拉菜單的菜單項。 當我將RibbonSplitButton
的IsEnabled
屬性設置爲false時,只有頂部按鈕變爲禁用,而不是打開下拉菜單的按鈕。如何禁用WPF RibbonSplitButton控件中的下拉按鈕
在此先感謝。
我正在使用WPF功能區4.我有一個RibbonSplitButton
控制下拉菜單的菜單項。 當我將RibbonSplitButton
的IsEnabled
屬性設置爲false時,只有頂部按鈕變爲禁用,而不是打開下拉菜單的按鈕。如何禁用WPF RibbonSplitButton控件中的下拉按鈕
在此先感謝。
我通過創建自己的分割按鈕解決了這個問題,繼承自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}"/>
只是擡起頭來;這種方法的一個障礙是,當IsToggleButtonEnabled爲false時,按鈕上的任何文本都會呈現爲禁用(灰色)。 – eilef