如何从WPF中的数据网格中获取单元格的值? [复制]

2024-05-20

可能的重复:
从 DataGrid 中选择 DataGridCell https://stackoverflow.com/questions/9978119/select-datagridcell-from-datagrid

我在 WPF 中有一个数据网格,其中包含一些列和行。当我单击一行时,我想获取所选行的第一列。我该怎么做?我可以使用 LINQ 吗? 谢谢


您可以简单地使用这个扩展方法 -

public static DataGridRow GetSelectedRow(this DataGrid grid)
{
    return (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem);
}

您可以通过现有的行和列 ID(在您的情况下为 0)获取 DataGrid 的单元格:

public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int column)
{
    if (row != null)
    {
        DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);

        if (presenter == null)
        {
            grid.ScrollIntoView(row, grid.Columns[column]);
            presenter = GetVisualChild<DataGridCellsPresenter>(row);
        }

        DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
        return cell;
    }
    return null;
}

检查此链接了解详细信息 -获取 WPF DataGrid 行和单元格 http://techiethings.blogspot.in/2010/05/get-wpf-datagrid-row-and-cell.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从WPF中的数据网格中获取单元格的值? [复制] 的相关文章