views:

1094

answers:

1

Hi,

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.

thanks! Bill

+2  A: 

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. jbe

jbe
Hi - Thanks for the response. Yes, I do know that the idea of the MVVM is not to eliminate code from the code behind completely but I do believe there are other answers to this example that do not include code in the code behind. Perhaps the solution is AttachedBehavior. I'm still looking.
Bill Campbell
I was hoping to find something better but had no luck. I could use the MVVM Toolkit Light with Event To Command but ended up just delegating from the codebehind's SelectionChanged.Thanks again jbe!
Bill Campbell
I have had success with the MVVM Light Toolkit's EventToCommand as part of an EventTrigger. I had seen a sample by Laurent Bugnion and found it immensely helpful: http://blog.galasoft.ch/archive/2009/12/17/silverlight-4-dragampdrop-with-eventtocommand.aspxAdmittedly I haven't used it in a datagrid scenario... just bound to other data templates.
Mike L