To bind to an item of a listview, you have to use RelativeSource in your binding. Below is an example of it being used.
<ListView ItemsSource="{Binding DataItems}"> <ListView.ContextMenu>
<ContextMenu>
<MenuItem Header="Add"
Command="{Binding CustomAddCommand}"/>
<MenuItem Header="Delete"
Command="{Binding CustomDeleteCommand}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem}"/>
</ContextMenu> </ListView.ContextMenu>
</ListView>
If you would want to set a list of selected items instead of just one item use the following in your binding for the command parameter.
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItems}"
This technique works for anything that needs a context menu.
No comments:
Post a Comment