Passing ListView.SelectedItems as a ContextMenu CommandParameter

I spent quite a while trying all of what I thought were the obvious methods of doing this and couldn’t get anything to work.  Then I stumbled on a Blog by Richard Griffin at that pointed me in the right direction.

	  <MenuItem Header="Schema"
	    Command="{Binding ExportSchemaCommand}"
	    CommandParameter="{Binding Path=PlacementTarget.SelectedItems, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}" />

Rather than trying to do something obvious like use RelativeSource or ElementName to find the BindingPath use the ContextMenu.PlacementTarget.

ListView.SelectedItems is of type System.Windows.Controls.SelectedItemCollection which is marked as Internal so in your command you have to cast it as its base class which is System.Collections.ObjectModel.ObservableCollection<object>.

One thought on “Passing ListView.SelectedItems as a ContextMenu CommandParameter

  1. I wish you’d put more of the sample code (especially the C# part regarding the command) since that link is dead now. Oh well, time to keep looking

Leave a Reply

Your email address will not be published. Required fields are marked *