DevExpress dateedit customization to allow only specific dates

自古美人都是妖i 提交于 2019-12-08 05:22:51

问题


I have used the example here How to create a DateEdit descendant that will allow date unit selection, and multiple dates and periods selection. I wanted to add one more functionality where I would provide an array of dates only those will be visible. I have modified the code and included a property to accept the date ranges, which if provided would only allow seeing and selecting those dates, but I'm unable to understand which function I should override to accomplish the task. The behavior should be like when the editor is supplied MaxValue and MinValue

Here is my code


回答1:


Take a look at the (standard) implementation of the VistaDateEditInfoArgs class. You may use a .NET assembly decompiler like .NET Reflector or ILSpy. There are a couple of virtual methods you may override and return null if the related datetime should not be visible/allowed. Here is the source code of these methods (please note the "standard" MinValue / MaxValue based checks) :

[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() };
}

I suggest you to override these methods in your descendant class and add your custom checks. For example, you may override the CreateMonthCellInfo method somehow like this:

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();
} 


来源:https://stackoverflow.com/questions/15172413/devexpress-dateedit-customization-to-allow-only-specific-dates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!