1

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

Houman
  • 64,245
  • 87
  • 278
  • 460

1 Answers1

2

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?

Community
  • 1
  • 1
Houman
  • 64,245
  • 87
  • 278
  • 460
  • 1
    A good answer to your own question. I was looking to do exactly this - bind an ICommand to the SelectedItemChanged event and it never occured to me to bind to the SelectedItem property and react to changes to the bound variable in the view model. – AdamCrawford Oct 09 '12 at 12:38