我只是想知道是否可以与WPF进行DATETIME比较,理想情况下我想根据它与当前日期的相关性来为我的数据网格着色.红色为过去的文件,绿色为未来.谢谢你的帮助!
<dg:DataGrid Name="files_datagrid" DataContext="{Binding Source={StaticResource filelist_provider}}"
ItemsSource="{Binding}" CanUserAddRows="False" AutoGenerateColumns="False" Grid.Row="1">
<Style TargetType="{x:Type dg:DataGridRow}"> <Style.Triggers>
<DataTrigger Binding="{Binding Path=[filedate]}" Value=">TODAY">
<Setter Property="Background" Value="Green" />
</DataTrigger>
</Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)
我认为你最好使用价值转换器.
像这样的东西:
[ValueConversion(typeof(DateTime), typeof(Brush))]
public class DateTimeToBrushConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var dateTime = (DateTime)value;
if (dateTime.Date < DateTime.Now)
return Brushes.Red;
if (dateTime.Date > DateTime.Now)
return Brushes.Green;
return Brushes.Black;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
您可以将颜色移动为参数,以使其更具通用性.
然后像这样申请:
<Style TargetType="{x:Type dg:DataGridRow}">
<Setter Property="Background"
Value="{Binding Path=fileDate,
Converter={StaticResource dateTimeToBrushConverter}}" />
</Style>
Run Code Online (Sandbox Code Playgroud)
在您的资源中创建dateTimeToBrushConverter的位置.