如何在WPF的gridview中设置列的背景颜色 [英] How to set the background color of a column in gridview of WPF

查看:557
本文介绍了如何在WPF的gridview中设置列的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的gridview是从SQL中读取的Datatable动态生成的,现在我想设置一个特定的列,比如说,salary有一个红色的背景颜色。我搜索过许多解决方案,但仍然没有线索。

My gridview is dynamically generated from a Datatable read from SQL, now I want to set a particular column, say, "salary" to have a red background color. I've searched SO for many solutions but still have no clues.

CmdString = @"  select id, firstname, lastname, title, level, salary from emplpyees";
            SqlCommand cmd = new SqlCommand(CmdString, con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            dt = new DataTable("employee");
            sda.Fill(dt);
            datagridall.ItemsSource = dt.DefaultView;


推荐答案

您可以创建一个转换器类

You could create a converter class

public class ErrorConverters : IValueConverter
  {
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      SolidColorBrush myBrush = default(SolidColorBrush);

      if ((bool)value == true)
      {
        myBrush = new SolidColorBrush(Colors.Red);
      }
      else
      {
        myBrush = new SolidColorBrush(Colors.Black);
      }

      return myBrush;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      //Throw New NotImplementedException()
      return null;
    }
  }

然后使用行模板将背颜色绑定到任何您的数据库中的项目将被传递到转换器。

Then using a row template bind the back color to whatever item in your database would be passed to the converter.

这篇关于如何在WPF的gridview中设置列的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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