基于单元格值的 DataGrid 行背景 [英] DataGrid Row Background Based On Cell Value

查看:21
本文介绍了基于单元格值的 DataGrid 行背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究 C# WPF 数据网格.我有一个 DataGrid,它具有自动生成的列,代码连接到 SQLite 数据库并创建一个数据集,然后将此数据集设置为 DataGrid ItemsSource.

I am currently working on a C# WPF datagrid. I have a DataGrid which has auto generated columns and the code connects to an SQLite Database and creates a dataset and then this dataset is set as the DataGrid ItemsSource.

下面是DataGrid的XAML代码

Below is the code with the XAML of the DataGrid

<DataGrid AutoGenerateColumns="True"
          Margin="12,71,12,32"
          Name="tblLog"
          ColumnWidth="*"
          CanUserResizeRows="False"
          AreRowDetailsFrozen="False"
          CanUserAddRows="True"
          CanUserDeleteRows="True"
          IsReadOnly="True"
          MouseDoubleClick="tblLog_MouseDoubleClick">                
</DataGrid>

下面是为DataGrid设置ItemsSource的代码

And below is the code to set the ItemsSource for the DataGrid

try
{
    DataSet ds = new DataSet();
    SQLiteDataAdapter da = new SQLiteDataAdapter(query, db.conn);
    da.Fill(ds);

    //tblGrid.AutoGenerateColumns = true;
    tblGrid.ItemsSource = ds.Tables[0].DefaultView;                    
}
catch (SQLiteException ex)
{
    MessageBox.Show("Unable to retrieve logins from database.

" + ex.Message + "

Error Code: " + ex.ErrorCode);
}

数据库中显示的列(自动生成)是 ID、日期、时间、状态.我需要能够做的是,如果状态列的一行中的值等于 Error 更改该行的背景颜色.

The columns that are shown in the database (auto generated) are ID, date, time, status. What I need to be able to do is if the value in a row of the status column equals Error change the background colour of that row.

我假设我需要在 DataGrid 标记中添加某种样式标记和 DataTriggers,但不确定我需要什么.我对设置 ItemsSource 的代码所做的任何尝试都会显示一个错误,指出在添加 ItemsSource 之前,Source 需要为空.

I assume I need to add some sort of styling tags and DataTriggers within the DataGrid tags but not sure what I need. Anything I have tried to the code that sets the ItemsSource displays an error saying that the Source needs to be empty before adding the ItemsSource.

感谢您提供的任何帮助.

Thanks for any help you can provide.

推荐答案

您可以使用 DataTrigger 来做到这一点.

You can use a DataTrigger to do this.

这是一个快速示例.我创建了一个名为 Person 的类,具有 Name、Age 和 Active 属性.

Here is a quick sample. I created a class called Person with the properties Name, Age, and Active.

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public bool Active { get; set; }
}

在主窗口的构造函数中,我将 3 个 Person 对象添加到一个列表中,然后将该列表绑定到 DataGrid.

In the constructor of the main window, I add 3 Person objects to a list, then bind that list to the DataGrid.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        List<Person> people = new List<Person>();
        people.Add(new Person() 
        { 
            Name = "John Doe",
            Age = 32,
            Active = true
        });
        people.Add(new Person()
        {
            Name = "Jane Doe",
            Age = 30,
            Active = true
        });
        people.Add(new Person()
        {
            Name = "John Adams",
            Age = 64,
            Active = false
        });
        tblLog.ItemsSource = people;
    }
}

然后在 MainWindow 的 XAML 中,我创建一个 DataTrigger 样式作为资源.

Then in the XAML for the MainWindow, I create a DataTrigger style as a resource.

<Window.Resources>
    <Style TargetType="DataGridRow">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Active}" Value="False">
                <Setter Property="Background" Value="Red" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

此触发器的作用是从 DataGridRow 中的 Person 对象的 Active 字段中获取值,如果该值是 false,则它会变为将行的背景颜色改为红色.

What this trigger does is it takes the value from the Active field from the Person object that is in the DataGridRow, and if that value is false, then it turns to background color of the row to red.

这篇关于基于单元格值的 DataGrid 行背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆