WPF ListView Selecting Multiple List View Items

前端 未结 5 2080
故里飘歌
故里飘歌 2021-02-07 12:21

I am figuring out a way to Select Multiple items in list view and deleting them on a certain action. What I can\'t figure out is, how should I have these multiple items selected

5条回答
  •  迷失自我
    2021-02-07 12:56

    Arcturus answer is good if you're not using MVVM. But if you do and your ItemsSource is binded to some ObservableCollection of objects in your ViewModel, I would recommend Tigran answer, with Noman Khan clarification.

    This is how it would look like:

    
        
           
        
        ...
    
    

    In View Model you would have object: public ObservableCollection SomeListViewList{ get; set; }

    SomeItem Class would include a Selected property:

    public class SomeItem
    {
        public string SomeItemName { get; set; }
    
        public string SomeItemNum { get; set; }
    
        public bool SomeItemSelected { get; set; }
    }
    

    Then you could iterate/run over the list and get the ones that are selected:

    foreach (var item in SomeListViewList)
       if (item.SomeItemSelected)
          // do something
    

提交回复
热议问题