0
我想编辑单元格而不是其值,但是它的背景颜色。我知道rowIndex和columnIndex。但要通过电网是一个很难的部分。我只是希望像以编程方式编辑wpf中的datagrid单元格
DataGrid.Rows [0] [3] .BackgroundColor = WhateverIWant
与VisualTreeHelper的帮助会更加循环,但亲切指导我吧。
感谢
我想编辑单元格而不是其值,但是它的背景颜色。我知道rowIndex和columnIndex。但要通过电网是一个很难的部分。我只是希望像以编程方式编辑wpf中的datagrid单元格
DataGrid.Rows [0] [3] .BackgroundColor = WhateverIWant
与VisualTreeHelper的帮助会更加循环,但亲切指导我吧。
感谢
使用下面的方法:
public static DataGridCell GetDataGridCell(DataGrid grid, int rowIndex, int colIndex)
{
DataGridCell result = null;
DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(rowIndex);
if (row != null)
{
DataGridCellsPresenter presenter = GetFirstVisualChild<DataGridCellsPresenter>(row);
result = presenter.ItemContainerGenerator.ContainerFromIndex(colIndex) as DataGridCell;
}
return result;
}
public static T GetFirstVisualChild<T>(DependencyObject depObj)
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
return (T)child;
}
T childItem = GetFirstVisualChild(child);
if (childItem != null) return childItem;
}
}
return null;
}
你也可以把它作为扩展方法上的DataGrid