DatePicker的BlackoutDates,日历控件可选范围设定。

简述:

       在已存在的时间列表中,存在以下条件

         1、当日及以上的时间可选。

         2、已存在的时间可选的范围是存在的时间的第二天,到下个月的1日。

画面代码:

<UserControl x:Class="SilverlightApplication2.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" >

    <Grid x:Name="LayoutRoot" Background="White">
        <sdk:DatePicker Height="23" HorizontalAlignment="Left" Margin="61,83,0,0" Name="datePicker1" VerticalAlignment="Top" Width="120" />
    </Grid>
</UserControl>

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightApplication2
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            InitDatePicker();
        }

        private void InitDatePicker()
        {
            List<DateTime> list = this.GetDateTime();

            List<DateTime> hanTime = new List<DateTime>();
            for (int i = 0; i < list.Count - 1; i++)
            {
                DateTime d1 = list[i];
                DateTime d2 = list[i + 1];

                if (i == 0)
                {
                    this.datePicker1.BlackoutDates.Add(new CalendarDateRange(new DateTime(), d1));
                }
                this.datePicker1.BlackoutDates.Add(new CalendarDateRange(d1));

                DateTime dt = new DateTime(d1.Year, d1.AddMonths(1).Month, 1);

                if (d1.Month != d2.Month && dt != d2)
                {
                    List<DateTime> hanTimeList = this.GetTimeList(d1, d2);
                    this.datePicker1.BlackoutDates.Add(new CalendarDateRange(hanTimeList[0], hanTimeList[1]));
                }
            }
            this.datePicker1.BlackoutDates.Add(new CalendarDateRange(list[list.Count - 1]));
        }
        //已存在时间列表
        private List<DateTime> GetDateTime()
        {
            List<DateTime> rtn = new List<DateTime>();

            DateTime time = new DateTime(2014, 8, 12);
            DateTime time3 = new DateTime(2014, 8, 15);
            DateTime time1 = new DateTime(2014, 9, 27);
            DateTime time2 = new DateTime(2014, 9, 28);
            DateTime time4 = new DateTime(2014, 9, 30);
            DateTime time7 = new DateTime(2014, 10, 1);
            DateTime time5 = new DateTime(2014, 10, 12);
            DateTime time6 = new DateTime(2014, 12, 1);

            rtn.Add(time);
            rtn.Add(time3);
            rtn.Add(time1);
            rtn.Add(time2);
            rtn.Add(time4);
            rtn.Add(time7);
            rtn.Add(time5);
            rtn.Add(time6);

            return rtn;
        }

               
         //上一个时间和下一个时间移除范围
        private List<DateTime> GetTimeList(DateTime d1, DateTime d2)
        {
            List<DateTime> hanTime = new List<DateTime>();
            DateTime dt = d1.AddMonths(1);

            hanTime.Add(new DateTime(dt.Year, dt.Month, 2));
            hanTime.Add(d2.AddDays(-1));

            return hanTime;
        }
    }
}


 



 

猜你喜欢

转载自blog.csdn.net/JasmineDawn/article/details/41445917