我正在创建一个绑定到数据库的故障日志应用程序
我想要的主要用途是在DateTime时将DateTime单元格格式化为RED 列出的是当前日期之前的3周以上。
MainWindow.xaml
<DataGrid AutoGenerateColumns="False" Height="379" HorizontalAlignment="Left" Margin="0,36,0,0" Name="dataGridLog" VerticalAlignment="Top" Width="432" SelectionChanged="dataGridLog_SelectionChanged" IsReadOnly="True" MouseDoubleClick="dataGridLog_MouseDoubleClick" ItemsSource="{Binding}" RowDetailsVisibilityMode="VisibleWhenSelected">
<DataGrid.Columns>
<DataGridTemplateColumn x:Name="dateColumn" Header="Date" Width="80" CanUserReorder="True" CanUserSort="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock x:Name="dateColumnTextBlock" Text="{Binding Path=DateSent, StringFormat={}{0:dd/MM/yyyy}}" Width="80" />
<DataTemplate.Triggers>
<!--<DataTrigger Binding="{Binding Path=DateSent, StringFormat={}{0:dd/MM/yyyy}, Converter={x:Reference mIsEqualOrGreaterThanConverter.Instance}, ConverterParameter=3}" Value="True">
<Setter TargetName="dateColumnTextBlock" Property="Background" Value="Red" />
</DataTrigger>-->
</DataTemplate.Triggers>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn x:Name="priorityColumn" Header="Priority" Width="80" CanUserReorder="True" CanUserSort="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock x:Name="priorityColumnTextBlock" Text="{Binding Path=Priority}" Width="80" />
<!--<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=Priority}" Value="Urgent">
<Setter TargetName="priorityColumnTextBlock" Property="Background" Value="Red" />
</DataTrigger>
</DataTemplate.Triggers>-->
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn x:Name="titleColumn" Binding="{Binding Path=Fault}" Header="Title" Width="270" CanUserReorder="True" CanUserSort="True" />
</DataGrid.Columns>
</DataGrid>
MainWindow.xaml.cs
[ValueConversion(typeof(DateTime), typeof(String))]
public class DateConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
DateTime date = (DateTime)value;
DateTime curDate = DateTime.Now;
TimeSpan span = curDate.Subtract(date);
return span.Days;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string strValue = value as string;
DateTime resultDateTime;
if (DateTime.TryParse(strValue, out resultDateTime))
{
return resultDateTime;
}
return DependencyProperty.UnsetValue;
}
}
我似乎无法绑定转换器,无法转换日期。
答案 0 :(得分:2)
如果您已在Converter
Resources
<Window.Resources>
<local:IsEqualOrGreaterThanConverter x:Key="IsEqualOrGreaterThanConverter" />
</Window.Resources>
你可以像这样引用它
<DataTrigger Binding="{Binding Path=DateSent, Converter={StaticResource IsEqualOrGreaterThanConverter}, ConverterParameter=3}" Value="True">
<Setter TargetName="dateColumnTextBlock" Property="Background" Value="Red" />
</DataTrigger>
您的DataTrigger
与True比较。因此,Converter
应该返回一个bool。
[ValueConversion(typeof(DateTime), typeof(bool))]
public class IsEqualOrGreaterThanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
DateTime date = (DateTime)value;
DateTime curDate = DateTime.Now;
TimeSpan span = curDate.Subtract(date);
return span.TotalDays > (int)parameter * 7;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}