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

发布于 2024-12-02 07:40:25 字数 130 浏览 1 评论 0原文

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

我将 VS2010 与 RibbonControlsLibrary.dll 一起使用。

Let's say I have a ribbon tab name A (name="_tabA") and B (name="_tabB").
How can I disable or hide tab A or B dynamically?

I use VS2010 with RibbonControlsLibrary.dll.

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

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

发布评论

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

评论(2

清君侧 2024-12-09 07:40:25
<ribbon:RibbonTab Visibility="{Binding ShowThisRibbonTab, Converter=...}">

其中 ShowThisRibbonTab 是 ViewModel 的属性,而 Converter 很可能是 BooleanToVisibilityConverter

或者,如果您不使用 MVVM,您可以只给它一个名称并设置 Visibility

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

Where ShowThisRibbonTab is a property of your ViewModel and the Converter is most likely a BooleanToVisibilityConverter.

Alternatively, if you're not doing MVVM, you can just give it a name and set the Visibility

深居我梦 2024-12-09 07:40:25

如果没有 MVVM,

我可以使用 _tabA.Visibility = Visibility.CollapsedVisibility.Visible 轻松隐藏/显示。

使用 MVVM

The .xaml.cs code

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

The .xaml code

此 XAML 有两个功能区选项卡:_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>

Without MVVM

I could easily hide/show with _tabA.Visibility = Visibility.Collapsed or Visibility.Visible.

With MVVM

The .xaml.cs code

  1. Make the class inherit also from INotifyProperty
  2. Make a property to raise event when property is modified
  3. Setup DataContext.
  4. Make Converter code.

The main code is as follows

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

The converter code is as follows

[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();
    }
}

The .xaml code

This XAML has two ribbon tabs: _ribboHome and _ribbonHelp. And the property of "VisibleA" controls the visibility. When I click the checkbox, the VisibleA property turns on/off, and the _ribbonHome is visbile/collapsed accordingly.

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