Silverlight自定义DatePicker
Xaml代码:
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
CS代码:
public partial class DatePickerEx : DatePicker
{
public DatePickerEx()
{
//InitializeComponent();
}
#region 私有字段
DatePickerTextBox CurrentTextBox;//DatePicker的文本框控件
Popup CurrentPopup;//DatePicker控件的TempPart
Calendar CurrentCalendar;//日历控件
Button CurrentButon;//DatePicker的Button控件
string DateFormat;//定义时间显示格式
string _InputText;
public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(DatePickerEx), new PropertyMetadata(false));
//public static readonly DependencyProperty InputTextProperty = DependencyProperty.Register("InputText", typeof(String), typeof(DatePickerEx), new PropertyMetadata("", InputTextChanged));
public static readonly DependencyProperty DateModeProperty = DependencyProperty.Register("DateMode", typeof(CalendarMode), typeof(DatePickerEx), new PropertyMetadata(CalendarMode.Month, DateModeChanged));
#endregion
#region 属性
///
/// 得到手动输入的值
///
public string InputText
{
//get { return (string)GetValue(InputTextProperty); }
//set { SetValue(InputTextProperty, value); }
get { return _InputText == null ? "" : _InputText; }
private set { _InputText = value; }
}
///
/// 启用和关闭手动输入
///
public bool IsReadOnly
{
get { return (bool)GetValue(IsReadOnlyProperty); }
set { SetValue(IsReadOnlyProperty, value); }
}
/
/// 输入时间类型
///
public CalendarMode DateMode
{
get { return (CalendarMode)GetValue(DateModeProperty); }
set { SetValue(DateModeProperty, value); }
}
#endregion
#region 重写方法及事件
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
this.CurrentTextBox = GetTemplateChild("TextBox") as DatePickerTextBox;
this.CurrentPopup = GetTemplateChild("Popup") as Popup;
this.CurrentButon = GetTemplateChild("Button") as Button;
#region 注册及绑定事件
if (null != this.CurrentTextBox)
{
//this.CurrentTextBox.BorderThickness = new Thickness(1);
//this.CurrentButon.Margin = new Thickness(-25, 0, 0, 0);
this.SetTextMode(this.CurrentTextBox);
this.CurrentTextBox.IsReadOnly = this.IsReadOnly;
this.CurrentTextBox.TextChanged += ((sender, e) =>
{
if (this.IsReadOnly)
this.CurrentTextBox.Text = this.SelectedDate.HasValue ? this.SelectedDate.Value.ToString(this.DateFormat) : "";
else
{
if (this.CurrentTextBox.Text.Trim().Length >= this.DateFormat.Length)
{
DateTime timeTemp;
if (DateTime.TryParse(this.CurrentTextBox.Text.Trim(), out timeTemp))
this.CurrentTextBox.Text = timeTemp.ToString(this.DateFormat);
}
this.InputText = this.CurrentTextBox.Text;
}
SetTextMode(this.CurrentTextBox);
});
}
this.CalendarClosed += new RoutedEventHandler(DatePickerEx_CalendarClosed);
if (this.DateMode == CalendarMode.Month)
return;
if (null != this.CurrentPopup)
//利用扩展方法找到Calendar控件
this.CurrentCalendar = this.CurrentPopup.Child.Descendents().OfType().FirstOrDefault();
if (null != this.CurrentButon)
this.CurrentButon.Click += ((sender, e) =>
{
this.CurrentPopup.IsOpen = true;
});
if (null != this.CurrentCalendar)
{
this.CurrentCalendar.IsTodayHighlighted = true;
this.CurrentCalendar.DisplayModeChanged += new
EventHandler(CurrentCalendar_DisplayModeChanged);
this.CurrentCalendar.DisplayMode = this.DateMode;
this.CurrentCalendar.LostMouseCapture += ((sender, e) =>
{
this.SelectedDate = this.DisplayDate;
});
}
#endregion
}
void CurrentCalendar_DisplayModeChanged(object sender, CalendarModeChangedEventArgs e)
{
this.CurrentCalendar.DisplayModeChanged -= CurrentCalendar_DisplayModeChanged;
this.CurrentCalendar.DisplayModeChanged += CurrentCalendar_DisplayModeChanged;
Calendar cal = (Calendar)sender;
//首次加载以及重新赋值DisplayMode Calendar视图情况判断
if (e.OldMode.Equals(CalendarMode.Year) && !e.NewMode.Equals(CalendarMode.Month))
cal.DisplayMode = e.NewMode;
else
cal.DisplayMode = this.DateMode;
//仅选择月 Calendar关闭情况判断
if (e.NewMode.Equals(CalendarMode.Month))
this.CurrentPopup.IsOpen = false;
//只选择年。 Calendar 关闭情况判断
if (this.DateMode == CalendarMode.Decade && e.NewMode == CalendarMode.Year && e.OldMode == this.DateMode)
this.CurrentPopup.IsOpen = false;
}
///
/// 设置日期显示格式
///
///
protected void SetTextMode(DatePickerTextBox tbx)
{
if (null == tbx)
return;
switch (this.DateMode)
{
case CalendarMode.Year:
DateFormat = "yyyy";
tbx.Watermark = "";
break;
case CalendarMode.Month:
DateFormat = "yyyy-MM";
tbx.Watermark = "";
break;
case CalendarMode.Decade:
DateFormat = "yyyy";
tbx.Watermark = "";
break;
default:
DateFormat = "yyyy-MM-dd";
break;
}
tbx.UpdateLayout();
}
#endregion
//protected override
#region 自定义事件
protected override void OnKeyUp(KeyEventArgs e)
{
base.OnKeyUp(e);
if (!this.IsReadOnly)
this.InputText = this.CurrentTextBox.Text;
}
protected virtual void DatePickerEx_CalendarClosed(object sender, RoutedEventArgs e)
{
if (null != this.SelectedDate)
this.Text = this.SelectedDate.Value.ToString(this.DateFormat);
this.InputText = this.Text;
}
//protected static void InputTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
//{
// DatePickerEx sender = d as DatePickerEx;
// if (null != sender.CurrentTextBox)
// sender.CurrentTextBox.Text = e.NewValue.ToString();
//}
protected static void DateModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
DatePickerEx sender = d as DatePickerEx;
if (null != sender.CurrentCalendar)
sender.CurrentCalendar.DisplayMode = (CalendarMode)e.NewValue;
sender.SetTextMode(sender.CurrentTextBox);
}
#endregion
}