WPF DataGrid:如何获取单个单元格的内容?
如何在 C# 中获取 WPF 工具包 DataGrid 的单个单元格的内容?
我所说的内容是指其中可能存在的一些纯文本。
How do you get the content of a single cell of a WPF toolkit DataGrid in C#?
By content I mean some plain text that could be in there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
按照 Phillip 的说法 - DataGrid 通常是数据绑定的。 下面是一个示例,其中我的 WPF
DataGrid
绑定到ObservableCollection
,其中PersonName
由FirstName
组成code> 和LastName
(都是字符串)。DataGrid
支持自动列创建,因此该示例非常简单。 您将看到,我可以通过索引访问行,并使用与列名称对应的属性名称来获取该行中单元格的值。Following what Phillip said - the
DataGrid
is usually data-bound. Below is an example where my WPFDataGrid
is bound to anObservableCollection<PersonName>
where aPersonName
is comprised of aFirstName
andLastName
(both strings).The
DataGrid
supports automatic column creation so the example is quite simple. You'll see that I can access rows by their index and get the value of a cell in that row by using the property name that corresponds to the column name.通常,DataGrid 单元格的内容是数据绑定的,因此反映了给定行中显示的对象的属性状态(在大多数情况下)。 因此访问模型可能比访问视图更容易。
话虽如此(访问模型而不是视图),我的问题是:您想做什么? 您是否正在寻找遍历可视化树的方法来查找屏幕上呈现的一个或多个控件? 您希望如何通过行和列索引引用单元格?
Normally, the content of a DataGrid cell is data-bound, and therefore reflect the state of a property (in most cases) of an object which is being displayed in a given row. Hence it might be easier to access the model rather than the view.
Having said that (access model not view) my question is: what are you trying to do? Are you looking of ways to traverse the visual tree to find the control (or controls) that is rendered on screen? How do you expect to reference the cell, by row and column index?
如果使用 DataTable 进行绑定,则可以从 Row 的 Item 属性获取 DataRowView。
If you bind using a DataTable you can get the DataRowView from the Row's Item property.