We're using the WPF DataGrid from the WPF Toolkit and are employing MVVM. I'm finding some challenges in using MultiSelect in MVVM and am sure that I'm just missing something simple. I can use the "IsSelected" in the ViewModel to get the event when selecting a row in the DataGrid. This works fine for a single selection.
If I set "SelectionMode=Extended" I still only get the event on the selection of the first row in the DataGrid.SelectionChanged in the codebehind gets each selection event but I really want to make this work in the ViewModel. There is probably a simple solution to this multiSelect that I am just missing. I also tried binding SelectedItems (plural) to a collection to get all of these, but SelectedItems is readonly so it can't be bound to. I sure would appreciate any ideas on how to do this as I can't get this method moved from the codebehind to the ViewModel.
You can listen to the SelectionChanged event in the code-behind file and delegate the call to the ViewModel.
The M-V-VM pattern is not about to eliminate ALL code from the code-behind file. It’s about separating of concerns - to put view logic into the ViewModel and let the view be as stupid as possible.
If you are interested in a concrete example then you might have a look at the ViewModel sample of the WPF Application Framework (WAF).
I hope this helps.