我发现很难找到绑定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>
答案 0 :(得分:1)
您可以创建Converter IsNotNullToVisibilityConverter
使用这样的Convert方法:
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); }
}