【问题标题】:Get selected TabControl name in DataTrigger binding在 DataTrigger 绑定中获取选定的 TabControl 名称
【发布时间】: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


【解决方案1】:

首先从绑定路径中删除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>

所以它应该按预期工作。

【讨论】:

  • 非常感谢,这成功了。我已经尝试删除 TabControl。从绑定路径,这不起作用,但将共享属性添加到上下文菜单是我错过的。谢谢!
猜你喜欢
  • 2013-12-14
  • 1970-01-01
  • 1970-01-01
  • 2019-11-30
  • 2013-06-04
  • 2018-04-05
  • 1970-01-01
  • 1970-01-01
  • 2017-07-18
相关资源
最近更新 更多