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