如何在WPF DataGrid中添加年、月、两周边界分隔符(需要将当前日期与以前的日期进行比较)

如何在WPF DataGrid中添加年、月、两周边界分隔符(需要将当前日期与以前的日期进行比较),wpf,xaml,datagrid,border,Wpf,Xaml,Datagrid,Border,我想在数据网格中分别添加年、月和两周的行。当然,viewmodel的FieldModel字段属性包含一个Date属性,该属性按升序排序。我认为我不能在数据网格上画线,所以我更改了水平上边框的厚度 老实说,我不知道WPF在幕后是如何管理这些值转换器的。使用\u previousDate私有字段并将其与当前字段(由绑定提供)进行比较效果很好,但并不准确。。同样,我不知道它是否对EnableRowVirtualization=“True”有影响(我有几千行要显示) XAML 公共类DateToOrde

我想在
数据网格
中分别添加年、月和两周的行。当然,viewmodel的
FieldModel字段
属性包含一个
Date
属性,该属性按升序排序。我认为我不能在
数据网格上画线,所以我更改了水平上边框的厚度

老实说,我不知道WPF在幕后是如何管理这些值转换器的。使用
\u previousDate
私有字段并将其与当前字段(由绑定提供)进行比较效果很好,但并不准确。。同样,我不知道它是否对
EnableRowVirtualization=“True”
有影响(我有几千行要显示)

XAML

公共类DateToOrderThicknessConverter:IValueConverter
{
私有日期时间?\u previousDate=null;
公共重写对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{
DateTime currentDate=((日期时间)值);
//反转当前和以前的日期(没有效果,因为即使我从上到下滚动,仍然会得到一些_previousDate>currentDate)
字符串BorderThichKness=(\u previousDateBinding.DoNothing;
私有字符串GetBorderThichkness(DateTime?currentDate,DateTime?previousDate)
{
字符串边框厚度=“0”;
//两周

_previousDate?.Day<15&&15我找到了一个部分解决方案。我将
FieldModel
扩展到
ExtendedFieldModel
,在那里我添加了
FormatDate
属性(仍然需要考虑正确的名称)使用
两周
等进行
枚举
。在初始化视图模型时,我使用与上述相同的原则计算值。似乎工作正常,但仍对更好的想法感兴趣:-)
public class DateToBorderThicknessConverter : IValueConverter
{
    private DateTime? _previousDate = null;

    public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        DateTime currentDate = ((DateTime)value);
        
        // Inverts the current and previous dates (no effect, because even if I scroll from top to bottom, I still get some _previousDate > currentDate)
        string borderThickness = (_previousDate < currentDate) ? GetBorderThichkness(currentDate, _previousDate) : GetBorderThichkness(_previousDate, currentDate);

        _previousDate = currentDate;

        return borderThickness;
    }

    public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => Binding.DoNothing;

    private string GetBorderThichkness(DateTime? currentDate, DateTime? previousDate)
    {
        string borderThickness = "0";

        // Fortnights
        _previousDate?.Day < 15 && 15 <= currentDate?.Day : borderThickness; // Ideally with discontiguous line

        // Months
        //borderThickness = _previousDate?.Month < currentDate?.Month ? "0 1 0 0" : borderThickness;

        // Years
        borderThickness = _previousDate?.Year < currentDate?.Year ? "0 2.5 0 0" : borderThickness;

        return borderThickness;
    }
}