You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello everyone,
I'm facing an issue in my application, where I basically have a DataGrid with a binding to a DataGridCollectionView initialized from an ObservableCollection. I need to manage the SelectedItem (which represents a whole row) so that it is setted both by the View (user selection) and programmatically in the ViewModel (TwoWay Binding).
I face a random issue when I try to set the SelectedItem property from the ViewModel.
The problem is that sometimes a change from null to a valid reference to an object of the DataGridCollectionView fails, and I see two calls to a OnSelectedItemChanged callback in my ViewModel.
ViewModel::OnSelectedItemChanged: old value = null -> new value = <object>
ViewModel::OnSelectedItemChanged: old value = <object> -> new value = null
I digged into the DataGrid.cs code and found the the problem lies in the OnSelectedItemChanged method of DataGrid, specifically at the end when this check is performed:
When I encounter the wrong behaviour, _successfullyUpdatedSelection is false.
The method is reverting all changes and is setting SelectedItemProperty to the old value, which is null.
This is the source of the second event described above, the callback to OnSelectedItemChanged() (in the ViewModel) with null as the new value.
I've investigated further in the call stack of DataGrid's OnSelectedItemChanged, to understand why _successfullyUpdatedSelection remains set to false and I've concluded that the culprit lies in the following lines:
The method ProcessSelectionAndCurrency() is actually responsible for setting _successfullyUpdatedSelection = true but it is not called in each code path. If DataConnection.CollectionView.CurrentPosition != rowIndex then just DataConnection.MoveCurrentTo() is called, and it does not process the Selection at all.
That's what I found in my debug session.
I kindly ask DataGrid experts if there's something actually wrong in the code spotted above.
My opinion is that:
ProcessSelectionAndCurrency() should be always called, or
_successfullyUpdatedSelection should be set to true when the DataConnection.MoveCurrentTo() path is taken.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello everyone,
I'm facing an issue in my application, where I basically have a DataGrid with a binding to a
DataGridCollectionViewinitialized from an ObservableCollection. I need to manage theSelectedItem(which represents a whole row) so that it is setted both by the View (user selection) and programmatically in the ViewModel (TwoWay Binding).I face a random issue when I try to set the
SelectedItemproperty from the ViewModel.The problem is that sometimes a change from null to a valid reference to an object of the DataGridCollectionView fails, and I see two calls to a OnSelectedItemChanged callback in my ViewModel.
null-> new value =<object><object>-> new value =nullI digged into the
DataGrid.cscode and found the the problem lies in theOnSelectedItemChangedmethod of DataGrid, specifically at the end when this check is performed:When I encounter the wrong behaviour,
_successfullyUpdatedSelectionisfalse.The method is reverting all changes and is setting
SelectedItemPropertyto the old value, which is null.This is the source of the second event described above, the callback to OnSelectedItemChanged() (in the ViewModel) with null as the new value.
I've investigated further in the call stack of DataGrid's OnSelectedItemChanged, to understand why _successfullyUpdatedSelection remains set to false and I've concluded that the culprit lies in the following lines:
The method
ProcessSelectionAndCurrency()is actually responsible for setting _successfullyUpdatedSelection = true but it is not called in each code path. IfDataConnection.CollectionView.CurrentPosition != rowIndexthen justDataConnection.MoveCurrentTo()is called, and it does not process the Selection at all.That's what I found in my debug session.
I kindly ask DataGrid experts if there's something actually wrong in the code spotted above.
My opinion is that:
ProcessSelectionAndCurrency()should be always called, or_successfullyUpdatedSelectionshould be set totruewhen theDataConnection.MoveCurrentTo()path is taken.Thanks!
Beta Was this translation helpful? Give feedback.
All reactions