Silverlight 學習——重寫DatePicker

一、問題與目的:

             學習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" />

   

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章