如何禁用 WPF RibbonSplitButton 控件中的下拉按钮

发布于 2024-12-17 19:21:07 字数 160 浏览 3 评论 0原文

我正在使用 WPF Ribbon 4。我有一个带有菜单项下拉菜单的 RibbonSplitButton 控件。 当我将 RibbonSplitButton 的 IsEnabled 属性设置为 false 时,仅顶部按钮被禁用,而不是打开下拉菜单的按钮。

提前致谢。

I am using WPF Ribbon 4. I have a RibbonSplitButton control with dropdown menu of menu items.
When I set IsEnabled property of RibbonSplitButton to false only top button becomes disabled, not the button which opens dropdown menu.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

莫相离 2024-12-24 19:21:07

我通过创建自己的分割按钮、继承 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}"/>

I solved this problem by creating my own split button, inheriting from RibbonSplitButton and adding an dependency property that I can bind to for enabling or disabling the split button alone.

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;
    }
}

and in XAML:

   <local:MyRibbonSplitButton Label="New" Command="{Binding SomeCommand}" 
                          LargeImageSource="Images/Large/New.png"
                          ItemsSource="{Binding Templates}"
                          IsToggleButtonEnabled="{Binding HasTemplates}"/>
无畏 2024-12-24 19:21:07

您可以简单地将 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;
        }
    }

You can simply add a DropDownOpened="RibbonMenuButton_OnDropDownOpened" to the WPF and then

    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;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文