Search code examples
silverlightwindows-phone-7silverlight-toolkit

howto get the selected ContextMenu item in a LongListSelector?


I have a problem, I need to get the selected Item from a ContextMenu in a LongListSelector. Before I was using a normal ListBox and I did it with:

var selectedItem = myList.ItemContainerGenerator.ContainerFromItem(menuItem.DataContext) as ListBoxItem;

But now I'm using the LongListSelector and this method doesn't work.

Who can I do it?


Solution

  • If the LongListSelector is databound, you can use the DataContext property to access the selected item.

    private void MenuItem_Click( object sender, RoutedEventArgs e ) {
        MyObject obj = ( sender as MenuItem ).DataContext as MyObject;
    }
    

    assuming your LongListSelector is databound to a list of MyObject objects.