Win8 自定义 日历 控件 --- 只是简单说明思路而已

本文介绍了一个使用WPF实现的月历视图应用程序,包括如何构建数据模型、更新视图以及响应用户操作等内容。该应用通过ObservableCollection管理项,并利用CollectionViewSource进行分组显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>



//    效果图


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值