以编程方式为 DataGrid 中的行分配颜色
我需要为运行时添加到数据表的行分配颜色。这怎么能做到呢?
I need to assign a color to the row I add at runtime to the DataTable. How can this be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以处理 DataGrid 的 LoadingRow 事件来检测何时添加行。在事件处理程序中,您可以获得对添加到充当 ItemsSource 的 DataTable 的 DataRow 的引用。然后您可以根据需要更新 DataGridRow 的颜色。
要在 XAML 中注册活动:
或在 C# 中:
You can handle the DataGrid's LoadingRow event to detect when a row is being added. In the event handler you can get a reference to the DataRow that was added to the DataTable that is acting as your ItemsSource. Then you can update the DataGridRow's color however you like.
To sign up for the event in XAML:
Or in C#:
尝试这个
在XAML中
在数据网格中
在代码中我有一个类
当设置DataContext时
如果您没有设置行的颜色默认值为灰色
尝试这个示例
与这个样本
Try this
In the XAML
In the datagrid
In the code i have a class with
When set the DataContext
If you have not set the color of the row the default value is Grey
Try this sample
with this sample
重要:请务必始终为未按条件或任何其他样式着色的行分配默认值。
请参阅我对 C# Silverlight Datagrid - 行颜色更改的回答。
附言。我在 Silverlight 中,尚未在 WPF 中确认此行为
IMPORTANT : be sure to always assign defaults for rows that aren't being colored by a condition - or any other style.
See my answer to C# Silverlight Datagrid - Row Color Change.
PS. I am in Silverlight and haven't confirmed this behavior in WPF