WPF功能区上下文选项卡可见性绑定 [英] WPF Ribbon Contextual Tab Visibility binding

查看:150
本文介绍了WPF功能区上下文选项卡可见性绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现很难找到绑定

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

以下是我的WPF代码的摘要.我正在寻找一种将Visibility属性绑定到MainWindow.Port是否为null的解决方案.

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>

推荐答案

您可以创建一个Converter IsNotNullToVisibilityConverter

You can create a Converter IsNotNullToVisibilityConverter

使用类似的Convert方法:

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

然后将其放入您的XAML

And then put it in your XAML

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

在您后面的代码中:

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

这篇关于WPF功能区上下文选项卡可见性绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆