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

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

问题描述

我有一个WPF数据网格。我需要比较两列类型为datetime的列,并根据比较结果,为当前列和行中的两个单元格设置单元格背景色。我为每个datagrid行执行此操作。为此,我使用了转换器。

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. 返回画笔

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


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

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

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