0
ここではこのコードのデプロイメントを使用しています。しかし、GetCellでのキャストは行内でnullになり、セルの色を変更できません。私がしたいことは、セルを取得し、その値に応じてセルの色を赤または緑に変更することです。データグリッドの行の特定のセルに色を変更します。
public void ColorChange()
{
DataGridCell cell = GetCell(11, 1, dgLectura);
cell.Background = new SolidColorBrush(Colors.Red);
}
public DataGridCell GetCell(int rowIndex, int columnIndex, DataGrid dg)
{
//DataGridRow row = dg.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow;
DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(rowIndex);
if (row == null)
{
dg.UpdateLayout();
dg.ScrollIntoView(dg.Items[rowIndex]);
row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(rowIndex);
}
DataGridCellsPresenter p = GetVisualChild<DataGridCellsPresenter>(row);
DataGridCell cell = p.ItemContainerGenerator.ContainerFromIndex(columnIndex) as DataGridCell;
return cell;
}
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;
}