WPF Ribbon Contextual Tab Visibility binding

亡梦爱人 提交于 2019-12-11 18:56:56

问题


I am finding it surprisingly hard to find examples of binding the visibility of a RibbonContextualTabGroup. I have a property in my code-behind that should decide when to display a ribbon tab, but everything I've tried so far has no effect. My code-behind is essentially:

public partial class MainWindow : RibbonWindow
{
    public string Port { get; set; }
}

A summary of my WPF code is below. I'm looking for a solution that binds the Visibility property to whether or not MainWindow.Port is null.

<ribbon:RibbonWindow
    ...
    xmlns:src="clr-namespace:MagExplorer" />

    ...

    <ribbon:RibbonTab x:Name="COMTab" 
                      Header="COM"
                      ContextualTabGroupHeader="Communications">
    ...
    </ribbon:RibbonTab>

    <ribbon:Ribbon.ContextualTabGroups>
        <ribbon:RibbonContextualTabGroup Header="Communications"
                                         Visibility="<What goes here?>" />
    </ribbon:Ribbon.ContextualTabGroups>

回答1:


You can create a Converter IsNotNullToVisibilityConverter

with the Convert method like this:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is string)
        {
            if (!string.IsNullOrEmpty((string)value))
                return Visibility.Visible;
        }
        else if (value != null)
        {
            return Visibility.Visible;
        }

        return Visibility.Collapsed;
    }

And then put it in your XAML

<Window.Resources>
    <IsNotNullToVisibilityConverter x:Key="IsNotNullToVisibilityConverter" />
</Window.Resources>
...
Visibility="{Binding Path=Port, Converter={StaticResource IsNotNullToVisibilityConverter}}" 

In your code behind:

public static readonly DependencyProperty PortProperty =
        DependencyProperty.Register
        ("Port", typeof(String), typeof(NameOfYourClass),
        new PropertyMetadata(String.Empty));

public String Port
    {
        get { return (String)GetValue(PortProperty); }
        set { SetValue(PortProperty, value); }
    }


来源:https://stackoverflow.com/questions/24126430/wpf-ribbon-contextual-tab-visibility-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!