如何更改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对象,并在滚动视图时循环该行。
源: msdn.microsoft/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 : msdn.microsoft/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
发布评论