I am trying create a style resource for a context menu that I am assigning dynamically in code to expanders. I wish to set a click event listener for the menu items in the context menu in the style but I am receiving errors of which I cannot find a solution for.
Errors:
- The event 'Click' cannot be specified on a Target tag in a Style. Use an EventSetter instead.
Invalid value for property 'Handler': 'Microsoft.VisualStudio.DesignTools.Xaml.LanguageService.Semantics.XmlValue'
<Style x:Key="LincrosContextMenu" TargetType="{x:Type ContextMenu}" BasedOn="{StaticResource MetroContextMenu}"> <Style.Resources> <Style TargetType="{x:Type MenuItem}"> <Setter Property="FontSize" Value="12" /> <Setter Property="Padding" Value="0, 3, 0, 3" /> <EventSetter Event="Click" Handler="ContextMenu_MenuItem_Click"/> </Style> <Style TargetType="{x:Type Separator}"> <Setter Property="Margin" Value="6, 0, 0, 0" /> </Style> </Style.Resources> </Style> <ContextMenu x:Key="ProjectFolderContext" Style="{StaticResource ResourceKey=LincrosContextMenu}"> <MenuItem Header="Add"/> <Separator/> <MenuItem Header="Cut" /> <MenuItem Header="Copy" /> <MenuItem Header="Paste" /> <MenuItem Header="Rename" /> <Separator/> <MenuItem Header="Open Folder in File Browser" /> <Separator/> <MenuItem Header="Properties" /> </ContextMenu>
I have tried out this solution but unfortunately it had no effect for me.
Reference: WPF ListView SelectionChanged inside style does not work. EventSetter either