C# 更改 WPF 数据网格行颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10056657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:16:51  来源:igfitidea点击:

Change WPF Datagrid Row Color

c#wpfdatagridcolorsrow

提问by juergen d

I have a WPF datagrid that is filled with an ObserverableCollection.

我有一个充满 ObserverableCollection 的 WPF 数据网格。

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?

如何在启动和运行时访问行?

采纳答案by H.B.

Use the RowStyle. You can use the Triggersto change the color conditionally, or just bind it to a Brushproperty on your items and change that property respectively.

使用RowStyle. 您可以使用Triggers来有条件地更改颜色,或者只是将其绑定到Brush您的项目上的属性并分别更改该属性。

回答by itman

To change it through code instead of a trigger it could look something like below. You can access the data as an array and then compare it. In this example I am comparing the 4th column to see if it is greater than 0 and the 5th column to see if it is less than 0 otherwise just paint it the default color. The try/catch it there becuase some logic needs to be added to see if it is a valid row or not.....or you can just ignore the errors as below (not really good practice though) but should be usable as is.

要通过代码而不是触发器来更改它,它可能如下所示。您可以将数据作为数组访问,然后进行比较。在这个例子中,我比较第 4 列以查看它是否大于 0,并比较第 5 列以查看它是否小于 0,否则只需将其绘制为默认颜色。在那里尝试/捕获它,因为需要添加一些逻辑以查看它是否是有效行.....或者您可以忽略如下错误(尽管不是很好的做法)但应该可以按原样使用.

    private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
    {
        try
        {
            if (Convert.ToDouble(((System.Data.DataRowView)(e.Row.DataContext)).Row.ItemArray[3].ToString()) > 0)
            {
                e.Row.Background = new SolidColorBrush(Colors.Green);
            }
            else if (Convert.ToDouble(((System.Data.DataRowView)(e.Row.DataContext)).Row.ItemArray[4].ToString()) < 0)
            {
                e.Row.Background = new SolidColorBrush(Colors.Red);
            }
            else
            {
                e.Row.Background = new SolidColorBrush(Colors.WhiteSmoke);
            }
        }
        catch
        {
        } 
    }