ObservableCollection<Item> list;
ObservableCollection<Items> array;
DateTime currenttime;
string timeformat = "MMM,dd yyyy";
DateTime now = DateTime.Now;
public Test()
{
this.InitializeComponent();
list = new ObservableCollection<Item>();
array = new ObservableCollection<Items>();
Currenttime = new DateTime(DateTime.Now.Year, DateTime.Now.Month,1);
init_Data();
this.itemViewSource.Source = array;
}
private void init_Data()
{
this.currentdate.Text = Currenttime.ToString(timeformat);
int begin = 0;
switch (Currenttime.DayOfWeek)
{
case DayOfWeek.Sunday:
begin = 0;
break;
case DayOfWeek.Monday:
begin = -1;
break;
case DayOfWeek.Tuesday:
begin = -2;
break;
case DayOfWeek.Wednesday:
begin = -3;
break;
case DayOfWeek.Thursday:
begin = -4;
break;
case DayOfWeek.Friday:
begin = -5;
break;
case DayOfWeek.Saturday:
begin = -6;
break;
default:
break;
}
// 找到这6行7列 42个数据
for (int i = 0; i < 42; i++)
{
Item temp = new Item();
temp.day = Currenttime.AddDays(begin + i).Day;
list.Add(temp);
}
// 初始化为 7组,每组6个数
for (int i = 0; i < 7; i++)
{
Items temp = new Items();
temp.TopItems = new List<Item>();
for (int j = 0; j < 6; j++)
{
Item tmp;
tmp = list[i + 7*j];
temp.TopItems.Add(tmp);
}
array.Add(temp);
}
//array[0].TopItems[1].day = array[0].TopItems[2].day - 7;
}
public DateTime Currenttime
{
set { this.currenttime = value; }
get { return this.currenttime; }
}
// 上个月
private void premonth_Click(object sender, RoutedEventArgs e)
{
now = now.AddMonths(-1);
Currenttime = new DateTime(now.Year, now.Month, 1);
list.Clear();
array.Clear();
init_Data();
this.itemViewSource.Source = array;
foreach (var item in array)
{
for (int i = 0; i < 6; i++)
{
System.Diagnostics.Debug.WriteLine(item.TopItems[i].day);
}
}
}
// 下个月
private void nextyear_Click(object sender, RoutedEventArgs e)
{
now = now.AddMonths(1);
Currenttime = new DateTime(now.Year, now.Month, 1);
list.Clear();
array.Clear();
init_Data();
this.itemViewSource.Source = array;
}
public class Item
{
public int day { get; set; }
}
public class Items
{
public int week { get; set; }
public List<Item> TopItems{ get; set; }
}
// XAML 关键代码
<Grid.Resources>
<CollectionViewSource x:Name="itemViewSource" x:Key="itemViewSource" IsSourceGrouped="true" ItemsPath="TopItems"/>
</Grid.Resources>
<Grid Background="#E1C78C" Width="350" Height="420">
<Grid.Resources>
<DataTemplate x:Key="CalendarTemplate" >
<StackPanel Orientation="Vertical" Width="40" Height="40">
<TextBlock Text="{Binding day}" TextAlignment="Center" FontSize="20" />
</StackPanel>
</DataTemplate>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="10"/>
<RowDefinition Height="40"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<Button x:Name="preyear" Content="<<" Background="#F7D46D" Margin="0,0,0,0"/>
<Button x:Name="premonth" Content="<" Background="#F7D46D" Click="premonth_Click"/>
<TextBlock x:Name="currentdate" Text="05,Seb 2013" Width="120" TextAlignment="Center" FontSize="20" Margin="0,20,0,10" Foreground="Green"/>
<Button x:Name="nextyear" Content=">" Background="#F7D46D" Click="nextyear_Click"/>
<Button x:Name="nextmonth" Content=">>" Background="#F7D46D"/>
</StackPanel>
<Rectangle Fill="DarkCyan" Height="10" Grid.Row="1"/>
<TextBlock Grid.Row="2" Text="Sun Mon Tue Wed Thu Fri Sat" FontSize="20" Foreground="Gray" Margin="10,10,0,5" />
<GridView x:Name="listview"
SelectionMode="None"
IsItemClickEnabled="True"
Grid.Row="3"
ItemsSource="{Binding Source={StaticResource itemViewSource}}"
ItemTemplate="{StaticResource CalendarTemplate}">
<!--<ListViewItem>ItemClick="item_OnClick"
<StackPanel Orientation="Vertical" Width="40" Height="40">
<TextBlock Text="{Binding day}" TextAlignment="Center" FontSize="20" />
</StackPanel>
</ListViewItem>-->
</GridView>
</Grid>
// 效果图