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

后端 未结 9 1214
暖寄归人
暖寄归人 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:18

    Have a look over here http://blog.functionalfun.net/2009/02/how-to-databind-to-selecteditems.html

    0 讨论(0)
  • 2020-12-09 18:19

    The solution of Drew Marsh works very well, I recommend it. And I have another solution !

    Model View ViewModel is a Passive View, you can also use a Presentation Model to access some datas of your presentation without being coupled with WPF (this pattern is used in the Stocktrader example of PRISM).

    0 讨论(0)
  • 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<string>();
    
    foreach (var selectedModule in listBox1.SelectedItems)
    {
        viewModel.SelectedModules.Add(selectedModule.ToString());
    }
    
    0 讨论(0)
提交回复
热议问题