我正在尝试使用wpf制作日历。通过使用itemsPanel和更多,我有一个网格有7列(星期六 - 星期六)和6行(星期#月)。如果我可以通过获取工作日和周数(月份)找到每月第一个的起始位置,我如何找到周数(每月0-5)?我也不能以某种方式从那里填写日历?我迷路了,我不知道还有什么可以尝试。
public partial class SchedulePage : Page
{
MainWindow _parentForm;
public int dayofweek;
public SchedulePage(MainWindow parentForm)
{
InitializeComponent();
_parentForm = parentForm;
// DateTime date = new DateTime(year, month, day);
_parentForm.bindings = new BindingCamper();
_parentForm.bindings.schedule.Add(new Schedule { WeekNo = (int) getWeekNumber(), WeekDay = dayofweek });
DataContext = _parentForm.bindings;
// lblTest.Content = dates(2011, 10, 27);
}
public double getWeekNumber()
{
dayofweek = getWeekDay(2011, 10, 31);
double h = dayofweek / 7;
double g = Math.Floor(h);
return g;
}
public int getWeekDay(int year, int month, int day)
{
//year = 2011;
//month = 10;
//day = 27;
int[] t = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
// year -= month < 3;
return (year + year / 4 - year / 100 + year / 400 + t[month - 1] + day) % 7;
}
答案 0 :(得分:17)
您必须优先使用Calendar.GetDayOfWeek和Calendar.GetWeekOfYear来自己写作。
您可以保证,如果您自己编写任何日期/时间处理代码,它将包含错误,并且在不同的区域设置中不起作用。
public class Row
{
public string MonthWeek { get; set; }
public string Year { get; set; }
public string Month { get; set; }
public string Day { get; set; }
public string WeekOfYear { get; set; }
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var l = new List<Row>();
DateTime startDate = DateTime.Now;
DateTime d = new DateTime(startDate.Year, startDate.Month, 1);
var cal = System.Globalization.DateTimeFormatInfo.CurrentInfo.Calendar;
var ms = cal.GetWeekOfYear(new DateTime(d.Year, d.Month, 1), System.Globalization.CalendarWeekRule.FirstDay, System.DayOfWeek.Sunday);
for (var i = 1; d.Month == startDate.Month; d = d.AddDays(1))
{
var si = new Row();
var month_week = (d.Day / 7) + 1;
si.MonthWeek = month_week.ToString();
si.Month = d.Year.ToString();
si.Year = d.Month.ToString();
si.Day = d.Day.ToString();
si.WeekOfYear = cal.GetWeekOfYear(d, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString();
l.Add(si);
}
dataGrid1.ItemsSource = l;
}
}
与XAML中的强制性DataGrid一起:
<DataGrid AutoGenerateColumns="true" Name="dataGrid1" />
答案 1 :(得分:11)
您可以使用Globalization中的Calendar.GetWeekOfYear
来执行此操作。
以下是MSDN文档:http://msdn.microsoft.com/en-us/library/system.globalization.calendar.getweekofyear.aspx
您应该将适当的文化属性从CultureInfo.CurrentCulture
传递到GetWeekOfYear
,以便您恰当地匹配当前文化。
示例:
int GetWeekOfYear(DateTime date)
{
return Calendar.GetWeekOfYear(
date,
CultureInfo.CurrentCulture.DateTimeFormat.CalendarWeekRule,
CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek
);
}
您可以轻松地将其修改为DateTime
上的扩展方法:
static int GetWeekOfYear(this DateTime date)
{
return Calendar.GetWeekOfYear(
date,
CultureInfo.CurrentCulture.DateTimeFormat.CalendarWeekRule,
CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek
);
}
答案 2 :(得分:0)
使用@Polynomial答案时,出现此错误:
非静态字段,方法或属性需要对象引用。
如果您之前对GregorianCalendar进行了实例化,则可以调用GetWeekOfYear方法!
private static int GetWeekNumber(DateTime time)
{
GregorianCalendar cal = new GregorianCalendar();
int week = cal.GetWeekOfYear(time, CalendarWeekRule.FirstFullWeek, DayOfWeek.Monday);
return week;
}