C#Silverlight Datagrid

编程入门 行业动态 更新时间:2024-10-27 07:25:11
本文介绍了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对象,并在滚动视图时循环该行。

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

本文发布于:2023-11-11 10:13:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1578137.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Silverlight   Datagrid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!