在 DataTrigger 绑定中获取选定的 TabControl 名称

Get selected TabControl name in DataTrigger binding

我的 window 中有两个 TabControl(tabQueryControl 和 tabControl),我创建了带有上下文菜单的样式,我将其设置为右键单击选项卡上的两个 TabControl。但是,根据右键单击的选项卡控件,我想隐藏一些上下文菜单项。这是我在样式中的代码。

<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 中,我尝试获取所选选项卡控件的名称,并根据名称设置菜单项的可见性,但是当我 运行 代码时,两个选项卡控件中的可见性都折叠了.我认为问题出在我对每个数据触发器的绑定中。

首先从绑定路径中删除 TabControl.

绑定生效后,您会发现样式没有按预期工作。对于这两个控件,菜单项都是可见的。问题是,触发器确实在同一个 ContextMenu 实例上工作。

要避免它,请将 x:Shared="false" 添加到 ContextMenu:

<Style x:Key="OutputContextMenuStyle" TargetType="{x:Type TextBlock}">
    <Setter Property="ContextMenu" Value="{DynamicResource OutputContextMenu}"/>
</Style>
<ContextMenu x:Key="OutputContextMenu" x:Shared="false">
    <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=Name, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabControl}}}" Value="tabQueryControl">
                        <Setter Property="Visibility" Value="Collapsed" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=Name, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabControl}}}" Value="tabControl">
                        <Setter Property="Visibility" Value="Visible" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </MenuItem.Style>
    </MenuItem>
</ContextMenu>

所以它应该按预期工作。