I've used the following method:
<ListView x:Name="itemsListView" ItemsSource="{Binding MyItems}">
<ListView.ContextMenu>
<ContextMenu>
<MenuItem Header="Remove"
Command="{Binding RemoveItem}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem}" />
</ContextMenu>
</ListView.ContextMenu>
</ListView>
provided by blindmeis here, for creating and binding ContextMenu
and it works fine if I first select an item and then right click. If I, however, do not select any item and click anywhere in ListView
it still appears and invoke the command with null
.