wpf使用网格制作日历
我正在制作的日历有一个具体问题。这很难解释,但通过确定周#(每月)和周中的某一天,我制作了一个网格。日期排列不正确。示例: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用
(day / 7) + 1
来获取该月的 WeekNo(行号)。除非该月从星期日开始,否则这是不正确的。使用这个SO答案中找到的代码来获得正确的答案月份中的周数
它为
DateTime
类创建一个ExtensionMethod
,然后您可以通过调用来使用它newcurr.GetWeekOfMonth()
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 theDateTime
class, which you can then use by callingnewcurr.GetWeekOfMonth()