问题描述
我发现很难找到绑定 RibbonContextualTabGroup .我在代码的后面有一个属性,该属性应该决定何时显示功能区选项卡,但是到目前为止,我尝试过的所有操作均无效.我的后台代码本质上是:
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功能区上下文选项卡可见性绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!