Hello! Silverlitght windows phone toolkit has a context menu control. So, the toolkit itself gives an example of using the menu in single objects, I use the menu in the list and, therefore, I cannot figure out how after clicking on one of the context menu items to determine for which element of the list this menu is called. The whole thing is complicated by the fact that in the example of the toolkit there is no example of creating a menu in the code, only in the xaml markup. Help experienced, please! Here is a piece of context menu code in xaml:

<toolkit:ContextMenuService.ContextMenu> <toolkit:ContextMenu> <toolkit:MenuItem Header="добавить в..." Click="MenuItem_Click"/> <toolkit:MenuItem Header="переименовать"/> <toolkit:MenuItem Header="удалить"/> <toolkit:MenuItem Header="доп. информация" Click="ShowFileInfo"/> </toolkit:ContextMenu> </toolkit:ContextMenuService.ContextMenu> 

    1 answer 1

    With each menu item you need to associate a command. Something like this (where YourList is the name of the list to which the list is attached):

     <toolkit:ContextMenuService.ContextMenu> <toolkit:ContextMenu> <toolkit:MenuItem Header="добавить в..." Command={ElementName=YourList, Path=DataContext.AddItemCommand}/> <toolkit:MenuItem Header="доп. информация" Command={ElementName=YourList, Path=DataContext.ShowItemInfoCommand}/> </toolkit:ContextMenu> </toolkit:ContextMenuService.ContextMenu> 

    View the model (using the RelayCommand RelayCommand ):

     using GalaSoft.MvvmLight.Command; public class SomeViewModel { // для привязки к списку public ObservableCollection<SomeItem> Items { get; set; } public RelayCommand<SomeItem> AddItemCommand { get; set; } public RelayCommand<SomeItem> ShowItemInfoCommand { get; set; } public MessagesViewModel() { AddItemCommand = new RelayCommand<SomeItem>(OnAddItem); ShowItemInfoCommand = new RelayCommand<SomeItem>(OnShowItemInfo); } private void OnAddItem(SomeItem item) { ... } private void OnShowItemInfo(SomeItem item) { ... } } 
    • I got to the list item like this: 'private async void ContextMenuItemClick (object sender, RoutedEventArgs e) {var ViewItem = ((ViewItem) ((FrameworkElement) ((ContextMenu) ((MenuItem) sender) .Parent) .Owner). DataContext) ;} 'it seems to work, but I don’t know if it’s correct or not)) Thanks for the hint, I’ll use your method when I get to implement the MVVM model - Roman Nomokonov
    • @RomanNomokonov will be brief - use MVVM :). - andreycha