C# Silverlight Datagrid - 行颜色更改 [英] C# Silverlight Datagrid - Row Color Change

查看:19
本文介绍了C# Silverlight Datagrid - 行颜色更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do you change the color of the silverlight datagrid rows?!

我已经尝试过这个,但它似乎并没有像我想要的那样工作......随机行的颜色不正确:

I've tried this but it doesn't seem to work how I want it to...Random rows get colored incorrectly:

 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);
        }

推荐答案

Microsoft 文档:

为了提高性能,EnableRowVirtualization 属性是默认设置为true.当 EnableRowVirtualization 属性为设置为 true,DataGrid 不会实例化 DataGridRow 对象绑定数据源中的每个数据项.相反,DataGrid 创建DataGridRow 仅在需要时才对象,并尽可能多地重用它们能够.例如,DataGrid 为每个数据创建一个 DataGridRow 对象当前在视图中的项目并在它滚动时回收该行的观点.

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 事件来取消您设置的样式.

the proper (though not easier I admit) solution being, hence, to use the UnloadingRow event to unset the style you had set.

这篇关于C# Silverlight Datagrid - 行颜色更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆