Michel Keijzers
Michel Keijzers

Reputation: 15367

Error: System.Windows.Style' is not a valid value for property 'ContextMenu'

I am trying to use a context menu and I get the following error as soon as I press the right mouse button:

System.Windows.Style' is not a valid value for property 'ContextMenu'.

XAML code:

<UserControl ...>
    <UserControl.Resources>
        <ContextMenu x:Key="SharedInstanceContextMenu">
            <MenuItem Header="Edit" Command="{Binding Path=EditSelectedItemCommand}"/>
        </ContextMenu>

        <Style x:Key="MyItemContainerStyle" TargetType="{x:Type ListViewItem}">
            <Setter Property="ContextMenu" Value="{StaticResource SharedInstanceContextMenu}" />
        </Style>
    </UserControl.Resources>

    ...

    <ListView ...
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="ListBoxItem.IsSelected" Value="{Binding Path=IsSelected}" />
                <Setter Property="ContextMenu" Value="{StaticResource MyItemContainerStyle}"/>

(I used dots to remove unrelated code).

Does anybody know how to prevent the error (and seeing a context menu)?

Thanks in advance.

Upvotes: 0

Views: 3474

Answers (1)

Thomas Levesque
Thomas Levesque

Reputation: 292465

<Setter Property="ContextMenu" Value="{StaticResource MyItemContainerStyle}"/>

MyItemContainerStyle is a Style, not a ContextMenu... you should probably write this instead:

<Setter Property="ContextMenu" Value="{StaticResource SharedInstanceContextMenu}"/>

Upvotes: 3

Related Questions