【问题标题】:wpf making calendar using gridwpf使用网格制作日历
【发布时间】:2011-12-16 23:10:42
【问题描述】:

我正在制作的日历存在特定问题。它很难解释,但通过确定星期#(一个月的)和星期几,我制作了一个网格。日期排列不正确。示例:2011 年 10 月 1 日应该从星期六开始,因为第 1 天是在第 1 周,但从星期六开始,日历应该如下所示:(假设每天都有方格)我知道为什么会出现这个问题我只是不知道如何解决。日期没有排列在网格上应有的位置。

日历应如下所示:

列 2011 年 10 月 grid.column = 7 (0-6) grid.row = 6 (0-5)(周日至周六) 周日 周一 周二 周三 周四 周五 周六 行 0 1 2 3 4 5 6 0 -------------------------------------------------第一 1 2 3 4 5 6 -----------等..--- 2 --------------------------------------------等...- ----- 3 ------等..---------------------------------------- ----- 4 ---------------- 25 日 26 日 27 日 28 日 29 日 5 月 30 日 31 日

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

周日 周一 周二 周三 周四 周五 周六 行 0 1 2 3 4 5 6 0 第二 第三 第三 第四 第五 第六---------- 第一 1 9日 10日 11日 12日 13日-----------等..--- 2 --------------------------------------------等...- ----- 3 ------等等..-----25th 26th 27th----------------- 4 30 日 31 日--- --------- 28 日 29 日 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()
        });
    }
}

【问题讨论】:

  • 对不起,我的图纸没有按照我想要的方式出现,但是如果您查看 2011 年 10 月的日历,您会看到总共有 6 行,第一行 (grid.row(0) ) 仅包含星期六(oct.1st),但我的包含 2nd-6th 和星期六 1st。
  • 您可以使用 ImageShack 上传正在发生的事情和您的意图的快照,并通过使用图像小屋图片链接在堆栈溢出问题编辑器上使用“插入图像”选项。

标签: c# wpf visual-studio observablecollection


【解决方案1】:

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

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

它为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);
    }
}

【讨论】:

    猜你喜欢
    • 2019-07-31
    • 2013-03-19
    • 2023-03-20
    • 1970-01-01
    • 2011-08-05
    • 2014-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多