wpf使用网格制作日历

发布于 2024-12-12 11:30:05 字数 2940 浏览 3 评论 0原文

我正在制作的日历有一个具体问题。这很难解释,但通过确定周#(每月)和周中的某一天,我制作了一个网格。日期排列不正确。示例: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()
        });
    }
}

I have a specific problem with my calendar I'm making. Its difficult to explain but By determining the week#(of the month) and the dayofweek i make a grid. The dates are not lining up the right way. Example: October 1, 2011 should begin on a saturday, since the 1st is in week 1 but starts on a saturday, the calendar should look something like this: (assuming there's grid squares for each day) Iknow why this problem is occuring I just don't know how to fix it. The dates are not lining up where they should on the grid.

Calendar should look like this:

   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

But mine looks like this: (column 0-4 should be shifted down one row)

    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    -------------

Code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凶凌 2024-12-19 11:30:05

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

使用这个SO答案中找到的代码来获得正确的答案月份中的周数

它为 DateTime 类创建一个 ExtensionMethod ,然后您可以通过调用来使用它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);
    }
}

You are using (day / 7) + 1 to get the WeekNo in that month (Row No). This will be incorrect unless the month begins on a Sunday.

Use the code found in this SO answer to get the correct week number in the month

It creates an ExtensionMethod for the DateTime class, which you can then use by calling 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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文