Xceed datagrid - 根据 DateTime 值更改数据行颜色

Xceed datagrid - change datarow colour based on DateTime value

我有一个包含一般发票信息的数据网格(Xceed 社区版 3.1.0 版)。其中一个字段包含发票的到期日。我想为到期日期过期的行使用不同的颜色。为此,我使用了值转换器。 这是我的 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>

这是我的转换器 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();
    }
}

我在转换器中放置了一个断点并执行了代码,但行颜色没有改变。将颜色设置为固定颜色(如注释行中所示)效果很好。我不知道我做错了什么。任何帮助将不胜感激。

尝试使用 DataTrigger:

<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>

和转换器:

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();
    }
}