How to get a cell from DataGrid?

前端 未结 1 607
伪装坚强ぢ
伪装坚强ぢ 2021-01-27 03:23

I have a DataGrid (.net framework 3.5, WPFToolKit). And i want to change borders (left or right) of some cells. One, two or three. So how can I get an access to a single cell? A

相关标签:
1条回答
  • 2021-01-27 04:18

    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.

    0 讨论(0)
提交回复
热议问题