Question

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

Was it helpful?

Solution

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.

What is the easiest way to handle SelectedItem event with MVVM?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top