在设定的时间段内向列表添加空格

时间:2014-03-31 19:01:22

标签: c# wpf list mvvm listbox

我正在使用MVVM创建一个WPF应用程序。我想创建一个基本的预订列表,用于从数据库中撤回的特定日期,但也包括列表中的空白区域(显示在列表框中),以显示预订之间的空闲时间,工作时间。

到目前为止,我可以添加空格,但仅限于2个预订中的空白,而我希望从9:00到21:00每隔一小时显示一次空格。

private void CheckForGaps(List<DisplayBookingDetails> list)
{
    DateTime endIndex;
    DateTime startIndex;
    int minBookingLength = 60;

    DisplayBookingDetails[] test;
    DisplayBookingDetails nullMarker = null;

    DateTime dayStart = new DateTime(0001, 01, 01).AddHours(09).AddMinutes(00).AddSeconds(00);
    DateTime dayEnd = new DateTime(0001, 01, 01).AddHours(21).AddMinutes(00).AddSeconds(00);


    test = list.ToArray();
    List<DisplayBookingDetails> testList = new List<DisplayBookingDetails>();

    for (int i = 0; i < test.Length; i++)
    {
        if (i > 0)
        {
            endIndex = list[i - 1].EndTime;
            startIndex = list[i].StartTime;
            int diff = ((int)(startIndex - endIndex).TotalMinutes);

            testList.Add(test[i - 1]);

            while (diff >= minBookingLength)
            {
                nullMarker = new DisplayBookingDetails(0) { Start = endIndex.ToShortTimeString(), End = endIndex.AddMinutes(60).ToShortTimeString() };
                testList.Add(nullMarker);
                diff -= minBookingLength;
            }
        }
    }
    if (test.Length > 0)
        testList.Add(test[test.Length - 1]);
        DisplayBookingDetails = testList;
    }

上面的代码遍历数组,如果它不是空的,它会检查预订之间的差异并在列表中添加空标记行,但我希望这显示空行,即使日期没有预订一点都不。

有谁知道如何添加此功能,谢谢?This shows what the functionality already displays

2nd screenshot provided of latest display

1 个答案:

答案 0 :(得分:2)

我不确定为什么要创建测试而不是使用列表,但我已完全修改了您的代码以完全按照您的意愿执行。

private void CheckForGaps(List<DisplayBookingDetails> list)
{
    DateTime endPrevious;
    DateTime startCurrent;
    int minBookingLength = 60;

    DisplayBookingDetails nullMarker = null;

    DateTime selectedDate = [CalendarObject].Value.Date;

    DateTime dayStart = selectedDate.AddHours(09)
    DateTime dayEnd = selectedDate.AddHours(21)

    List<DisplayBookingDetails> testList = new List<DisplayBookingDetails>();

    if (list.Length > 0) // First we check if the list has any items
    {
        DateTime startTime = dayStart;
        DateTime endTime = list[0].StartTime;
        // Fill the gap before the first appointment with blank appointments
        while ((endTime - startTime).TotalMinutes >= minBookingLength)
        {
            nullMarker = new DisplayBookingDetails(0) { Start = startTime.ToShortTimeString(), End = startTime.AddMinutes(minBookingLength).ToShortTimeString() };
            testList.Add(nullMarker);
            startTime = startTime.AddMinutes(minBookingLength);
        }
        // Go through the appointments adding them
        for (int i = 1; i < list.Length; i++)
        {
            testList.Add(list[i - 1]);

            endPrevious = list[i - 1].EndTime;
            startCurrent = list[i].StartTime;

            startTime = endPrevious;
            // Fill gaps between appointments
            while ((startCurrent - startTime).TotalMinutes >= minBookingLength)
            {
                nullMarker = new DisplayBookingDetails(0) { Start = startTime.ToShortTimeString(), End = startTime.AddMinutes(minBookingLength).ToShortTimeString() };
                testList.Add(nullMarker);
                startTime = startTime.AddMinutes(minBookingLength);
            }
        }
        // Add the last appointment
        testList.Add(list[list.Length - 1]);

        // Add blank appointments after the last appointment until End of Day
        startTime = list[list.Length - 1].EndTime;
        while ((dayEnd - startTime).TotalMinutes >= minBookingLength)
        {
            nullMarker = new DisplayBookingDetails(0) { Start = startTime.ToShortTimeString(), End = startTime.AddMinutes(minBookingLength).ToShortTimeString() };
            testList.Add(nullMarker);
            startTime = startTime.AddMinutes(minBookingLength);
        }
    }
    else // No items in list, add all blank appointments
    {
        DateTime startTime = dayStart;
        while((dayEnd - startTime).TotalMinutes >= minBookingLength)
        {
            nullMarker = new DisplayBookingDetails(0) { Start = startTime.ToShortTimeString(), End = startTime.AddMinutes(minBookingLength).ToShortTimeString() };
            testList.Add(nullMarker);
            startTime = startTime.AddMinutes(minBookingLength);
        }
    }
    // Display the final list
    DisplayBookingDetails.ItemsSource = testList;
}