在 WPF 中获取 DataGrid 中单元格的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13204536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:03:43  来源:igfitidea点击:

Getting value of cell in DataGrid in WPF

c#wpfdatagrid

提问by Omid

For WinForms it's:

对于 WinForms,它是:

var value = DataGridView.Rows[0].Cells[0].Value

Is there any way to get it in WPF?

有没有办法在WPF中获得它?

回答by dwonisch

I think the best way would be to use the Items property and directly access your data item:

我认为最好的方法是使用 Items 属性并直接访问您的数据项:

var dataItem = dataGrid.Items[0] as ...;

But You can use this class to get the cell and access the value with the GetValue() method (would be more like your example).

但是您可以使用此类来获取单元格并使用 GetValue() 方法访问该值(更像是您的示例)。

Code taken from here: datagrid get cell index

代码取自此处:datagrid get cell index

static class DataGridHelper {
    static public DataGridCell GetCell(DataGrid dg, int row, int column) {
        DataGridRow rowContainer = GetRow(dg, row);

        if (rowContainer != null) {
            DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);

            // try to get the cell but it may possibly be virtualized
            DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            if (cell == null) {
                // now try to bring into view and retreive the cell
                dg.ScrollIntoView(rowContainer, dg.Columns[column]);
                cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            }
            return cell;
        }
        return null;
    }

    static public DataGridRow GetRow(DataGrid dg, int index) {
        DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
        if (row == null) {
            // may be virtualized, bring into view and try again
            dg.ScrollIntoView(dg.Items[index]);
            row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
        }
        return row;
    }

    static T GetVisualChild<T>(Visual parent) where T : Visual {
        T child = default(T);
        int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < numVisuals; i++) {
            Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
            child = v as T;
            if (child == null) {
                child = GetVisualChild<T>(v);
            }
            if (child != null) {
                break;
            }
        }
        return child;
    }
}

回答by Eren Ers?nmez

Generally, you shouldn't need to do that. In WPF, datagrid is meant to be used with data binding, which means there is an underlying collection or object that has the same value as the cell, so you need to access that collection/object directly. If you are needing to access the cell value, you might need to reconsider your design.

通常,您不需要这样做。在 WPF 中,datagrid 旨在与数据绑定一起使用,这意味着存在与单元格具有相同值的底层集合或对象,因此您需要直接访问该集合/对象。如果您需要访问单元格值,您可能需要重新考虑您的设计。