Silverlight 学习——重写DatePicker
来源:互联网 发布:网络机顶盒升级包下载 编辑:程序博客网 时间:2024/05/05 14:51
一、问题与目的:
学习Silverlight已经有一段时间了。一直没时间写一篇学习笔记(如有错误,望同仁们指出,其实也很简单,就当做个笔记了)。
关于Silverlight的DatePicker控件,原来的DatePicker控件,不可控制是否能手动输入。如果允许用户手动输入,则会出现错误输入日期格式的情况,但是,DatePicker未提供一个属性来获取DatePicker错误输入的内容,不能做到正确提示,也不可代码清空文本内容,当然,错误输入可引发DateValidationError事件,但是只有事件CalendarClosed和CalendarOpened引发此事件验证,很不方便。因此重写了DatePicker!
二、解决办法
在控件中添加IsReadOnly属性,控制是否手动输入,再添加InputText属性,获取错误输入的文本内容。可同步控件本身的Text属性。这样,既可以控制用户手动输入,也可以获取用户错误输入内容,方便代码人员提示!
三、代码
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Data;
public class DatePickerEx : DatePicker
{
#region 私有字段
TextBox CurrentTextBox;
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));
#endregion
#region 属性
/// <summary>
/// 得到手动输入的值
/// </summary>
public string InputText
{
get { return (string)GetValue(InputTextProperty); }
set { SetValue(InputTextProperty, value); }
}
/// <summary>
/// 启用和关闭手动输入
/// </summary>
public bool IsReadOnly
{
get { return (bool)GetValue(IsReadOnlyProperty); }
set { SetValue(IsReadOnlyProperty, value); }
}
#endregion
#region 重写方法及事件
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
this.CurrentTextBox = GetTemplateChild("TextBox") as TextBox;
this.CalendarClosed += new RoutedEventHandler(DatePickerEx_CalendarClosed);
}
protected override void OnKeyUp(KeyEventArgs e)
{
base.OnKeyUp(e);
if (!this.IsReadOnly)
this.InputText = this.CurrentTextBox.Text;
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (this.IsReadOnly)
e.Handled = this.IsReadOnly;
}
#endregion
#region 自定义事件
public virtual void DatePickerEx_CalendarClosed(object sender, RoutedEventArgs e)
{
this.InputText = this.Text;
}
protected static void InputTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
DatePickerEx sender = d as DatePickerEx;
sender.CurrentTextBox.Text = e.NewValue.ToString();
}
#endregion
}
使用方式在XAML页 添加(xmlns:ControlsEx="clr-namespace:FXD.CommonLib.Controls;assembly=FXD.CommonLib") 引用,当然,此组件引用根据实际情况而改变; 使用控件时; <controlsEx:DatePickerEx IsReadOnly="True" Height="23" Margin="5,0,0,0" InputText="{Binding SearchDate,Mode=TwoWay}" Width="120" />
- Silverlight 学习——重写DatePicker
- Silverlight 学习——重写DatePicker (二)
- Silverlight中的Calendar和DatePicker学习
- android学习——DatePicker 和 TimePicker
- silverlight: DatePicker Control dateformat
- Silverlight学习笔记—2
- Silverlight学习笔记—3
- Android开发————DatePicker学习笔记
- Android学习笔记之——DatePicker&TimePicker
- android学习笔记——DatePicker、TimePicker探究
- DatePicker学习
- Silverlight之DatePicker日期格式化
- 日期选择器——datepicker
- Silverlight 2.0 学习笔记——RIAs
- Silverlight 2.0学习笔记——WPF
- Silverlight 2.0学习笔记——XAML
- Silverlight Toolkit 中 DatePicker & TimePicker 的用法
- 【C#学习】——重写 覆盖 重载
- 高效地显示Bitmap图片
- 获得URI的路径
- ANDROID 经典开源项目
- 关于ActionScript中那些你不知道的事情
- input file 赋值方法
- Silverlight 学习——重写DatePicker
- linux下删除某一时间段的文件
- J2EE到底是什么?
- oracle导入导出--批处理
- 从GridView中导出全部页数据示例
- ResolveUrl() 和 ResolveClientUrl()
- c# 树状视图(TreeView类)
- 在asp.net中对url地址统一编码的示例
- java代码缺陷自动分析工具之FindBugs介绍