更改WPF Datagrid行颜色 [英] Change WPF Datagrid Row Color
本文介绍了更改WPF Datagrid行颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
现在我想根据程序开始的行内容对行进行着色,如果有改变在运行时。
System.Windows.Controls.DataGrid areaDataGrid = ...;
ObservableCollection< Area>区域;
//向区域集合添加项目
areaDataGrid.ItemsSource = areas;
areaDataGrid.Rows< - 属性不可用。如何访问行?
CollectionView myCollectionView =(CollectionView)CollectionViewSource.GetDefaultView(areaDataGrid.Items);
((INotifyCollectionChanged)myCollectionView).CollectionChanged + = new NotifyCollectionChangedEventHandler(areaDataGrid_Changed);
...
void areaDataGrid_Changed(object sender,NotifyCollectionChangedEventArgs e)
{
//如何访问更改的行?
}
如何在开始和运行时访问行?
解决方案
使用 RowStyle
。您可以使用 触发器
有条件地更改颜色,或者只是将其绑定到您的项目上的画笔
属性,并分别更改该属性。
I have a WPF datagrid that is filled with an ObserverableCollection.
Now I want to color the rows depending on the row content at the program start and if something changes during runtime.
System.Windows.Controls.DataGrid areaDataGrid = ...;
ObservableCollection<Area> areas;
//adding items to areas collection
areaDataGrid.ItemsSource = areas;
areaDataGrid.Rows <-- Property not available. how to access rows here?
CollectionView myCollectionView = (CollectionView)CollectionViewSource.GetDefaultView(areaDataGrid.Items);
((INotifyCollectionChanged)myCollectionView).CollectionChanged += new NotifyCollectionChangedEventHandler(areaDataGrid_Changed);
...
void areaDataGrid_Changed(object sender, NotifyCollectionChangedEventArgs e)
{
//how to access changed row here?
}
How can I access the rows at start and runtime?
解决方案
Use the RowStyle
. You can use the Triggers
to change the color conditionally, or just bind it to a Brush
property on your items and change that property respectively.
这篇关于更改WPF Datagrid行颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文