How to make context menu work for windows phone?

前端 未结 2 1761
轮回少年
轮回少年 2020-12-06 23:12

I am using ContextMenu from Windows Phone Control toolkit. Wondering how do I know which list item in the list is pressed? It seems I can know which context menu is selected

2条回答
  •  日久生厌
    2020-12-06 23:56

    I would also recommend MVVM, but it can be simplified. No need to go to the extreme of having a ViewModel for every object. The key is to bind the command to the DataContext of your ItemsControl (eg ListBox).

    Let's assume that your ItemTemplate is for a ListBox, the ListBox has it's ItemsSource property bound to your ViewModel. Your xaml would look like this:

    
        
            
                
                    
                    
                       
                           
                       
                    
                              
            
        
    
    

    Your ViewModel, would then have the property Songs, that is a collection of your model object. It also has an ICommand AddToPlaylistCommand. As I've said before, my favorite implementation of ICommand is the DelegateCommand from the PNP team.

    public class ViewModel : INotifyPropertyChanged
    {
        public ViewModel()
        {
            Songs = new ObservableCollection();
            AddToPlaylistCommand = new DelegateCommand(AddToPlaylist);
        }
        public ICollection Songs { get; set; }
        public ICommand AddToPlaylistCommand  { get; private set; }
    
        private void AddToPlaylist(Song song)
        {
            // I have full access to my model!
            // add the item to the playlist
        }
    
        // Other stuff for INotifyPropertyChanged
    }
    

提交回复
热议问题