WPF M-V-VM: Get selected items from a ListCollectionView?

后端 未结 9 1230
暖寄归人
暖寄归人 2020-12-09 17:33

I\'ve got a WPF app using the Model-View-ViewModel pattern.
In my ViewModel I\'ve got a ListCollectionView to keep a list of items.
This ListCollectionView is bound

9条回答
  •  离开以前
    2020-12-09 18:22

    For me the best answer is to break a little the principle of MVVM.

    On the code behind 1. Instanciate your viewModel 2. add an event handler SelectionChanged 3. iterate through your selected items and add each item to your list of the viewModel

    ViewModel viewModel = new ViewModel();
    
    viewModel.SelectedModules = new ObservableCollection();
    
    foreach (var selectedModule in listBox1.SelectedItems)
    {
        viewModel.SelectedModules.Add(selectedModule.ToString());
    }
    

提交回复
热议问题