I uses MVVM and I bind datagrid to collection with some code:
<DataGrid ItemsSource="{Binding Entites}" AutoGenerateColumns="False" IsSynchronizedWithCurrentItem="True"
SelectedItem="{Binding SelectedEntity}">
And I aslo use binding to IsSelectedProperty using style (RowStyle or ItemContainerStyle)
<DataGrid.RowStyle>
<Style>
<Setter Property="DataGridRow.IsSelected" Value="{Binding IsSelectedProperty, Mode=TwoWay}" />
</Style>
</DataGrid.RowStyle>
It works well. But if I scroll datagrid down and up, it stops working.
I encountered this problem, and the reason was my DataGrid was using virtualization - when you scroll a selected DataGridRow off the screen, the DataGridRow visual element is either destroyed, and a new one created, or - if the DataGrid's VirtualizingStackPanel.VirtualizationMode
property is set to Recycling
- it is reused for whatever row is entering the viewport.
When either event occurs, the binding between your ViewModel (with its IsSelectedProperty
property set) and the DataGridRow (with its IsSelected
property set) is broken.
To confirm this is the case, try setting the DataGrid's EnableRowVirtualization
property to false.
In my case, I needed the performance required by using virtualization, and ended up implementing selectability using Attached Behaviors: specifically, a click on a row would use a LeftClickCommand attached behavior to invoke a delegate command on the ViewModel that sets IsSelectedProperty
. I then used a DataTrigger
bound to IsSelectedProperty
in the DataGridRow's style to highlight the row.
This solution essentially involves rolling your own selection mechanism, but it was the only way I found to get both row virtualization and MVVM-friendly row selection.
Try setting the virtualizationmode to standard on the DataGrid: VirtualizingStackPanel.VirtualizationMode="Standard"
.
I believe virtualization is turned on by default but the mode is recycling. So the IsSelected property for some reason doesn't get reevaluated when a new row is created on scroll.
Is there a particular reason you're binding in the RowStyle? I've always bound my SelectedItem directly to my SelectedEntity property in my ViewModel.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With