ContextMenu and Data?

I would like to add a ContextMenu to objects in a ListBox.
These objects have a DataTemplate. I got it to add a ContextMenu to the DataTemplate so it was displayed on each item.
But how can I react to any Click Event

I tried to use <MenuItem Click="OnItemMenuClick" /> but how can I get a reference to the data the ContextMenu lies on top Parent properties point to nothing ...


Answer this question

ContextMenu and Data?

  • Kevin Prendergast

    Thanks for your reply.

    This will work for me but isn't it possible to bind a MenuItem to its dataitem at all
    I want to customize the context menu on the dataitem's properties but can't see a way how I can do it e.g. by DataTriggers :(

  • Shree S

    I do not know if it will help in your case, but look at EventSetter.  The style for your data template can apply an EventSetter to attach an event.  Getting it to target the object in the data context might be an issue.  You might get away with it if the EventSetter is in the data template itself rather than the style, but I am not sure a template can use setters in this way.

    This actually sounds like what I would like to do in some cases as well.  I would like to use a data template to place a number of UI related objects in the display that have interaction methods.  I would then like to bind the events and properties of the UI to these ViewModel objects.  The property part works fine, but the events part is a work in progress.

  • paul rees

    Is there a reason you can't just put the ContextMenu on the listbox itself and just operate on the selected item(s)
  • britttd

    Every item should have different options depending on its properties.

  • DSH_msn

    Well I'd have to think about it some more, but just like you could configure the ContextMenu in the event handler before you show it manually, you might also be able to bind it the to SelectedItem manually instead and let all the DataBinding magic declared inside your ContextMenu instance take place. That should just involve updating the DataContext property of the ContextMenu to the currently SelectedItem which should cause all the bindings within the menu to update themselves.

    I'd experiment with it some more myself before recommending this solution, but I'm in the middle of a release today and don't have the time. Be sure to let us know how that works out for you!

    Cheers,
    Drew

  • Golf67

    First off, you have the context menu showing up, right

    So you're saying you want to populate the context menu instance with items based on it's specific state when the user clicks to show the menu, right I imagine that's the case since I can't think of any other explanation for wanting to bind the click of each item. However, if that is the case then you can do the very same thing by handling the event at the ListBox level. You can get at the SelectedItem in the event handler and check it's state to figure out what ContextMenu (or what options on a ContextMenu) to show from there.

    Does this not work for you If not, can you give some more detail

    HTH,
    Drew

  • Yuri Kuleshov

    Sorry I did a mistake. I added the ContextMenu to the ItemContainerStyle and not to the DataTemplate because I thought this is impossible.
    So I can't use EventSetters on the Template ...

  • ContextMenu and Data?