1

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

Community
  • 1
  • 1
Garry Mason
  • 33
  • 1
  • 8

2 Answers2

2

The reason why it is not working is because you cannot have a style that uses an EventSetter inline (i.e., inside the style of a control, or in your case, inside another style). You can fix this by putting the MenuItem style into its own Resource.

See below:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.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 x:Key="LincrosContextMenu" TargetType="{x:Type ContextMenu}">
            <Style.Resources>
                <Style TargetType="{x:Type Separator}">
                    <Setter Property="Margin" Value="6, 0, 0, 0" />
                </Style>
            </Style.Resources>
        </Style>

        <ContextMenu x:Key="ProjectFolderContext" Style="{StaticResource 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>
    </Window.Resources>

    <Button Width="100" Height="75" ContextMenu="{StaticResource ProjectFolderContext}"/>
</Window>
d.moncada
  • 16,900
  • 5
  • 53
  • 82
  • It seems the error disappeared after I built the project. I guess the solution I referenced wasn't working because I hadn't tried building the project. I guess I shouldn't completely trust visual studios editor then. Thanks for your help! :) – Garry Mason Aug 24 '15 at 17:12
0

Since I'm unable to edit my own question, this was my end solution based of the accepted answer from "d.moncada".

    <Style x:Key="LincrosContextMenuItem" 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 x:Key="LincrosContextMenu"
           TargetType="{x:Type ContextMenu}"
           BasedOn="{StaticResource MetroContextMenu}">
        <Style.Resources>
            <Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource LincrosContextMenuItem}" />
            <Style TargetType="{x:Type Separator}">
                <Setter Property="Margin" Value="6, 0, 0, 0" />
            </Style>
        </Style.Resources>
    </Style>

Happy coding. :)

Garry Mason
  • 33
  • 1
  • 8