wpf 以编程方式为 DataGrid 中的行分配颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1847359/
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-09-08 21:17:57  来源:igfitidea点击:

Programmatically assigning a color to a row in DataGrid

wpfcolorsdatagridrow

提问by Abdul Khaliq

I need to assign a color to the row I add at runtime to the DataTable. How can this be done?

我需要为我在运行时添加到 DataTable 的行分配颜色。如何才能做到这一点?

回答by Jeremy

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.

您可以处理 DataGrid 的 LoadingRow 事件以检测何时添加行。在事件处理程序中,您可以获得对添加到充当您的 ItemsSource 的 DataTable 的 DataRow 的引用。然后您可以随心所欲地更新 DataGridRow 的颜色。

void dataGrid_LoadingRow(object sender, Microsoft.Windows.Controls.DataGridRowEventArgs e)
{
    // Get the DataRow corresponding to the DataGridRow that is loading.
    DataRowView item = e.Row.Item as DataRowView;
    if (item != null)
    {
        DataRow row = item.Row;

            // Access cell values values if needed...
            // var colValue = row["ColumnName1]";
            // var colValue2 = row["ColumName2]";

        // Set the background color of the DataGrid row based on whatever data you like from 
        // the row.
        e.Row.Background = new SolidColorBrush(Colors.BlanchedAlmond);
    }           
}

To sign up for the event in XAML:

要在 XAML 中注册活动:

<toolkit:DataGrid x:Name="dataGrid"
    ...
    LoadingRow="dataGrid_LoadingRow">

Or in C#:

或者在 C# 中:

this.dataGrid.LoadingRow += new EventHandler<Microsoft.Windows.Controls.DataGridRowEventArgs>(dataGrid_LoadingRow);

回答by DarKainSoul

U can try this

你可以试试这个

In the XAML

在 XAML 中

<Window.Resources>
<Style TargetType="{x:Type DataGridRow}">
    <Style.Setters>
        <Setter Property="Background" Value="{Binding Path=StatusColor}"></Setter>
    </Style.Setters>            
</Style>
</Window.Resources>

In the datagrid

在数据网格中

<DataGrid AutoGenerateColumns="False" CanUserAddRows="False" Name="dtgTestColor" ItemsSource="{Binding}" >
<DataGrid.Columns>                            
    <DataGridTextColumn Header="Valor" Binding="{Binding Path=Valor}"/>
</DataGrid.Columns>
</DataGrid>

In the code i have a class with

在代码中,我有一个类

public class ColorRenglon
{
    public string Valor { get; set; }
    public string StatusColor { get; set; }
}

When set the DataContext

设置 DataContext 时

dtgTestColor.DataContext = ColorRenglon;
dtgTestColor.Items.Refresh();

If u not set the color of the row the default value is Grey

如果你没有设置行的颜色,默认值是灰色

u can try this sample with this sample

你可以用这个样本试试这个样本

List<ColorRenglon> test = new List<ColorRenglon>();
ColorRenglon cambiandoColor = new ColorRenglon();
cambiandoColor.Valor = "Aqui va un color"; 
cambiandoColor.StatusColor = "Red";
test.Add(cambiandoColor);
cambiandoColor = new ColorRenglon();
cambiandoColor.Valor = "Aqui va otro color"; 
cambiandoColor.StatusColor = "PaleGreen";
test.Add(cambiandoColor);

回答by Simon_Weaver

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.

请参阅我对C# Silverlight Datagrid - Row Color Change 的回答。

PS. I am in Silverlight and haven't confirmed this behavior in WPF

附注。我在 Silverlight 中,尚未在 WPF 中确认此行为