Search code examples
c#wpfdatagridxceed

Xceed datagrid - change datarow colour based on DateTime value


I have a datagrid (Xceed community edition version 3.1.0) containing general invoice information. One of the fields contains the due date of the invoice. I want to have a different colour for the rows with an expired due date. To accomplish this I'm using a value converter. Here's my XAML:

<dg:DataGridControl.Resources>
    <Style TargetType="{x:Type dg:DataRow}" BasedOn="{StaticResource DefaultDataRowStyle}">
        <Setter Property="Background" Value="{Binding DocumentDueDate, Converter={StaticResource DueDateToBrushConverter}}"/>
        <!--<Setter Property="Background" Value="OrangeRed"/>-->
    </Style>
</dg:DataGridControl.Resources>

<dg:DataGridControl.Columns>
    <dg:Column Title="{bx:LocalizeBinding Type, Global=True}" FieldName="MovementType" Width="45"/>
    <dg:Column Title="{bx:LocalizeBinding Number, Global=True }" FieldName="DocumentNumber" Width="90"/>
    <dg:Column Title="{bx:LocalizeBinding Date, Global=True }" FieldName="DocumentDate" Width="90"/>
    <dg:Column Title="{bx:LocalizeBinding DueDate, Global=True }" FieldName="DocumentDueDate" Width="90"/>
    <dg:Column Title="{bx:LocalizeBinding Debit, Global=True }" FieldName="Debit" CellHorizontalContentAlignment="Right" Width="80">
        <dg:Column.CellContentTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding StringFormat=N2}" HorizontalAlignment="Right"/>
            </DataTemplate>
        </dg:Column.CellContentTemplate>
    </dg:Column>
    <dg:Column Title="{bx:LocalizeBinding Credit, Global=True }" FieldName="Credit" CellHorizontalContentAlignment="Right" Width="80">
        <dg:Column.CellContentTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding StringFormat=N2}" HorizontalAlignment="Right"/>
            </DataTemplate>
        </dg:Column.CellContentTemplate>
    </dg:Column>
    <dg:Column Title="{bx:LocalizeBinding Balance, Global=True }" FieldName="Balance" CellHorizontalContentAlignment="Right">
        <dg:Column.CellContentTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding StringFormat=N2}" HorizontalAlignment="Right"/>
            </DataTemplate>
        </dg:Column.CellContentTemplate>
    </dg:Column>
    <dg:Column Title="{bx:LocalizeBinding Difference, Global=True }" FieldName="Difference" CellHorizontalContentAlignment="Right">
        <dg:Column.CellContentTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding StringFormat=N2}" HorizontalAlignment="Right"/>
            </DataTemplate>
        </dg:Column.CellContentTemplate>
    </dg:Column>
    <dg:Column Title="{bx:LocalizeBinding Currency, Global=True }" FieldName="Currency" Width="70"/>
    <dg:Column Title="{bx:LocalizeBinding Description, Global=True }" FieldName="Description" Width="250" TextWrapping="Wrap">
    </dg:Column>
</dg:DataGridControl.Columns>

<dg:DataGridControl.View>
    <dg:TableView/>
</dg:DataGridControl.View>

And here's my converter class:

public class DueDateToBrushConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        DateTime? input = value as DateTime?;
        if (input.HasValue && input < DateTime.Now)
            return Brushes.OrangeRed;
        else
            return Brushes.Transparent;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

I put a breakpoint in the converter and the code is executed, but the row colour is not changing. Setting the colour to a fixed colour (as in the commented line) works just fine. I have no idea what I'm doing wrong. Any help would be much appreciated.


Solution

  • Try with a DataTrigger instead:

    <dg:DataGridControl.Resources>
        <Style TargetType="{x:Type dg:DataRow}" BasedOn="{StaticResource DefaultDataRowStyle}">
            <Style.Triggers>
               <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=DataContext.DocumentDueDate, Converter={StaticResource ResourceKey=DueDateToBrushConverter}}" Value="true">
                    <Setter Property="Background" Value="OrangeRed" />
               </DataTrigger>
            </Style.Triggers>
        </Style>
    </dg:DataGridControl.Resources>
    

    And the converter:

    public class DueDateToBrushConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            DateTime? input = value as DateTime?;
            if (input.HasValue && input < DateTime.Now)
                return true;
            else
                return false;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }