Silverlight context menu: how to determine which menu was clicked?

You can get the item the ListBoxItem is bound to by casting the sender as a FrameworkElement to get access to the DataContext.

Up vote 0 down vote favorite share g+ share fb share tw.

I have the following context menu: As you can see, each displayed item has its own context menu. Each context menu is hooked up to the same event handler: private void ContextMenuItem_Click(object sender, RoutedEventArgs e) { } From this method, how can I tell which context menu was clicked? I want to know what the DataContext for the corresponding DataTemplate is.

Silverlight windows-phone-7 link|improve this question asked Dec 20 '10 at 18:06Rosarch8,9041267218 62% accept rate.

You can get the item the ListBoxItem is bound to by casting the sender as a FrameworkElement to get access to the DataContext: (sender as FrameworkElement). DataContext You can then cast this to the appropriate model class and access the details you need. E.g.

: ((sender as FrameworkElement). DataContext as ItemViewModel).DisplayName.

If you put a breakpoint inside the event handler ContextMenuItem_Click, you will then be able to examine the properties of sender and e. You will probably find your answer there. One way to do this is to hover over those words.

Another would be to use the Immediate Window. Type in sender and a dot to get intellisense.

If you use then ((FrameworkElement)sender). Tag will return the DataContext object (you'll have to cast it before use, of course).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions