C#Silverlight Datagrid - 行颜色变化 [英] C# Silverlight Datagrid - Row Color Change

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

问题描述

如何更改silverlight datagrid行的颜色?



我已经尝试过了,但是似乎没有办法。 ..Random行颜色不正确:

  void dataGrid1_LoadingRow(object sender,DataGridRowEventArgs e)
{
var c = e.Row.DataContext作为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属性的默认值为
。当EnableRowVirtualization属性的
设置为true时,DataGrid不会为绑定数据源中的每个数据项
实例化一个DataGridRow对象。相反,DataGrid仅在需要时才创建
DataGridRow对象,并重新使用
即可。例如,DataGrid为当前正在查看的每个数据
项目创建一个DataGridRow对象,并在滚动
视图时循环该行。


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



这解释了您所经历的行为



正确的(尽管不容易)我的解决方案是,因此,使用UnloadingRow事件来取消设置您设置的样式。 >

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 Documentation :

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.

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

this explains the behaviour you have been experiencing

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天全站免登陆