Search code examples
c#wpfdatagridselectedindex

Set the selectedIndex property and selectedItems is empty


I have an wpf application that use the MVVM with a datagrid. I set the selectedIndex property in the viewModel, but the SelectedItems property is empty. Shouldn't it have the selected the selected item?


Solution

  • Not necessarily.

    One way of doing this is to set SelectedItem property in the datagrid xaml to a property on your view model which implements INotifyPropertyChanged. Then set the xaml binding mode to two way.

    Then if you click on a selected item it will trigger a change from the xaml binding to update the value in the view model