简体   繁体   中英

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.

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

MyItemContainerStyle是一个Style ,而不是ContextMenu ......你应该写这个:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM