nerdalert
nerdalert

Reputation: 461

Get selected TabControl name in DataTrigger binding

I have two TabControls in my window (tabQueryControl, and tabControl) and I've created style with a ContextMenu which I set to both the TabControls on a tab right click. However, depending on the tabcontrol which has been right clicked, I want to hide some context menu items. This is my code in the 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>

In the DataTrigger I try to get the name of the selected tab control, and set the visibility of the menu item depending on the name, but when I run the code the visibility is collapsed in both tabcontrols. I think the issue is in my binding for each data trigger.

Upvotes: 1

Views: 165

Answers (1)

Rekshino
Rekshino

Reputation: 7325

First remove TabControl. from binding path.

After the binding works you can find, that the style doesn't work as expected. For both controls the menu item is visible. The problem is, that the triggers do work on the same ContextMenu instance.

To avoid it add x:Shared="false" to the 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>

So it should work as expected.

Upvotes: 1

Related Questions