views:

433

answers:

2

Hello,

In particular I would like to know how to bind the 'SelectionChanged' event of the dataGrid to a Command on my Viewmodel.

Since DataGrid doesnt have a Command property, how do I call a modelView command as in MVVM fashion? I dont mind using a delegate on the code behind XAML, if I knew how to do that...

Since I am new to WPF I am very stuck on how to accomplish this. May someone please help me with this?

Kind Regards, Kave

A: 

See

Binding Commands to Events?

Cameron MacFarland
+1  A: 

Thanks Cameron. I had discovered it first too, but I prefer not using 3rd Party libraries at this stage.

After many many hours, I have found this link that helped me solving the problem in a different way.

In fact, there is no need for a DataGrid to use commands because its not really executing an action such as a button. The 'SelectionChanged' event can be easily made talking to the modelview by exposing a "selectedItem" property in the modelView and bind it to the Datagrid's selectedItem. The following example does it with a combobox instead of a datagrid, but its exactly the same concept. However I recommend using the CollectionView instead and making sure to feed the datagrid with a ObservableCollection<> and not with a e.g. DataTable.

http://stackoverflow.com/questions/1011750/what-is-the-easiest-way-to-handle-selecteditem-event-with-mvvm

Kave