WPF Change datagrid cell background color using a converter

若如初见. 提交于 2020-01-30 03:47:54

问题


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>

The converter:

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.");
    }
}

Here I have two problems:

  1. When date1 > date2 cell background color is not being update to brown.
  2. In case date1 <= date2, the default datagrid cell background color should be returned and I do not know how to do this.

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:


  1. Return a Brush:

    if (date1.Date > date2.Date)
    {
        return System.Windows.Media.Brushes.Brown;
    }
    
  2. Return System.Windows.Data.Binding.DoNothing.



来源:https://stackoverflow.com/questions/47871745/wpf-change-datagrid-cell-background-color-using-a-converter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!