Datagrid.IsSelected 绑定和滚动
我使用 MVVM 并使用一些代码将数据网格绑定到集合:
<DataGrid ItemsSource="{Binding Entites}" AutoGenerateColumns="False" IsSynchronizedWithCurrentItem="True"
SelectedItem="{Binding SelectedEntity}">
并且我还使用样式(RowStyle 或 ItemContainerStyle)绑定到 IsSelectedProperty
<DataGrid.RowStyle>
<Style>
<Setter Property="DataGridRow.IsSelected" Value="{Binding IsSelectedProperty, Mode=TwoWay}" />
</Style>
</DataGrid.RowStyle>
它效果很好。但是如果我向下和向上滚动数据网格,它就会停止工作。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了这个问题,原因是我的 DataGrid 正在使用虚拟化 - 当您将选定的 DataGridRow 滚动到屏幕之外时,DataGridRow 可视元素要么被销毁,并创建一个新元素,要么 - 如果 DataGrid 的 VirtualizingStackPanel.VirtualizationMode 属性设置为
Recycling
- 它会被重新用于进入视口的任何行。当任一事件发生时,ViewModel(及其
IsSelectedProperty
属性集)和 DataGridRow(及其IsSelected
属性集)之间的绑定就会被破坏。要确认这种情况,请尝试将 DataGrid 的
EnableRowVirtualization
属性设置为 false。就我而言,我需要使用虚拟化所需的性能,并最终使用 附加行为来实现选择性:具体来说,单击一行将使用 LeftClickCommand 附加行为来调用 ViewModel 上设置
IsSelectedProperty
的委托命令。然后,我使用绑定到 DataGridRow 样式中的IsSelectedProperty
的DataTrigger
来突出显示该行。该解决方案本质上涉及滚动您自己的选择机制,但这是我发现获得行虚拟化和 MVVM 友好的行选择的唯一方法。
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 toRecycling
- 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 itsIsSelected
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 aDataTrigger
bound toIsSelectedProperty
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.
尝试在 DataGrid 上将虚拟化模式设置为标准:
VirtualizingStackPanel.VirtualizationMode="Standard"
。我相信默认情况下虚拟化是打开的,但模式是回收的。因此,当滚动创建新行时,出于某种原因,IsSelected 属性不会重新计算。
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.
您在 RowStyle 中进行绑定有什么特殊原因吗?我总是将 SelectedItem 直接绑定到 ViewModel 中的 SelectedEntity 属性。
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.