How to get a cell from DataGrid?

故事扮演 提交于 2019-12-02 07:27:26
denis morozov

You can extend the DataGrid and add the following, NOTE: it's just a sample, you don't need to do some of the processing that I am doing:

public DataGridCell GetCell(int row, int column)
    {
        var rowContainer = GetRow(row);

        if (rowContainer != null)
        {
            var presenter = FindVisualChild<DataGridCellsPresenter>(rowContainer);
            if (presenter == null)
                return null;

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

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

For the definition of FindVisualChild, look at this site.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!