Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set ContextMenu of a bound item?

I am trying to achieve the following:

<Style TargetType="ListBoxItem">
    <Setter Property="ContextMenu">
        <Setter.Value>
            <ContextMenu>
                <MenuItem Name="mnuEdit" Header="_Edit" Click="MenuItem_Click" />
            </ContextMenu>
        </Setter.Value>
    </Setter>
<Style>

But it throws the following exception:

Cannot add content of type 'System.Windows.Controls.ContextMenu'
to an object of type 'System.Object'.
Error at object 'System.Windows.Controls.ContextMenu'
in markup file blah blah blah
like image 597
Shimmy Weitzhandler Avatar asked Sep 09 '09 04:09

Shimmy Weitzhandler


1 Answers

Try this instead:

<ContextMenu x:Key="contextMenu">
    <MenuItem Name="mnuEdit" Header="_Edit" Click="MenuItem_Click" />
</ContextMenu>

<Style TargetType="ListBoxItem">
    <Setter Property="ContextMenu" Value="{DynamicResource contextMenu}" />
</Style>
like image 138
Drew Noakes Avatar answered Sep 17 '22 17:09

Drew Noakes