Win8 自定义DatePicker --- 用户控件

38 篇文章 1 订阅

MyDatePicker.xaml

<UserControl
    x:Class="BPCompanion.MyDatePicker"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:BPCompanion"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="40"
    d:DesignWidth="260">
    
    <Grid >
        <StackPanel Orientation="Horizontal" Height="40">
            <ComboBox x:Name="monthlist" HorizontalAlignment="Left" VerticalAlignment="Top" Width="80" Height="40" >
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=MONTH}" VerticalAlignment="Center" FontSize="16" Width="60"/>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
            <ComboBox x:Name="daylist"  HorizontalAlignment="Left"  VerticalAlignment="Top" Width="80" Height="40" Margin="10,0,0,0">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=DAY}" VerticalAlignment="Center" FontSize="16" />
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
            <ComboBox x:Name="yearlist" HorizontalAlignment="Left" VerticalAlignment="Top" 
                      Width="80" Height="40" Margin="10,0,0,0">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=YEAR}" VerticalAlignment="Center" FontSize="15" />
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
        </StackPanel>
    </Grid>
</UserControl>

MyDatePicker.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// 

namespace BPCompanion
{
    public partial class MyDatePicker : UserControl
    {
        public delegate void Date_ChangedHandler(DateTime date);     // 声明一个 委托,在日期选择发生变化时调用
        public event Date_ChangedHandler Date_ChangedHandlerEvent;                  // 声明一个这种委托的事件  

        public int select_year { get; set; }
        public int select_month { get; set; }
        public int select_day { get; set; }
        public DateTime currentDate { get; set; }


        public void setDate(DateTime datetime)
        {
            this.yearlist.SelectedIndex = datetime.Year - 1901;
            this.monthlist.SelectedIndex = datetime.Month - 1;
            this.daylist.SelectedIndex = datetime.Day - 1;

            select_year = datetime.Year;
            select_month = datetime.Month;
            select_day = datetime.Day;

            currentDate = new DateTime(select_year, select_month, select_day);
        }

        public MyDatePicker()
        {
            this.InitializeComponent();

            Date date = new Date();
            DateTime now = DateTime.Now;


            DateModel datemodel = new DateModel(date, now.Year, now.Month);

            this.yearlist.ItemsSource = datemodel.Years;
            this.monthlist.ItemsSource = datemodel.Months;
            this.daylist.ItemsSource = datemodel.Days;

            this.yearlist.SelectedIndex = now.Year - 1901;
            this.monthlist.SelectedIndex = now.Month - 1;
            this.daylist.SelectedIndex = now.Day - 1;

            select_year = now.Year;
            select_month = now.Month;
            select_day = now.Day;

            currentDate = new DateTime(select_year, select_month, select_day);

            this.yearlist.SelectionChanged += (a, args) =>
                {
                    if (args.AddedItems.Count <= 0)
                    {
                        return;
                    }
                    Year item = (dynamic)args.AddedItems[0];
                    if (item != null)
                    {
                        select_year = item.YEAR;

                        if (datemodel.Days.Count != (new DateModel(date, select_year, select_month)).Days.Count)
                        {
                            datemodel = new DateModel(date, select_year, select_month);
                            this.daylist.ItemsSource = datemodel.Days;
                            if (select_day > datemodel.Days.Count)
                            {
                                this.daylist.SelectedIndex = 0;
                                select_day = 1;
                            }
                            else
                            {
                                this.daylist.SelectedIndex = select_day - 1;
                            }
                        }
                        
                        DateTime dt = new DateTime(select_year, select_month, select_day);
                        currentDate = dt;

                        if (Date_ChangedHandlerEvent != null)           // 判断是否有注册事件
                        {
                            Date_ChangedHandlerEvent(dt);      //调用所有注册对象的方法
                        }
                    }
                };
            this.monthlist.SelectionChanged += (a, args) =>
            {
                if (args.AddedItems.Count <= 0)
                {
                    return;
                }
                Month item = (dynamic)args.AddedItems[0];
                if (item != null)
                {
                    select_month = this.monthlist.SelectedIndex + 1;

                    if (datemodel.Days.Count != (new DateModel(date, select_year, select_month)).Days.Count)
                    {
                        datemodel = new DateModel(date, select_year, select_month);
                        this.daylist.ItemsSource = datemodel.Days;
                        if (select_day > datemodel.Days.Count)
                        {
                            this.daylist.SelectedIndex = 0;
                            select_day = 1;
                        }
                        else
                        {
                            this.daylist.SelectedIndex = select_day - 1;
                        }
                    }

                    DateTime dt = new DateTime(select_year, select_month, select_day);
                    currentDate = dt;
                    if (Date_ChangedHandlerEvent != null)
                    {
                        Date_ChangedHandlerEvent(dt);      //调用所有注册对象的方法
                    }
                }
            };
            this.daylist.SelectionChanged += (a, args) =>
            {
                if (args.AddedItems.Count <= 0)
                {
                    return;
                }
                Day item = (dynamic)args.AddedItems[0];
                if (item != null)
                {
                    select_day = item.DAY;

                    if (datemodel.Days.Count != (new DateModel(date, select_year, select_month)).Days.Count)
                    {
                        datemodel = new DateModel(date, select_year, select_month);
                        this.daylist.ItemsSource = datemodel.Days;
                        this.daylist.SelectedIndex = 0;
                    }

                    DateTime dt = new DateTime(select_year, select_month, select_day);
                    currentDate = dt;
                    if (Date_ChangedHandlerEvent != null)
                    {
                        Date_ChangedHandlerEvent(dt);      //调用所有注册对象的方法
                    }
                }
            };
        }
    }

    public class DateModel
    {
        public List<Year> years;
        public List<Month> months;
        public List<Day> days;
        bool isLeapYear = false;        // 标识 是否为闰年,默认为false

        public DateModel(Date date, int year_p, int month_p)
        {
            years = date.years;
            months = date.months;

            //if (((0 == year_p % 100) && (0 == year_p % 4)) || (0 == year_p % 400))
            if (DateTime.IsLeapYear(year_p))
            {
                isLeapYear = true;
            }

            if (2 == month_p)
            {
                if (isLeapYear)
                {
                    days = date.day_29;
                }
                else
                {
                    days = date.day_28;
                }
            }
            else
            {
                if (1 == month_p || 3 == month_p || 5 == month_p || 7 == month_p || 8 == month_p || 10 == month_p || 12 == month_p)
                {
                    days = date.day_31;
                }
                else
                {
                    days = date.day_30;
                }
            }
        }

        public List<Year> Years
        {
            get { return years; }
            set { years = value; }
        }
        public List<Month> Months
        {
            get { return months; }
            set { months = value; }
        }
        public List<Day> Days
        {
            get { return days; }
            set { days = value; }
        }
    }

    public class Date
    {
        public List<Year> years = new List<Year>(200);       // 1901 ~ 2100
        public List<Month> months = new List<Month>(12);       // 
        public List<Day> day_28 = new List<Day>(28);
        public List<Day> day_29 = new List<Day>(29);
        public List<Day> day_30 = new List<Day>(30);
        public List<Day> day_31 = new List<Day>(31);

        public Date()
        {
            for (int i = 0; i < 200; i++)
            {
                years.Add(new Year(1901 + i));
            }
            //for (int i = 0; i < 12; i++)
            //{
            //    months.Add(new Month(i + 1));
            //}
            months.Add(new Month("Jan"));
            months.Add(new Month("Feb"));
            months.Add(new Month("Mar"));
            months.Add(new Month("Apr"));
            months.Add(new Month("May"));
            months.Add(new Month("Jun"));
            months.Add(new Month("Jul"));
            months.Add(new Month("Aug"));
            months.Add(new Month("Sep"));
            months.Add(new Month("Oct"));
            months.Add(new Month("Nov"));
            months.Add(new Month("Dec"));

            for (int i = 0; i < 31; i++)
            {
                if (i < 28)
                {
                    day_28.Add(new Day(i + 1));
                }
                if (i < 29)
                {
                    day_29.Add(new Day(i + 1));
                }
                if (i < 30)
                {
                    day_30.Add(new Day(i + 1));
                }
                if (i < 31)
                {
                    day_31.Add(new Day(i + 1));
                }
            }
        }
    }

    public class Year
    {
        public int _year;

        public Year(int year_p)
        {
            YEAR = year_p;
        }

        public int YEAR
        {
            get { return _year; }
            set { _year = value; }
        }
    }

    public class Month
    {
        public string _month;

        public Month(string month_p)
        {
            MONTH = month_p;
        }

        public string MONTH
        {
            get { return _month; }
            set { _month = value; }
        }
    }

    public class Day
    {
        public int _day;

        public Day(int day_p)
        {
            DAY = day_p;
        }

        public int DAY
        {
            get { return _day; }
            set { _day = value; }
        }
    }

    // // 数字月份
    //public class Month
    //{
    //    public int _month;

    //    public Month(int month_p)
    //    {
    //        MONTH = month_p;
    //    }

    //    public int MONTH
    //    {
    //        get { return _month; }
    //        set { _month = value; }
    //    }
    //}

   

}



// 使用方法:

1:在XAML中引入-----------xmlns:picker="using:BPCompanion"

2:在使用的位置----------
                    <picker:MyDatePicker x:Name="datepicker" Grid.Row="3" />

3:设置数据、获得数据

this.datepicker.setDate(DateTime.Now.AddHours(-8));

            this.datepicker.Date_ChangedHandlerEvent += (date)=>{           // 向委托注册事件
                this.profilename.Text = date.ToString("MMM,dd yyyy");
            };
this.datepicker.currentDate;

效果:



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值