如何动态隐藏/禁用WPF功能区选项卡

本文关键字:WPF 禁用 功能区 选项 隐藏 何动态 动态 | 更新日期: 2023-09-27 18:06:59

假设我有一个ribbon选项卡名称a (name="_tabA")和B (name="_tabB")。如何动态禁用或隐藏选项卡A或B ?

我使用VS2010与RibbonControlsLibrary.dll .

如何动态隐藏/禁用WPF功能区选项卡

<ribbon:RibbonTab Visibility="{Binding ShowThisRibbonTab, Converter=...}">

其中ShowThisRibbonTab是您的ViewModel的属性和转换器是最有可能的BooleanToVisibilityConverter

或者,如果你不做MVVM,你可以给它一个名字并设置可见度

无MVVM

我可以很容易地隐藏/显示_tabA.Visibility = Visibility.CollapsedVisibility.Visible

使用MVVM

. example .cs代码
  1. 让类也继承INotifyProperty
  2. 创建一个属性,当属性被修改时引发事件
  3. 设置DataContext。
  4. 生成转换器代码。

主代码如下

public partial class MainWindow : RibbonWindow, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
    }
    public const string NamePropertyName = "VisibleA"; 
    private bool _visibleA = true;
    public bool VisibleA
    {
        get
        {
            return _visibleA;
        }
        set
        {
            _visibleA = value;
            RaisePropertyChanged(NamePropertyName); 
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    } 
}

转换器代码如下

[ValueConversion(typeof(bool), typeof(Visibility))]
internal class CheckVisibleA : System.Windows.Data.IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool? val = value as bool?;
        string param = parameter as string;
        if (value != null)
        {
            if (val == true)
            {
                return Visibility.Visible;
            }
        }
        return Visibility.Collapsed;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
.xaml代码这个XAML有两个ribbon选项卡:_ribboHome和_ribbonHelp。"VisibleA"的属性控制可见性。当我点击复选框时,VisibleA属性打开/关闭,_ribbonHome相应地可见/折叠。
<ribbon:Ribbon DockPanel.Dock="Top" Title="teusje.wordpress.com" >
    <ribbon:RibbonTab Header="Home" Name="_ribbonHome" Visibility="{Binding Path=VisibleA, Converter={StaticResource CheckVisibleA}, ConverterParameter=Show}">
        <ribbon:RibbonGroup Name="Clipboard" Header="Clipboard">
            <ribbon:RibbonButton Command="{StaticResource hwc}" CommandParameter="Hello, smcho" Label="Copy" LargeImageSource="Images/LargeIcon.png" /> 
        </ribbon:RibbonGroup>
    </ribbon:RibbonTab>
    <ribbon:RibbonTab Header="Help">
        <ribbon:RibbonGroup Name="_ribbonHelp" Header="Help this">
            <ribbon:RibbonButton Command="{StaticResource hwc}" CommandParameter="Hello, smcho" Label="Copy Help" LargeImageSource="Images/LargeIcon.png"/>
            <ribbon:RibbonCheckBox IsChecked="{Binding Path=VisibleA}"/>
        </ribbon:RibbonGroup>
    </ribbon:RibbonTab>