开发者

Datagrid.IsSelected Binding and scrolling

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜