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

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

问题描述

我目前工作的一个C#WPF DataGrid中。我有一个具有自动生成列和代码连接到SQLite数据库,并创建一个数据集,然后将此数据设置为数据网格的ItemsSource一个DataGrid。

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>

和下面是设置的ItemsSource为DataGrid

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.\n\n" + ex.Message + "\n\nError Code: " + ex.ErrorCode);
}

这在数据库中所示的列(自动生成的)是ID,日期,时间,状态。
我需要能够做的是,如果在状态列中各行的值等于错误更改该行的背景颜色。

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.

我想我需要添加某种风格标签和DataTriggers的DataGrid的标签内,但不知道我需要什么。任何事情我都试图在设置的ItemsSource的代码显示一个错误,指出来源必须添加的ItemsSource前空的。

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与性能的姓名,年龄和活动类。

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 对象到一个列表,然后绑定该列表到的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;
    }
}



然后在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,如果该值是假的,那么它变成了排红色的背景色域。

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天全站免登陆