C# Silverlight Datagrid - 行颜色更改

标签 c# silverlight datagrid row

如何更改 silverlight 数据网格行的颜色?!

我已经试过了,但它似乎并没有按照我想要的方式工作......随机行的颜色不正确:

 void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            var c = e.Row.DataContext as Job;
            if (c != null && c.Status.Contains("complete"))
                e.Row.Background = new SolidColorBrush(Colors.Green);
            else
                e.Row.Background = new SolidColorBrush(Colors.Red);
        }

最佳答案

微软文档:

To improve performance, the EnableRowVirtualization property is set to true by default. When the EnableRowVirtualization property is set to true, the DataGrid does not instantiate a DataGridRow object for each data item in the bound data source. Instead, the DataGrid creates DataGridRow objects only when they are needed, and reuses them as much as it can. For example, the DataGrid creates a DataGridRow object for each data item that is currently in view and recycles the row when it scrolls out of view.

来源:http://msdn.microsoft.com/en-gb/library/system.windows.controls.datagrid.unloadingrow.aspx

这解释了你一直在经历的行为

因此,正确的(虽然我承认并不容易)解决方案是使用 UnloadingRow 事件来取消设置您设置的样式。

关于C# Silverlight Datagrid - 行颜色更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1509714/

相关文章:

c# - .Net Core 2.1 中的动态代码编译

c# - 在队列中播放 .wav 文件

.net - WPF、XBAP、Silverlight...哪个适合业务应用程序?

silverlight - WP7 ProgressBar高度和颜色问题

javascript - 如何在数据网格中显示由具有属性和值的对象组成的数组?

vb.net - 如何在 GridView 中为网格单元格着色?

javascript - Infragistics网络货币编辑四舍五入问题

c# - 使用 C# 检索 API 数据

Silverlight - 需要很棒的演示应用程序示例!

wpf - DataGrid:关于单元格验证错误,其他行单元格不可编辑/只读