WPF Listbox selectionchanged MVVM

后端 未结 2 1739
死守一世寂寞
死守一世寂寞 2020-12-28 16:11

I have a list box in my WPF application. I know how to use the selectionchanged event. However I am trying to follow the MVVM design. However I am not sure how to do this.

2条回答
  •  轮回少年
    2020-12-28 16:51

    Example of binding ListBox SelectionChanged Event to command in your ViewModel

    
       
          
            
        
      
    
    

    In your ViewModel :

    public class myViewModel
    {
    
        public myViewModel()
        {
            SelectedItemChangedCommand = new DelegateCommand((selectedItem) => 
            {
                 // Logic goes here
            });
        }
    
        public List SomeCollection { get; set; }
    
        public DelegateCommand SelectedItemChangedCommand { get; set; }
    }
    
    
    

    This particular example uses Prism MVVM Framework, but you can apply the same idea with any other MVVM framework you are using as well.

    Hope this helps

    提交回复
    热议问题