0

I try to use a context menu from a user control's list view but the command is not firing (neither enabling/disabling when needed).

Code:

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

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

How can I make the command firing (and enabling/disabling, part of the command behavior)?

(btw, this questions seems similar to Treeview context menu command not firing but after trying all solutions there it still does not work.)

Community
  • 1
  • 1
Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119

1 Answers1

1

Does your output window contain binding errors complaining the command doesn't exist on your view model? If so, it probably means that your ContextMenu's DataContext isn't set correctly. Context menus are not part of the visual tree because they need to pop up on top of elements which means they don't inherit their DataContext the way other controls do. One solution is to use the PlacementTarget to access your view model - see this post for more information.

Community
  • 1
  • 1
Dan Busha
  • 3,723
  • 28
  • 36
  • Thanks, I accepted the answer becasue of the output which indeed shows an error ... I haven't had the time to check how to fix it though. – Michel Keijzers Apr 19 '12 at 07:59