获取数据网格中的行 [英] Get row in datagrid

查看:23
本文介绍了获取数据网格中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图得到这样的行:

DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(i);
TextBlock cellContent = dataGrid.Columns[0].GetCellContent(row) as TextBlock;

但我只有null.还有其他解决方案吗?我做错了什么?

But I only got null. Is there another solution? What am I doing wrong?

我想从我的单元格中获取数据.我的单元格是复选框.

I want to get data from my cells. My cells are checkboxes.

推荐答案

这取决于您尝试获取此数据的方式/时间.WPF 更适合通过绑定在 ItemsSource 中的对象访问数据.因此,如果您的 ItemsSource 是 MyObject 的列表,那么特定行的类型将是 MyObject 而不是纯 DataRow.

This depends on how / when you are attempting to get this data. WPF is more geared towards accessing the data by the objects bound in the ItemsSource. So, if your ItemsSource is a List of MyObject, then the specific row will be of type MyObject instead of a pure DataRow.

如果您通过点击来访问数据,您可以执行以下操作:

If you are accessing the data by means of clicking on it, you can do something like this:

var currentItem = myDataGrid.SelectedItem as MyObject;

现在,您拥有当前 MyObject 的原始预期形式,而不是在网格上进行选择.

Now, you have the current MyObject in it's originally intended form rather than picking at the grid.

这篇关于获取数据网格中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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