DatePicker的BlackoutDates,日曆控制項可選範圍設定。,datetimepicker控制項
簡述:
在已存在的時間列表中,存在以下條件
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; } }}