Datagrid.IsSelected绑定和滚动 [英] Datagrid.IsSelected Binding and scrolling

查看:532
本文介绍了Datagrid.IsSelected绑定和滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MVVM,我绑定数据网格来收集一些代码:

I uses MVVM and I bind datagrid to collection with some code:

<DataGrid ItemsSource="{Binding Entites}" AutoGenerateColumns="False" IsSynchronizedWithCurrentItem="True" 
                  SelectedItem="{Binding SelectedEntity}">

和我ASLO使用使用样式(RowStyle或ItemContainerStyle)

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.

推荐答案

我遇到了这个问题,原因是我的DataGrid是使用虚拟化 - 当您滚动选定DataGridRow离开屏幕时,DataGridRow视觉元素要么破坏,一个新的创造,或者 - 如果DataGrid的 VirtualizingStackPanel.VirtualizationMode 属性设置为回收 - 它是为任何行进入视重用。

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.

当任一事件发生时,您的视图模型之间的结合(与 IsSelectedProperty 属性集)和DataGridRow(用其 IsSelected 属性集)被打破了。

When either event occurs, the binding between your ViewModel (with its IsSelectedProperty property set) and the DataGridRow (with its IsSelected property set) is broken.

要确认这种情况下,尝试设置DataGrid的 EnableRowVirtualization 属性设置为false。

To confirm this is the case, try setting the DataGrid's EnableRowVirtualization property to false.

在我的情况下,我需要通过使用虚拟化所需的性能,并最终通过实施可选择性< A HREF =http://www.codeproject.com/KB/WPF/AttachedBehaviors.aspx>附加行为:具体而言,在连续点击将使用LeftClickCommand附着行为的调用委托指令视图模型,设置 IsSelectedProperty 。然后我在DataGridRow的方式是使用 DataTrigger 势必 IsSelectedProperty 来突出显示该行。

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.

本解决方案主要涉及滚动你自己的选择机制,但它是我发现同时获得行虚拟化和MVVM友好行选择的唯一途径。

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.

这篇关于Datagrid.IsSelected绑定和滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆