我有一个包含一般发票信息的数据网格(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>
这是我的转换器类:
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();
}
}
我在转换器中放置了断点并执行了代码,但行颜色没有改变。将颜色设置为固定颜色(如在注释行中)可以正常工作。我不知道我做错了什么。任何帮助将不胜感激。
答案 0 :(得分:0)
尝试使用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();
}
}