DataGridTemplateColumn 获取单元格的值
我正在使用 WPF 工具包中的 WPF DataGrid
。
我向 DataGrid
添加了一个模板化列,其中每个单元格中都有一个 CheckBox
。现在如何访问这些单元格中的值?
DataGrid
中的其他列来自 DataSet
。我可以访问这些,但无法获取添加到 DataGrid
的 DataGridTemplateColumn
的值。
有人有什么想法吗?
I'm using the WPF DataGrid
from the WPF Toolkit.
I added a templated column to my DataGrid
, which has a CheckBox
in each cell. Now how do I access the values within these cells?
My other columns in the DataGrid
come from a DataSet
. I can access these, but I cannot get to the values of the DataGridTemplateColumn
I added to the DataGrid
.
Anyone have any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
现在你要从视觉树中取出东西了。这是一项艰苦的工作,您找不到绑定,因为它被埋在单元格模板中。我所做的就是为此类内容添加我自己的列,该列派生自 DataGridBoundColumn,这意味着它具有像所有其他列一样的绑定:(我不久前写过它,它可能需要一些查看)这让我只使用直装订。我不必设置单元格模板,我可以使用我更喜欢的 DataTemplate。
现在,如果您可以到达该列,您就可以到达该列上的绑定。如果您可以到达该单元格,那么您就可以找到数据项(行数据)。然后我要做的就是按照绑定来获取单元格值。它确实效率低下,而且是一种 hack。但它有效。为了遵循绑定,我使用这个。
最后一部分是一个名为 BindingEvaluator 的辅助类,它有一个 dp,我用它来跟踪绑定
,我这样称呼它:
your into pulling stuff out of the visual tree now. and thats hard work, you cant find the binding because that is buried in the cell template. what i did was add my own column for this kind of stuff, the column derives from DataGridBoundColumn, which means it has a binding like all the others: ( i wrote it a while ago, it could probably do with some looking at ) This lets me just use a straight binding. i dont have to set a cell template, i can just use a DataTemplate which i like better.
now if you can get to the column you can get to the binding on the column. if you can get to the cell then you can find the data item (the row data). then what i do is follow the binding to get the cell value. it is really inefficient, and it is a hack. but it works. to follow the binding i use this.
and the last piece is a helper class called BindingEvaluator which has one dp, that i use to follow the binding
and i call it like so: