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

发布于 2024-08-13 09:31:19 字数 35 浏览 2 评论 0原文

我需要为运行时添加到数据表的行分配颜色。这怎么能做到呢?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

零度° 2024-08-20 09:31:19

您可以处理 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);
    }           
}

要在 XAML 中注册活动:

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

或在 C# 中:

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

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.

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:

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

Or in C#:

this.dataGrid.LoadingRow += new EventHandler<Microsoft.Windows.Controls.DataGridRowEventArgs>(dataGrid_LoadingRow);
等待圉鍢 2024-08-20 09:31:19

尝试这个

在XAML中

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

在数据网格中

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

在代码中我有一个类

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

当设置DataContext时

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

如果您没有设置行的颜色默认值为灰色

尝试这个示例
与这个样本

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);

Try this

In the 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

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

If you have not set the color of the row the default value is Grey

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);
逆蝶 2024-08-20 09:31:19

重要:请务必始终为未按条件或任何其他样式着色的行分配默认值。

请参阅我对 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文