Ejemplo n.º 1
0
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            _datePickerTextbox = new DatePickerTextBox();
            _masktextbox       = new MaskedTextBox();

            _datePickerTextbox            = this.Template.FindName("PART_TextBox", this) as DatePickerTextBox;
            _datePickerTextbox.Visibility = System.Windows.Visibility.Hidden;


            Grid _grid = new Grid();

            _grid = (Grid)_datePickerTextbox.Parent;
            _grid.Children.Insert(1, _masktextbox);


            #region MaskTextBox Properties Configuration

            _masktextbox.Mask = "00/00/0000";
            _masktextbox.AutoSelectBehavior = AutoSelectBehavior.OnFocus;
            _masktextbox.AutoMoveFocus      = true;
            _masktextbox.InsertKeyMode      = InsertKeyMode.Overwrite;



            #endregion

            #region  CodeBehind TwoWays DataBinding Configuration

            Binding datebinding = new Binding("date");
            datebinding.Source = this;
            datebinding.Mode   = BindingMode.TwoWay;
            datebinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;

            #endregion

            #region  Set DataBinding to DatePicker And MaskTextBox

            _datePickerTextbox.SetBinding(DatePickerTextBox.TextProperty, datebinding);
            _masktextbox.SetBinding(MaskedTextBox.TextProperty, datebinding);

            #endregion

            #region  Subscribe Events for MaskTextBox

            if (_masktextbox != null)
            {
                _masktextbox.TextChanged    += _datePickerTextbox_TextChanged;
                _masktextbox.PreviewKeyDown += _masktextbox_PreviewKeyDown;
            }
            #endregion
        }
Ejemplo n.º 2
0
        private void DatePickerTextBoxGotMouseCapture(object sender, MouseEventArgs e)
        {
            //初始没有鼠标焦点时,datePickerTextBox为只读控件,鼠标点击后,变为可编辑
            datePickerTextBox.IsReadOnly = false;

            //初始状态,如果没有值,并且有鼠标点击,则添加所有事件和绑定
            if (string.IsNullOrEmpty(datePickerTextBox.Text))
            {
                #region 因为前期不能有值,因此需要将绑定移到后台代码动态添加(因是否显示时间分情况添加不同绑定)

                if (NoTime)
                {
                    var dateTextBoxNoTimeConverter   = new DateTextBoxNoTimeConverter();
                    var datePickerTextBoxTextBinding = new Binding("SelectedDateTime")
                    {
                        Source = this, Converter = dateTextBoxNoTimeConverter, Mode = BindingMode.OneWay
                    };
                    datePickerTextBox.SetBinding(TextBox.TextProperty, datePickerTextBoxTextBinding);
                }
                else
                {
                    var dateTextBoxConverter         = new DateTextBoxConverter();
                    var datePickerTextBoxTextBinding = new Binding("SelectedDateTime")
                    {
                        Source = this, Converter = dateTextBoxConverter, Mode = BindingMode.OneWay
                    };
                    datePickerTextBox.SetBinding(TextBox.TextProperty, datePickerTextBoxTextBinding);
                }

                #endregion

                #region 原有功能添加数据源和事件

                button.Click += ButtonClick;
                calendar.SelectedDatesChanged += CalendarSelectedDatesChanged;
                if (!NoTime)
                {
                    hourComboBox.ItemsSource         = hourSource;
                    hourComboBox.SelectionChanged   += HourComboBoxSelectionChanged;
                    minuteComboBox.ItemsSource       = clockSource;
                    minuteComboBox.SelectionChanged += MinuteComboBoxSelectionChanged;
                    secondComboBox.ItemsSource       = clockSource;
                    secondComboBox.SelectionChanged += SecondComboBoxSelectionChanged;
                    ampmComboBox.ItemsSource         = ampmSource.Values;
                    ampmComboBox.SelectionChanged   += AmPmComboBoxSelectionChanged;
                }

                #endregion

                #region 新加功能添加事件

                //输入的数字必须在KeyUp事件才能处理,因此,将取消特定按键的输入放在PreviewKeyDown,将所有的处理逻辑放在KeyUp
                datePickerTextBox.KeyUp          += DatePickerTextBoxKeyUp;          //方向与空格键不支持KeyDown,事件优先级不够,会被挂起
                datePickerTextBox.PreviewKeyDown += DatePickerTextBoxPreviewKeyDown; //因为要彻底取消空格和字母的输入,因此采用PreviewKeyDown对可以支持KeyDown的按键的输入操作进行拦截

                #endregion

                //默认点击后选中的是月份
                selectCell = 2;
            }
            else
            {
                selectCell = GetMouseCell(datePickerTextBox.CaretIndex);
            }
            UpdateStartAndEnd(selectCell, ref start, ref length);
            datePickerTextBox.Select(start, length);
        }