[英]Change color to a specific cell in a row of a datagrid
I am using this code deployment on here. 我在这里使用此代码部署。 But the casting in GetCell, get null in the row and fails to change the color of the cell. 但是在GetCell中进行强制转换,在行中获取null并无法更改单元格的颜色。 What I want to do is get the cell, then according to their value to change the cell color to red or green. 我要做的是获取单元格,然后根据其值将单元格颜色更改为红色或绿色。
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;
}
This worked for me: 这对我有用:
I used here a MultiConverter here in order to compare two data grids and i changed the color in each cell that the values where different. 为了比较两个数据网格,我在这里使用了一个MultiConverter,并且在每个单元格中更改了值不同的颜色。 But you can use this for your scenario. 但是您可以将其用于您的方案。
public class NameToBrushMultiValueConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var dataContext = values[0];
var dg = (DataGrid)values[1];
var i = (DataGridCell)values[2];
var col = i.Column.DisplayIndex;
var row = dg.Items.IndexOf(i.DataContext);
if (row >= 0 && col >= 0)
{
DataTable td1 = ((CheckXmlAppWpf.ViewModel.MainWindowViewModel)(dataContext)).GenericDataTable;
DataTable td2 = ((CheckXmlAppWpf.ViewModel.MainWindowViewModel)(dataContext)).GenericDataTable2;
if (!td1.Rows[row][col].Equals(td2.Rows[row][col]))
{
GetCell(dg, row, col).Background = Brushes.Yellow;
}
}
return SystemColors.AppWorkspaceColor;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new System.NotImplementedException();
}
public DataGridCell GetCell(DataGrid dg, int row, int column)
{
DataGridRow rowContainer = GetRow(dg, row);
if (rowContainer != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
if (presenter == null)
{
dg.ScrollIntoView(rowContainer, dg.Columns[column]);
presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
}
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
return cell;
}
return null;
}
public static DataGridRow GetRow(DataGrid dg, int index)
{
DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
if (row == null)
{
dg.UpdateLayout();
dg.ScrollIntoView(dg.Items[index]);
row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
}
return row;
}
public static T GetVisualChild<T>(Visual parent) where T : Visual
{
if (parent == null) return null;
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.