There is a UserControl Sector , it has a property IsSectorEnabled .
It is necessary to add a context menu to it, in which the menu item header ( MenuItem Header ) changes depending on the IsSectorEnabled property.
It is DataTrigger make binding through DataTrigger :

 <UserControl x:Class="UserControlLibrary.SectorGrid" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:UserControlLibrary" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <UserControl.Resources> <Style x:Key="MenuItemStyle" TargetType="{x:Type MenuItem}"> <Style.Triggers> <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:Sector}}, Path=IsSectorEnabled}" Value="True"> <Setter Property="Header" Value="Disable"/> </DataTrigger> <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:Sector}}, Path=IsSectorEnabled}" Value="False"> <Setter Property="Header" Value="Enable"/> </DataTrigger> </Style.Triggers> </Style> <Style x:Key="SectorStyle" TargetType="{x:Type local:Sector}"> <Setter Property="ContextMenu"> <Setter.Value> <ContextMenu> <MenuItem Style="{StaticResource MenuItemStyle}"/> </ContextMenu> </Setter.Value> </Setter> </Style> </UserControl.Resources> <UniformGrid Name="uGrid" Columns="5" Rows="5"> <local:Sector Id="1" Text="A1" Style="{StaticResource SectorStyle}"/> <local:Sector Id="2" Text="A1" Style="{StaticResource SectorStyle}"/> <local:Sector Id="3" Text="A1" Style="{StaticResource SectorStyle}"/> <local:Sector Id="4" Text="A1" Style="{StaticResource SectorStyle}"/> <local:Sector Id="5" Text="A1" Style="{StaticResource SectorStyle}"/> <local:Sector Id="6" Text="A1" Style="{StaticResource SectorStyle}"/> <local:Sector Id="7" Text="A1" Style="{StaticResource SectorStyle}"/> <local:Sector Id="8" Text="A1" Style="{StaticResource SectorStyle}"/> <local:Sector Id="9" Text="A1" Style="{StaticResource SectorStyle}"/> <local:Sector Id="10" Text="A1" Style="{StaticResource SectorStyle}"/> <local:Sector Id="11" Text="A1" Style="{StaticResource SectorStyle}"/> <local:Sector Id="12" Text="A1" Style="{StaticResource SectorStyle}"/> <local:Sector Id="13" Text="A1" Style="{StaticResource SectorStyle}"/> <local:Sector Id="14" Text="A1" Style="{StaticResource SectorStyle}"/> <local:Sector Id="15" Text="A1" Style="{StaticResource SectorStyle}"/> <local:Sector Id="16" Text="A1" Style="{StaticResource SectorStyle}"/> <local:Sector Id="17" Text="A1" Style="{StaticResource SectorStyle}"/> <local:Sector Id="18" Text="A1" Style="{StaticResource SectorStyle}"/> <local:Sector Id="19" Text="A1" Style="{StaticResource SectorStyle}"/> <local:Sector Id="20" Text="A1" Style="{StaticResource SectorStyle}"/> <local:Sector Id="21" Text="A1" Style="{StaticResource SectorStyle}"/> <local:Sector Id="22" Text="A1" Style="{StaticResource SectorStyle}"/> <local:Sector Id="23" Text="A1" Style="{StaticResource SectorStyle}"/> <local:Sector Id="24" Text="A1" Style="{StaticResource SectorStyle}"/> <local:Sector Id="25" Text="A1" Style="{StaticResource SectorStyle}"/> </UniformGrid> </UserControl> 

    1 answer 1

    The problem is this. You are trying to “reach out” from MenuItem 's to the item that was clicked by searching up through the item tree. This does not work for what reason.

    When you open the context menu popup appears, which is a separate root element in the tree. So, going from MenuItem 's, you get into ContextMenu , then Popup , and that's it.

    But there is a way out: ContextMenu has a PlacementTarget property that stores that element in the main tree on which the click was made!

    Thus, your code should be as follows:

     <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ContextMenu}, Path=PlacementTarget.IsSectorEnabled}" Value="True"> <Setter Property="Header" Value="Disable"/> </DataTrigger> 
    • Thank you so much for the detailed, comprehensive answer! - Zer0CooL
    • @ Zer0CooL: Please! - VladD