C#的Silverlight的Datagrid - 行变色 [英] C# Silverlight Datagrid - Row Color Change
问题描述
嘿!
你如何改变Silverlight的DataGrid行的颜色?!
我试过这个,但它似乎没有工作,我怎么想...随机获得行彩色正确:
无效dataGrid1_LoadingRow(对象发件人,DataGridRowEventArgs E)
{
变种C = e.Row.DataContext的工作;
如果(C =空&安培;!&安培; c.Status.Contains(完全))
e.Row.Background =新的SolidColorBrush(Colors.Green);
,否则
e.Row.Background =新的SolidColorBrush(Colors.Red);
}
微软文档:
要提高性能,EnableRowVirtualization属性为
设置为true默认情况下。当EnableRowVirtualization属性为
设置为true,DataGrid不实例化
在绑定的数据源中的每个数据项的DataGridRow对象。相反,DataGrid中创建
DataGridRow对象仅在需要时,他们和他们重用尽可能多地
能。例如,DataGrid中会为目前在视图和回收行的时候它滚动出视野
每一个数据
项的DataGridRow对象。
块引用>
来源:的http:// msdn.microsoft.com/en-gb/library/system.windows.controls.datagrid.unloadingrow.aspx
这说明你已经遇到的行为
正确的(虽然不容易,我承认)解决方案之中,因此,使用UnloadingRow事件来取消你已经设置的样式。
Hey! 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屋!