WPF 使用转换器更改数据网格单元格背景颜色 [英] WPF Change datagrid cell background color using a converter

查看:29
本文介绍了WPF 使用转换器更改数据网格单元格背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WPF 数据网格.我需要比较两列日期时间类型,并根据比较结果,为当前列和行中的两个单元格设置单元格背景颜色.我为每个数据网格行执行此操作.为了做到这一点,我使用了转换器.

I have an WPF datagrid. There are two columns of type datetime which I need to compare and according to the compare result, I set a cell background color for the two cells in the current column and row. I do this for each datagrid row. In order to do this I use a converter.

<my:DataGridTextColumn Binding="{Binding Path=Date1, StringFormat={0:dd/MM/yyyy}}" Header="Date">
    <my:DataGridTextColumn.ElementStyle>
        <Style TargetType="TextBlock">
            <Setter Property="Background">
                <Setter.Value>
                    <MultiBinding Converter="{StaticResource CellDateColorConverter}">
                        <Binding Path="Date1"/>
                        <Binding Path="Date2"/>
                    </MultiBinding>
                </Setter.Value>
            </Setter>
         </Style>
    </my:DataGridTextColumn.ElementStyle>
</my:DataGridTextColumn>

<my:DataGridTextColumn Binding="{Binding Path=Date2, StringFormat={0:dd/MM/yyyy}}" Header="Date">
    <my:DataGridTextColumn.ElementStyle>
        <Style TargetType="TextBlock">
            <Setter Property="Background">
                <Setter.Value>
                    <MultiBinding Converter="{StaticResource CellDateColorConverter}">
                        <Binding Path="Date1"/>
                        <Binding Path="Date2"/>
                    </MultiBinding>
                </Setter.Value>
            </Setter>
         </Style>
    </my:DataGridTextColumn.ElementStyle>
</my:DataGridTextColumn>

转换器:

public class CellDateColorConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (values[0] is DateTime && values[1] is DateTime)
        {
            DateTime date1 = (DateTime)values[0];
            DateTime date2= (DateTime)values[1];                

            if (date1.Date > date2.Date)
            {
                return Color.Brown;
            }
        }

        return ????? // I need to return the default datagrid cell's background color. How to do this?
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException("CellDateColorConverter is a OneWay converter.");
    }
}

这里我有两个问题:

  1. 当 date1 > date2 单元格背景颜色未更新为棕色时.
  2. 如果 date1 <= date2,则应返回默认的 datagrid 单元格背景颜色,但我不知道该怎么做.

我还为数据网格定义了行样式.行样式根据某些条件设置整行的背景颜色.但是在这种情况下,这些条件不满足,但上面的列样式(date1.Date > date2.Date)满足,所以单元格背景应该涂成棕色.

Also I have defined a row style for the datagrid. The row style sets the entire row background color according to some conditions. But in this case, these conditions are not satisfied but the above column style (date1.Date > date2.Date) does, so cell background should be painted with brown.

利用这个帖子,如果满足行样式的条件,并且整个背景设置为例如橙色,如果单元格列样式(在此帖子中)也满足并且需要涂成棕色,那么哪个占上风?行样式还是单元格样式?

Taking advantage of this post, in case conditions for row style are satisfied, and entire background is set for example to orange, if cell column style (above in this post) is also satisfied and need to be painted in brown, then which prevails? the row style or cell style?

推荐答案

  1. 返回一个Brush:

if (date1.Date > date2.Date)
{
    return System.Windows.Media.Brushes.Brown;
}

  • 返回System.Windows.Data.Binding.DoNothing.

    这篇关于WPF 使用转换器更改数据网格单元格背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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