Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更改WPF Datagrid中单个单元格的颜色_C#_Wpf_.net 4.5 - Fatal编程技术网

C# 更改WPF Datagrid中单个单元格的颜色

C# 更改WPF Datagrid中单个单元格的颜色,c#,wpf,.net-4.5,C#,Wpf,.net 4.5,我有一个列数可变的WPF数据网格,在运行时用字符串[]的2D数组填充 String[] data = .....; datagrid.ItemsSource = data; 我需要根据它的值选择单元格颜色。但这些值是由用户在执行期间定义的。我发现的大多数关于更改单元格颜色的示例都在XAML上使用触发器,并且该值在设计时已知 所以我改变了手机的背景如下: DataGridCell cell = DataGridUtils.GetCell(datagrid, i, j); cell.Backg

我有一个列数可变的WPF数据网格,在运行时用字符串[]的2D数组填充

String[] data = .....;

datagrid.ItemsSource = data;
我需要根据它的值选择单元格颜色。但这些值是由用户在执行期间定义的。我发现的大多数关于更改单元格颜色的示例都在XAML上使用触发器,并且该值在设计时已知

所以我改变了手机的背景如下:

DataGridCell cell = DataGridUtils.GetCell(datagrid, i, j);

cell.Background = new SolidColorBrush(Colors.DarkGreen);
以及GetCell函数:

private DataGridCell GetCell(int rowIndex, int colIndex, DataGrid dg)
{
    DataGridRow row = dg.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow;
    DataGridCellsPresenter p = GetVisualChild<DataGridCellsPresenter>(row);
    DataGridCell cell = p.ItemContainerGenerator.ContainerFromIndex(colIndex) as DataGridCell;

    return cell;
}
私有DataGridCell GetCell(int-rowIndex、int-colIndex、DataGrid dg) { DataGridRow row=dg.ItemContainerGenerator.ContainerFromIndex(rowIndex)作为DataGridRow; DataGridCellsPresenter p=GetVisualChild(行); DataGridCell单元格=p.ItemContainerGenerator.ContainerFromIndex(colIndex)作为DataGridCell; 返回单元; } 乍一看,它似乎运行正常,我可以看到该值为绿色的单元格。但是,如果我一直向下滚动数据网格,然后以绿色返回单元格,则会做另一个随机单元格,并且更多单元格变为绿色。如果我继续上下移动,绿色的单元格就会随机变化

我知道这听起来很奇怪,但在代码中只有一个地方可以改变单元格的颜色,那就是点击按钮。我不知道在滚动数据网格时这是怎么发生的

我在某处读到ItemContainerGenerator这不是一个优雅的解决方案,应该避免。但这是我成功的唯一方法


有没有更好的方法来改变单元格的背景色?我可以在不知道设计时的值的情况下使用触发器执行此操作吗?如果是这样,如何?

DataGrid
虚拟化模式默认为
回收
,这意味着它将重用生成的
数据单元
,以提高性能。它不必创建n次控件

将此添加到您的
DataGrid

VirtualizingStackPanel.VirtualizationMode="Standard"

请注意,这将影响您的性能,WPF将创建n倍的控制。

您无需通过将
virtualizengmode
设置为
标准
来放弃良好的虚拟化功能。您可以处理事件
LoadingRow
来更新那里的背景,也可以处理
unloadinggrow
来将背景恢复到某个默认值。我刚刚在一个
DataGridRow
(不是
DataGridCell
)上测试了它,但它证明了这种方法是非常有前景的。虚拟化有它自己的方法来处理这个问题。@KingKing当你快速滚动网格并注意到背景滞后时,你会遇到问题。@ll从某种意义上说,这是可以接受的。然而,优点是节省内存(因为虚拟化仍在使用),而转向虚拟化的缺点是消耗大量内存。所以遵循这个或那个取决于一些标准。试图快速滚动网格是不正常的。如果你总是尝试这样快速地做一些事情,你会觉得WPF实际上没有很好的表现(就像广告上所说的那样)。一个简单的快速例子让我们不满意:尝试尽可能快地调整窗口大小,它有多闪烁?至少在Windows7和几乎所有的Windows上都会发生这种情况。@KingKing我同意。但是,如果
数据单元
依赖于序列化,并且需要
数据单元
的颜色应用于正确的背景,该怎么办<代码>加载/卸载
事件在这里对您没有帮助。这总是一种权衡,取决于他的用例,如果有什么得失。