【发布时间】:2022-03-23 05:15:54
【问题描述】:
我正在使用 WPF Ribbon 4。我有一个带有菜单项下拉菜单的 RibbonSplitButton 控件。
当我将RibbonSplitButton 的IsEnabled 属性设置为false 时,只有顶部按钮被禁用,而不是打开下拉菜单的按钮。
提前致谢。
【问题讨论】:
标签: wpf ribbon split-button
我正在使用 WPF Ribbon 4。我有一个带有菜单项下拉菜单的 RibbonSplitButton 控件。
当我将RibbonSplitButton 的IsEnabled 属性设置为false 时,只有顶部按钮被禁用,而不是打开下拉菜单的按钮。
提前致谢。
【问题讨论】:
标签: wpf ribbon split-button
我通过创建自己的拆分按钮解决了这个问题,从 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}"/>
【讨论】:
您可以简单地将 DropDownOpened="RibbonMenuButton_OnDropDownOpened" 添加到 WPF,然后
private void RibbonMenuButton_OnDropDownOpened(object sender, EventArgs e)
{
var rsb = sender as RibbonSplitButton;
if (rsb == null) return;
if (DataContext is GameCardViewModel vm)
{
rsb.IsDropDownOpen = vm.EasyInputMode;
}
}
【讨论】: