How to override ContextMenu in a global style?

Declare the style my control in the library:

 <ContentControl.Resources> <ContextMenu x:Key="ContextMenu"> <MenuItem Header="{x:Static Drawing:Headers.AddEdge}" Click="AddEdgeClick"/> <MenuItem Header="{x:Static Drawing:Headers.ChangeID}" Click="ChangeIDClick"/> <MenuItem Header="{x:Static Drawing:Headers.Remove}" Click="RemoveClick"/> </ContextMenu> <Style x:Key="Style" TargetType="{x:Type Drawing:Node}"> <Setter Property="ContextMenu" Value="{StaticResource ContextMenu}"/> </Style> </ContentControl.Resources> <ContentControl.Style> <StaticResource ResourceKey="Style"/> </ContentControl.Style> 

But the global style for this control in the application does not work ...

 <Style TargetType="Drawing:Node"> <Setter Property="ContextMenu" Value="{x:Null}"/> <EventSetter Event="MouseLeftButtonUp" Handler="DirectoryClicked"/> </Style> 
0
source share
1 answer

If you explicitly set the Style property for an element, then any implicit styles (i.e. your global style) will not apply. If your global style is actually the default, then it should still apply, but that doesn't look like what you are doing.

You can create your explicit style in your implicit style, though like this:

 <Style x:Key="Style" TargetType="{x:Type Drawing:Node}" BasedOn="{StaticResource {x:Type Drawing:Node}}"> <Setter Property="ContextMenu" Value="{StaticResource ContextMenu}"/> </Style> 

This, of course, assumes that Drawing: Node is the same class / type as GraphNode: Node.

0
source

Source: https://habr.com/ru/post/1343578/


All Articles