【问题标题】:How to disable dropdown button in WPF RibbonSplitButton control如何在 WPF RibbonSplitButton 控件中禁用下拉按钮
【发布时间】:2022-03-23 05:15:54
【问题描述】:

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

提前致谢。

【问题讨论】:

    标签: wpf ribbon split-button


    【解决方案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}"/>
    

    【讨论】:

    • 请注意;这种方法的一个问题是,当 IsToggleButtonEnabled 为 false 时,按钮上的任何文本都将呈现为禁用(灰色)。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-13
      • 1970-01-01
      • 2011-04-20
      • 2012-04-22
      • 2021-05-18
      相关资源
      最近更新 更多