我正在尝试在正确的位置显示自定义日历上的月份日期。使用绑定和使用可观察集合我有一个包含7列和6行的网格,我需要在每个textBlock上显示日期(数字)。我已经让自己完全迷失了,我不知道还有什么可以尝试的。不知怎的,我需要得到星期几和它的日子。我甚至不确定我是否正确地走这条路。我知道XAML很好(至少应该是)我对使用datetime和observable集合感到困惑。非常感谢任何帮助。
这里我使用Observable集合:
public SchedulePage(MainWindow parentForm)
{
InitializeComponent();
_parentForm = parentForm;
// DateTime date = new DateTime(year, month, day);
_parentForm.bindings = new BindingCamper();
int day = DateTime.DaysInMonth(2011, 10);
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 7; j++)
{
_parentForm.bindings.schedule.Add(new Schedule { WeekNo = i, WeekDay = j });
DataContext = _parentForm.bindings;
}
}
}
这是我的附表类:
public class Schedule //: INotifyPropertyChanged
{
public int WeekNo { get; set; }
public int WeekDay { get; set; }
public DateTime Date { get; set; }
public int datenum
{
get { return (int)Date.DayOfWeek; }
}
}
和XAML:
<ItemsControl ItemsSource="{Binding schedule}" Name="Calender" Margin="0,34,0,0" VerticalAlignment="Stretch">
<ItemsControl.Template>
<ControlTemplate TargetType="ItemsControl" >
<Border BorderBrush="Aqua" BorderThickness="1" CornerRadius="15">
<ItemsPresenter/>
</Border>
</ControlTemplate>
</ItemsControl.Template>
<!-- ItemsPanelTemplate -->
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid ShowGridLines="True" Name="gridCalender">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text ="{Binding datenum}" Background="#FFBCF4E0" OpacityMask="Black" Grid.Column="{Binding datenum}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
<!-- ItemContainerStyle -->
<ItemsControl.ItemContainerStyle>
<Style >
<Setter Property="Grid.Column" Value="{Binding WeekDay}" />
<Setter Property="Grid.Row" Value="{Binding WeekNo}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
我还有一个类创建一个新的ObservableCollection();
答案 0 :(得分:0)
创建日期的循环可能应该是日期循环,WeekNo和WeekDay是根据日期计算的值
例如,
DateTime today = DateTime.Now;
DateTime startDate = new DateTime(today.Year, today.Month, 1);
DateTime endDate = startDate.AddMonths(1).AddDays(-1);
while (startDate <= endDate)
{
DayCollection.Add(new Day
{
Date = startDate,
WeekDay = (int)startDate.DayOfWeek,
WeekNo = startDate.GetWeekOfMonth()
});
startDate = startDate.AddDays(1);
}
它使用扩展方法查找月内的周数,可以找到on another SO question
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);
}
}