wpf使用网格制作日历

时间:2011-10-28 00:46:08

标签: c# wpf visual-studio observablecollection

我的日历中有一个特定的问题。很难解释但是通过确定周(#月)和周日我做一个网格。日期不是正确的排队方式。示例:2011年10月1日应该从星期六开始,因为第一个是在第1周但是从星期六开始,日历看起来应该是这样的:(假设每天都有网格方块)我知道为什么会出现这个问题我只是不知道如何解决它。这些日期并没有排列在他们应该在网格上的位置。

日历应如下所示:

   Columns       October 2011    grid.column = 7 (0-6)  grid.row = 6 (0-5) (Sunday-Saturday)

     Sunday Monday Tuesday Wednesday Thursday Friday Saturday
row    0      1       2        3       4        5       6
0     ------------------------------------------------- 1st
1     2nd     3rd     4th     5th      6th-----------etc..--- 
2    --------------------------------------------etc...------
3    ------etc..---------------------------------------------
4    ---------------- 25th     26th    27th     28th    29th
5    30th     31st

但是我看起来像这样:(第0-4列应该向下移动一行)

    Sunday Monday Tuesday Wednesday Thursday Friday Saturday
row    0      1       2        3       4        5       6
0     2nd     3rd     4th     5th      6th------------- 1st
1     9th     10th     11th   12th     13th-----------etc..--- 
2    --------------------------------------------etc...------
3    ------etc..------25th     26th    27th-----------------
4    30th     31st--- ---------------------     28th    29th
5    -------------

代码:

public SchedulePage(MainWindow parentForm)
{
    InitializeComponent();

    _parentForm = parentForm;

    // DateTime date = new DateTime(year, month, day);
    var t = new List<Schedule>();
    DateTime curr = DateTime.Now;
    DateTime newcurr = new DateTime(curr.Year, curr.Month, 1);
    var cal = System.Globalization.DateTimeFormatInfo.CurrentInfo.Calendar;
    var ms = cal.GetWeekOfYear(new DateTime(newcurr.Year, newcurr.Month, 1), System.Globalization.CalendarWeekRule.FirstDay, System.DayOfWeek.Sunday);

    for (var i = 1; newcurr.Month == curr.Month; newcurr = newcurr.AddDays(1))
    {
        var sched = new Schedule();
        var month_week = (newcurr.Day / 7) + 1;
        sched.MonthWeek = month_week.ToString();
        sched.Month = newcurr.Month.ToString();
        sched.Year = newcurr.Year.ToString();
        sched.day = newcurr.Day.ToString();
        sched.WeekOfYear = cal.GetWeekOfYear(newcurr, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString();
        sched.dayofweek = newcurr.DayOfWeek.ToString();
        t.Add(sched);

     /* if (newcurr.Day == 1 && (int)newcurr.DayOfWeek == 7)
        {
            int findspot = month_week - 1;

            //_parentForm.bindings.schedule.Add(new Schedule {
            //    WeekNo = month_week - findspot,
            //    WeekDay = (int)newcurr.DayOfWeek,
            //    day = newcurr.Day.ToString()
            //});
        } */

        _parentForm.bindings.schedule.Add(new Schedule {
            WeekNo = month_week - 1 ,
            WeekDay = (int)newcurr.DayOfWeek,
            day = newcurr.Day.ToString()
        });
    }
}

1 个答案:

答案 0 :(得分:3)

您正在使用(day / 7) + 1获取该月的WeekNo(行号)。除非月份从星期日开始,否则这将是不正确的。

使用this SO answer中的代码获取当月的正确周数

它会为ExtensionMethod类创建DateTime,然后您可以通过调用newcurr.GetWeekOfMonth()

来使用它
static class DateTimeExtensions {
    static GregorianCalendar _gc = new GregorianCalendar();
    public static int GetWeekOfMonth(this DateTime time) {
        DateTime first = new DateTime(time.Year, time.Month, 1);
        return time.GetWeekOfYear() - first.GetWeekOfYear() + 1;
    }

    static int GetWeekOfYear(this DateTime time) {
        return _gc.GetWeekOfYear(time, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
    }
}