【发布时间】:2020-01-07 09:04:59
【问题描述】:
我的窗口中有两个 TabControl(tabQueryControl 和 tabControl),并且我使用 ContextMenu 创建了样式,我将其设置为右键单击选项卡上的两个 TabControl。但是,根据已右键单击的选项卡控件,我想隐藏一些上下文菜单项。这是我在 Style 中的代码。
<Style x:Key="OutputContextMenuStyle" TargetType="{x:Type TextBlock}">
<Setter Property="ContextMenu" Value="{DynamicResource OutputContextMenu}"/>
</Style>
<ContextMenu x:Key="OutputContextMenu">
<MenuItem Header="View in DataViewer" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type cw:ChromeWindow}}, Path=DataContext.ViewCommand}" CommandParameter="OutputWindow">
<MenuItem.Icon>
<Image Source="/Data_Viewer;component/Resources/NodeIcons/view_in_dataviewer.png"/>
</MenuItem.Icon>
<MenuItem.Style>
<Style TargetType="MenuItem">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<!-- if the name of the parent tab control is tabQueryControl, we hide this context menu item -->
<DataTrigger Binding="{Binding Path=TabControl.Name, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabControl}}}" Value="tabQueryControl">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=TabControl.Name, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabControl}}}" Value="tabControl">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</MenuItem.Style>
</MenuItem>
</ContextMenu>
在 DataTrigger 中,我尝试获取所选选项卡控件的名称,并根据名称设置菜单项的可见性,但是当我运行代码时,两个选项卡控件中的可见性都会折叠。我认为问题在于我对每个数据触发器的绑定。
【问题讨论】:
-
下次请努力创建MCVE,以便您更轻松地获得帮助。
标签: wpf data-binding wpf-controls