WPF and ListView context menus

Well, hopefully this is simple, but for some reason I cannot find a direct answer, and I am not familiar enough with WPF, but I don't know how to do it.

I have a listview, it is tied to an observable collection of objects to display. I want to have a context menu with many options. The options in the context menu relate to a specific object in the list that was clicked (such as deletion, export, etc.).

Therefore, I need an object that the user right-clicked in my list to pass it as a parameter to the command that the context menu executes.

How to do it?

Edit: I have to mention that I would prefer a solution that is mostly (if not completely) xaml - I'm trying to avoid having significant code in the code. If this is the only way to do it though ...

Further editing . The more detailed information that I forgot to mention is important. The command I want to execute is related to an object associated with the data context of my user control, but not on the objects in a list. Therefore, I need the context menu in the list view items to be attached to a command located in the context of user control data and the list item passed as a parameter to this command.

+5
source share
1 answer

, ContextMenu ListBox .

ContextMenu DataTemplate ( , ), DataContext MenuItem , , :

<MenuItem ... CommandParameter="{Binding}" />

, ContextMenu ListBox , SelectedItem ListBox:

<MenuItem ... CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource FindAncestor,ListBox,1}} />
+4

All Articles