Silverlight DataGrid 中的复选框行为异常 [英] Checkbox in silverlight DataGrid behaving strangely

查看:21
本文介绍了Silverlight DataGrid 中的复选框行为异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Silverlight 5 DataGriditemtemplate 列中使用 checkbox.我正面临一个奇怪的问题.当我选择多个 checkbox 然后上下滚动网格时,选择会转移到其他一些 checkbox.

I am using checkbox in an itemtemplate column in a Silverlight 5 DataGrid. I am facing a strange problem with it. When I select more than one checkbox and then scroll the grid up and down, the selection shifts to some other checkbox.

推荐答案

这是一种已知行为,因为 Silverlight 在 DataGrid 中重用其图形资源.此 Silverlight 线程 中对此进行了讨论.

This is a known behaviour since Silverlight is re-using its graphical resources in the DataGrid. There's a discussion about it in this Silverlight thread.

修复它的一种方法似乎是对 IsSelected 属性进行数据绑定:

It seems one way to fix it is to databind the IsSelected property:

我当时的解决方案是在我的数据源中添加一个新属性:IsSelected,并将复选框绑定到该值.

My solution at that time was to add a new property in my data source: IsSelected, and to bind the checkbox to that value.

您有更多附加信息在此线程中,Microsoft 回答:

You have more additional info in this thread, where Microsoft answers:

这不是错误.当您在DataGrid 与用于新数据的复选框相同,因为DataGrid 回收视觉效果.当您的新数据具有不同值,支票将通过绑定更改,您将收到事件.要让这个场景发挥作用,你可以做的是倾听到 LoadingRow,当一行进入视图时引发.在那里,您可以调用 column.GetCellContents 来获取单元格的内容.这将为您提供 CheckBox,您可以在以下位置附加到 CheckChanged这次.如果你这样做,你需要做类似的事情听 UnloadingRow 这样你就可以在复选框已滚出视图.

This is not a bug. What happens when you scroll around in the DataGrid is the same checkboxes are being used for new data because the DataGrid recycles the visuals. When your new data has different values, the check will change through the Binding and you'll receive the event. What you can do to get this scenario to work is to listen to LoadingRow which is raised when a row comes into view. In there, you can call column.GetCellContents to get the contents of the cell. This will give you the CheckBox, and you can attach to CheckChanged at this time. If you do this, you need to do something similar and listen to UnloadingRow so you can detach the eventhandler when the checkbox is scrolled out of view.

这篇关于Silverlight DataGrid 中的复选框行为异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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