【问题标题】:WPF Ribbon Contextual Tab Visibility bindingWPF 功能区上下文选项卡可见性绑定
【发布时间】:2014-06-09 18:22:22
【问题描述】:

我发现很难找到绑定RibbonContextualTabGroup 可见性的示例。我的代码隐藏中有一个属性应该决定何时显示功能区选项卡,但到目前为止我尝试的所有内容都没有效果。我的代码隐藏本质上是:

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

我的 WPF 代码摘要如下。我正在寻找将Visibility 属性绑定到MainWindow.Port 是否为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>

【问题讨论】:

    标签: c# wpf binding ribboncontrolslibrary


    【解决方案1】:

    您可以创建一个转换器 IsNotNullToVisibilityConverter

    使用这样的转换方法:

    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 中

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

    【讨论】:

    • 这与我尝试过的类似,但我无法让它工作。我尝试在Convert() 中添加Console.WriteLine("HERE"),但没有打印任何内容。还缺什么吗?
    • 如果没有调用Convert方法,可能是你的绑定不正确?您需要定义 ElementName (端口属性所在的位置)尝试将 RibbonWindow 放在顶部 x:Name="ctl" 然后绑定: Visibility="{Binding Path=Port, ElementName=ctl, Converter={ StaticResource IsNotNullToVisibilityConverter}}"
    • 哦等等,你的 Port 属性需要是一个 DependencyProperty 才能被绑定,我更新了我的答案
    猜你喜欢
    • 1970-01-01
    • 2011-12-08
    • 1970-01-01
    • 2011-08-28
    • 1970-01-01
    • 2011-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多