1

我在这里使用了示例How to create a DateEdit descendant that will allow date unit selection, and multiple dates and period selection。我想再添加一个功能,我将提供一组日期,只有那些日期才可见。我已经修改了代码并包含了一个属性来接受日期范围,如果提供它只会允许查看和选择这些日期,但我无法理解我应该重写哪个函数来完成任务。行为应该类似于提供编辑器时的行为,MaxValue并且MinValue

这是我的代码

4

1 回答 1

1

查看 VistaDateEditInfoArgs 类的(标准)实现。您可以使用 .NET 程序集反编译器,例如 .NET Reflector 或 ILSpy。如果相关的日期时间不可见/不允许,则可以覆盖几个虚拟方法并返回 null。以下是这些方法的源代码(请注意“标准”基于 MinValue / MaxValue 的检查):

[DevExpress.XtraEditors.ViewInfo.VistaDateEditInfoArgs]

protected virtual DayNumberCellInfo CreateMonthCellInfo(int row, int col)
{
    DayNumberCellInfo info;
    DateTime date = new DateTime(this.DateTime.Year, (1 + (row * 4)) + col, 1);
    if (date > this.Calendar.MaxValue)
    {
        return null;
    }
    if ((date < this.Calendar.MinValue) && (date.Month < this.Calendar.MinValue.Month))
    {
        return null;
    }

    return new DayNumberCellInfo(date) { Text = this.Calendar.DateFormat.GetAbbreviatedMonthName    (info.Date.Month) };
}

protected virtual DayNumberCellInfo CreateYearCellInfo(int row, int col)
{
    int num = ((this.DateTime.Year / 10) * 10) - 1;
    int year = (num + (row * 4)) + col;
    if ((year <= 0) || (year >= 0x2710))
    {
        return null;
    }
    DateTime date = new DateTime(year, 1, 1);
    if (date > this.Calendar.MaxValue)
    {
        return null;
    }
    if ((date < this.Calendar.MinValue) && (date.Year < this.Calendar.MinValue.Year))
    {
        return null;
    }
    DayNumberCellInfo info = new DayNumberCellInfo(date) {
        Text = year.ToString()
    };
    if ((year < ((this.DateTime.Year / 10) * 10)) || (year > (((this.DateTime.Year / 10) * 10) + 1)))
    {
        info.State = ObjectState.Disabled;
    }
    return info;
}

protected virtual DayNumberCellInfo CreateYearsGroupCellInfo(int row, int col)
{
    int num = ((this.DateTime.Year / 100) * 100) - 10;
    int year = num + (((row * 4) + col) * 10);
    if ((year < 0) || (year >= 0x2710))
    {
        return null;
    }
    int num3 = year + 9;
    if (year == 0)
    {
        year = 1;
    }
    DateTime date = new DateTime(year, 1, 1);
    if (date > this.Calendar.MaxValue)
    {
        return null;
    }
    if ((date < this.Calendar.MinValue) && (num3 < this.Calendar.MinValue.Year))
    {
        return null;
    }
    return new DayNumberCellInfo(date) { Text = year.ToString() + "-\n" + num3.ToString() };
}

我建议您在后代类中覆盖这些方法并添加您的自定义检查。例如,您可以像这样重写 CreateMonthCellInfo 方法:

protected override DayNumberCellInfo CreateMonthCellInfo(int row, int col)
{
    DateTime date = new DateTime(this.DateTime.Year, (1 + (row * 4)) + col, 1);

    if (!IsDateAvailable(date))
    {
        return null;
    }

    return base.CreateMonthCellInfo(row, col);
}

// Your date availibility check implementation here
private bool IsDateAvailable(DateTime date)
{
    // TODO provide implementation
    throw new NotImplementedException();
} 
于 2013-03-11T13:17:49.070 回答