开发者

How can I hide/disable WPF ribbon tab dynamically?

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.


<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


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜