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

2015-01-27 06:10:44 · 作者: · 浏览: 8

简述:

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

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

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

画面代码:

  

    
    
     
   

  

后台代码:

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
  
    list = this.GetDateTime();

            List
   
     hanTime = new List
    
     (); 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
     
       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
  
    GetDateTime()
        {
            List
   
     rtn = new List
    
     (); 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
  
    GetTimeList(DateTime d1, DateTime d2)
        {
            List
   
     hanTime = new List
    
     (); DateTime dt = d1.AddMonths(1); hanTime.Add(new DateTime(dt.Year, dt.Month, 2)); hanTime.Add(d2.AddDays(-1)); return hanTime; } } }