Пример #1
0
 public override void OnApplyTemplate()
 {
     base.OnApplyTemplate();
     _datePickerTextBox            = Template.FindName("PART_TextBox", this) as DatePickerTextBox;
     _datePickerTextBox.Background = null;
     _datePickerTextBox.Foreground = new SolidColorBrush(Color.FromRgb(241, 241, 241));
 }
Пример #2
0
        /// <summary>
        ///     When overridden in a derived class, is invoked whenever application code or internal processes call
        ///     <see cref="M:System.Windows.FrameworkElement.ApplyTemplate" />.
        /// </summary>
        public override void OnApplyTemplate()
        {
            UnSubscribeEvents();

            base.OnApplyTemplate();

            _popup        = GetTemplateChild(ElementPopup) as Popup;
            _button       = GetTemplateChild(ElementButton) as Button;
            _hourInput    = GetTemplateChild(ElementHourPicker) as Selector;
            _minuteInput  = GetTemplateChild(ElementMinutePicker) as Selector;
            _secondInput  = GetTemplateChild(ElementSecondPicker) as Selector;
            _hourHand     = GetTemplateChild(ElementHourHand) as FrameworkElement;
            _ampmSwitcher = GetTemplateChild(ElementAmPmSwitcher) as Selector;
            _minuteHand   = GetTemplateChild(ElementMinuteHand) as FrameworkElement;
            _secondHand   = GetTemplateChild(ElementSecondHand) as FrameworkElement;
            _textBox      = GetTemplateChild(ElementTextBox) as DatePickerTextBox;

            SetHandVisibility(HandVisibility);
            SetPickerVisibility(PickerVisibility);

            SetHourPartValues(SelectedDateTime.GetValueOrDefault().TimeOfDay);
            WriteValueToTextBox();

            SetDefaultTimeOfDayValues();
            SubscribeEvents();
            ApplyCulture();
            ApplyBindings();
        }
    private void SetWatermark()
    {
        FieldInfo fiTextBox = typeof(DatePicker).GetField("_textBox", BindingFlags.Instance | BindingFlags.NonPublic);

        if (fiTextBox != null)
        {
            DatePickerTextBox dateTextBox = (DatePickerTextBox)fiTextBox.GetValue(this);
            if (dateTextBox != null)
            {
                if (string.IsNullOrWhiteSpace(this.Watermark))
                {
                    this.Watermark = "Custom watermark";
                }
                // if you set property this way then you need to override OnSelectedDateChanged event
                //PropertyInfo piWatermark = typeof(DatePickerTextBox).GetProperty("Watermark", BindingFlags.Instance | BindingFlags.NonPublic);
                //if (piWatermark != null)
                //{
                //    piWatermark.SetValue(dateTextBox, this.Watermark, null);
                //}
                var partWatermark = dateTextBox.Template.FindName("PART_Watermark", dateTextBox) as ContentControl;
                if (partWatermark != null)
                {
                    partWatermark.Foreground = new SolidColorBrush(Colors.Gray);
                    partWatermark.Content    = this.Watermark;
                }
            }
        }
    }
Пример #4
0
        /// <summary>
        /// Called when watermark property is changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="args">
        /// The <see cref="DependencyPropertyChangedEventArgs"/>
        /// instance containing the event data.
        /// </param>
        private static void OnWatermarkPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            DatePickerTextBox datePickerTextBox = sender as DatePickerTextBox;

            Debug.Assert(datePickerTextBox != null, "The source is not an instance of a DatePickerTextBox!");
            datePickerTextBox.OnWatermarkChanged();
            datePickerTextBox.ChangeVisualState();
        }
Пример #5
0
    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        DatePickerTextBox textBox = (DatePickerTextBox)this.GetTemplateChild("TextBox");

        textBox.GotFocus  += new RoutedEventHandler(textBox_GotFocus);
        textBox.LostFocus += new RoutedEventHandler(textBox_LostFocus);
    }
Пример #6
0
        private void dp_endedatum_GotKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e)
        {
            DatePickerTextBox dptb_endedatum = (DatePickerTextBox)dp_endedatum.Template.FindName("PART_TextBox", dp_endedatum);

            if (dptb_endedatum != null)
            {
                dptb_endedatum.SelectAll();
            }
        }
        private void Dp_geburtsdatum_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            DatePickerTextBox dptb_geburtsdatum = (DatePickerTextBox)dp_geburtsdatum.Template.FindName("PART_TextBox", dp_geburtsdatum);

            if (dptb_geburtsdatum != null)
            {
                dptb_geburtsdatum.SelectAll();
            }
        }
Пример #8
0
        void _DatePickerTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            DatePickerTextBox _DatePickerTextBox = sender as DatePickerTextBox;

            if (string.IsNullOrEmpty(_DatePickerTextBox.Text))
            {
                _DatePickerTextBox.Text = FirstDateTime.ToString("yyyy-MM-dd");
            }
        }
        private void Dp_geburtsdatum_Loaded(object sender, RoutedEventArgs e)
        {
            DatePickerTextBox datePickerTextBox = (DatePickerTextBox)dp_geburtsdatum.Template.FindName("PART_TextBox", dp_geburtsdatum);

            if (datePickerTextBox != null)
            {
                datePickerTextBox.Focus();
            }
        }
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            _datePickerTextBox = this.Template.FindName("PART_TextBox", this) as DatePickerTextBox;
            if (_datePickerTextBox != null)
            {
                _datePickerTextBox.TextChanged += dptb_TextChanged;
            }
        }
Пример #11
0
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            DatePickerTextBox box = GetTemplateChild("PART_TextBox") as DatePickerTextBox;

            box.ApplyTemplate();

            ContentControl watermark = box.Template.FindName("PART_Watermark", box) as ContentControl;

            watermark.Content = DateTime.Now.ToString("yyyy/MM/dd");
        }
Пример #12
0
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            DatePickerTextBox textBox = GetTemplateChild(PART_TextBox) as DatePickerTextBox;

            if (textBox != null)
            {
                textBox.Background = new SolidColorBrush(Colors.Transparent);
            }
        }
Пример #13
0
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            DatePickerTextBox box = base.GetTemplateChild("PART_TextBox") as DatePickerTextBox;

            box.ApplyTemplate();

            ContentControl watermark = box.Template.FindName("PART_Watermark", box) as ContentControl;

            watermark.Content = WatermarkText;
        }
Пример #14
0
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            DatePickerTextBox _textBox = GetTemplateChild("TextBox") as DatePickerTextBox;

            if (_textBox != null)
            {
                _textBox.AddHandler(KeyDownEvent, new KeyEventHandler(TextBox_KeyDown), true);
            }
        }
Пример #15
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
        }
Пример #16
0
        public void SetWatermarkText(string text)
        {
            DatePickerTextBox datePickerTextBox = FindVisualChild <DatePickerTextBox>(this);

            if (datePickerTextBox != null)
            {
                ContentControl watermark = datePickerTextBox.Template.FindName("PART_Watermark", datePickerTextBox) as ContentControl;
                if (watermark != null)
                {
                    watermark.Content = text;
                }
            }
        }
Пример #17
0
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            datePickerTextBox = GetTemplateChild(ElementDateTimeTextBox) as DatePickerTextBox;
            button            = GetTemplateChild(ElementButton) as Button;
            popup             = GetTemplateChild(ElementPopup) as Popup;
            calendar          = GetTemplateChild(ElementCalendar) as Calendar;
            hourComboBox      = GetTemplateChild(ElementHourComboBox) as ComboBox;
            minuteComboBox    = GetTemplateChild(ElementMinuteComboBox) as ComboBox;
            secondComboBox    = GetTemplateChild(ElementSecondComboBox) as ComboBox;
            ampmComboBox      = GetTemplateChild(ElementAmPmSwithcerComboBox) as ComboBox;

            datePickerTextBox.GotMouseCapture += DatePickerTextBoxGotMouseCapture;
        }
        /// <summary>
        ///
        /// </summary>d
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            SetDateFormat();

            DatePickerTextBox box = base.GetTemplateChild("PART_TextBox") as DatePickerTextBox;

            box.ApplyTemplate();

            ContentControl watermark   = box.Template.FindName("PART_Watermark", box) as ContentControl;
            string         datePattern = CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern;

            watermark.Content = datePattern.ToUpper();
        }
Пример #19
0
        private void DatePickerLoaded(object sender, RoutedEventArgs e)
        {
            if (!(sender is DatePicker datePicker))
            {
                return;
            }
            DatePickerTextBox datePickerTextBox =
                FindVisualChild <DatePickerTextBox>(datePicker);

            if (datePickerTextBox?.Template.FindName("PART_Watermark", datePickerTextBox)
                is ContentControl watermark)
            {
                watermark.Content = "Enter your date of birth";
            }
        }
Пример #20
0
        public override void OnApplyTemplate()
        {
            _button        = (Button)GetTemplateChild(ElementButton);
            _button.Click += ButtonClick;

            _datePickerTextBox                  = (DatePickerTextBox)GetTemplateChild(ElementDatePickerTextBox);
            _datePickerTextBox.MouseDown       += DatePickerTextBoxMouseDown;
            _datePickerTextBox.KeyUp           += DatePickerTextBoxKeyUp;
            _datePickerTextBox.GotMouseCapture += DatePickerTextBoxGotMouseCapture;

            _popup = (Popup)GetTemplateChild(ElementPopup);
            _popup.PlacementTarget = _datePickerTextBox;

            _calendar = (Calendar)GetTemplateChild(ElementCalendar);
            _calendar.DisplayDateChanged   += CalendarDisplayDateChanged;
            _calendar.SelectedDatesChanged += CalendarSelectedDatesChanged;
        }
Пример #21
0
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            Popup popup = base.GetTemplateChild("Popup") as Popup;

            m_Calendar = null;

            if (popup != null && popup.Child != null)
            {
                Canvas canvas = popup.Child as Canvas;
                if (canvas != null)
                {
                    foreach (UIElement item in canvas.Children)
                    {
                        m_Calendar = item as Calendar;
                        if (m_Calendar != null)
                        {
                            break;
                        }
                    }
                }
            }

            m_TextBox = base.GetTemplateChild("TextBox") as DatePickerTextBox;
            //m_TextBox.FontSize = 11;
            //m_TextBox.Padding = new Thickness(2, 0, 2, 0);
            m_TextBox.VerticalAlignment = VerticalAlignment.Center;
            m_TextBox.Text = " ";

            if (m_TextBox != null)
            {
                m_TextBox.IsReadOnly = true;
            }

            var readOnlyVisualElement = base.GetTemplateChild("ReadOnlyVisualElement") as Border;

            if (readOnlyVisualElement != null)
            {
                readOnlyVisualElement.Background = new SolidColorBrush(Colors.Transparent);
            }

            if (m_Calendar != null)
            {
                m_Calendar.DisplayDateChanged += new EventHandler <CalendarDateChangedEventArgs>(calendar_DisplayDateChanged);
            }
        }
Пример #22
0
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            // this.PART_TextBox = VisualHelper.FindVisualElement<DatePickerTextBox>(this, "PART_TextBox_New");
            this.PART_TextBox = WPFControlsUtils.FindChildOfType <DatePickerTextBox>(this, "PART_TextBox_New");
            // this.PART_Popup = VisualHelper.FindVisualElement<Popup>(this, "PART_Popup_New");
            this.PART_Popup = WPFControlsUtils.FindChildOfType <Popup>(this, "PART_Popup_New");

            this.PART_Calendar       = GetTemplateChild("PART_Calendar") as Calendar;
            this.PART_TimePicker     = GetTemplateChild("PART_TimePicker") as TimePicker;
            this.PART_Clear_Button   = GetTemplateChild("PART_Clear_Button") as Button;
            this.PART_Today_Button   = GetTemplateChild("PART_Today_Button") as Button;
            this.PART_Confirm_Button = GetTemplateChild("PART_Confirm_Button") as Button;

            if (this.PART_Calendar != null)
            {
                this.PART_Calendar.AddHandler(Button.MouseLeftButtonDownEvent, new RoutedEventHandler(DayButton_MouseLeftButtonUp), true);
            }

            if (this.PART_TimePicker != null)
            {
                this.PART_TimePicker.SelectedTimeChanged += PART_TimePicker_SelectedTimeChanged;;
            }

            if (this.PART_Popup != null)
            {
                this.PART_Popup.Opened += PART_Popup_Opened;
                this.PART_Popup.Closed += PART_Popup_Closed;
            }

            if (this.PART_Today_Button != null)
            {
                this.PART_Today_Button.Click += PART_Today_Button_Click;
            }

            if (this.PART_Confirm_Button != null)
            {
                this.PART_Confirm_Button.Click += PART_Confirm_Button_Click;
            }

            if (this.PART_Clear_Button != null)
            {
                this.PART_Clear_Button.Click += PART_Clear_Button_Click;
            }
        }
Пример #23
0
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            if (this.SelectedDate == null)
            {
                this.SelectedDate = DateTime.Now;
            }
            FirstDateTime = this.SelectedDate.Value;
            DatePickerTextBox _DatePickerTextBox = GetTemplateChild("TextBox") as DatePickerTextBox;

            _DatePickerTextBox.TextChanged += new TextChangedEventHandler(_DatePickerTextBox_TextChanged);
            if (AllowCleanDate)
            {
                _DatePickerTextBox.IsReadOnly = false;
            }
            else
            {
                _DatePickerTextBox.IsReadOnly = true;
            }
        }
Пример #24
0
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            DatePickerTextBox _textBox = GetTemplateChild("PART_TextBox") as DatePickerTextBox;

            if (_textBox != null)
            {
                _textBox.FocusVisualStyle = null;
                _textBox.Style            = sTextBoxStyle;
            }

            Button _calendarBtn = GetTemplateChild("PART_Button") as Button;

            if (_calendarBtn != null)
            {
                _calendarBtn.Width               = 20;
                _calendarBtn.Height              = 20;
                _calendarBtn.Margin              = new Thickness(0, -1, -3, -3);
                _calendarBtn.RenderTransform     = new ScaleTransform(0.75, 0.75);
                _calendarBtn.HorizontalAlignment = HorizontalAlignment.Center;
            }
        }
Пример #25
0
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            textBox = this.GetTemplateChild("PART_TextBox") as DatePickerTextBox;
            popup   = this.GetTemplateChild("PART_Popup") as Popup;

            if (AlternativeCalendarStyle != null)
            {
                System.Windows.Controls.Calendar calendar = popup.Child as System.Windows.Controls.Calendar;

                calendar.Style = AlternativeCalendarStyle;
                calendar.ApplyTemplate();

                goToTodayButton = calendar.Template.FindName("PART_GoToTodayButton", calendar) as Button;
                if (goToTodayButton != null)
                {
                    gotoTodayCommand        = new SimpleCommand(CanExecuteGoToTodayCommand, ExecuteGoToTodayCommand);
                    goToTodayButton.Command = gotoTodayCommand;
                }
            }
            textBox.PreviewKeyDown -= new KeyEventHandler(DatePickerTextBox_PreviewKeyDown); //unhook
            textBox.PreviewKeyDown += new KeyEventHandler(DatePickerTextBox_PreviewKeyDown); //hook
        }
Пример #26
0
 private void PART_TextBox_Loaded(object sender, RoutedEventArgs e)
 {
     //这个具有特殊性, 第一次初始后, 需要记录这个对象, 才能在后面验证动画. 因为模板里面的名字引不出..
     dptx         = (DatePickerTextBox)sender;
     dptx.Loaded -= PART_TextBox_Loaded;//干掉自己...
 }
Пример #27
0
        /// <summary>
        /// Builds the visual tree for the DatePicker control when a new template is applied.
        /// </summary>
        public override void OnApplyTemplate()
        {
            if (_popUp != null)
            {
                _popUp.RemoveHandler(PreviewMouseLeftButtonDownEvent, new MouseButtonEventHandler(PopUp_PreviewMouseLeftButtonDown));
                _popUp.Opened -= PopUp_Opened;
                _popUp.Closed -= PopUp_Closed;
                _popUp.Child   = null;
            }

            if (_dropDownButton != null)
            {
                _dropDownButton.Click -= DropDownButton_Click;
                _dropDownButton.RemoveHandler(MouseLeaveEvent, new MouseEventHandler(DropDownButton_MouseLeave));
            }

            if (_textBox != null)
            {
                _textBox.RemoveHandler(TextBox.KeyDownEvent, new KeyEventHandler(TextBox_KeyDown));
                _textBox.RemoveHandler(TextBox.TextChangedEvent, new TextChangedEventHandler(TextBox_TextChanged));
                _textBox.RemoveHandler(TextBox.LostFocusEvent, new RoutedEventHandler(TextBox_LostFocus));
            }

            base.OnApplyTemplate();

            _popUp = GetTemplateChild(ElementPopup) as Popup;

            if (_popUp != null)
            {
                _popUp.AddHandler(PreviewMouseLeftButtonDownEvent, new MouseButtonEventHandler(PopUp_PreviewMouseLeftButtonDown));
                _popUp.Opened += PopUp_Opened;
                _popUp.Closed += PopUp_Closed;
                _popUp.Child   = this._calendar;

                if (this.IsDropDownOpen)
                {
                    this._popUp.IsOpen = true;
                }
            }

            _dropDownButton = GetTemplateChild(ElementButton) as Button;
            if (_dropDownButton != null)
            {
                _dropDownButton.Click += DropDownButton_Click;
                _dropDownButton.AddHandler(MouseLeaveEvent, new MouseEventHandler(DropDownButton_MouseLeave), true);

                // If the user does not provide a Content value in template, we provide a helper text that can be used in Accessibility
                // this text is not shown on the UI, just used for Accessibility purposes
                if (_dropDownButton.Content == null)
                {
                    _dropDownButton.Content = SR.Get(SRID.DatePicker_DropDownButtonName);
                }
            }

            _textBox = GetTemplateChild(ElementTextBox) as DatePickerTextBox;

            if (this.SelectedDate == null)
            {
                SetWaterMarkText();
            }

            if (_textBox != null)
            {
                _textBox.AddHandler(TextBox.KeyDownEvent, new KeyEventHandler(TextBox_KeyDown), true);
                _textBox.AddHandler(TextBox.TextChangedEvent, new TextChangedEventHandler(TextBox_TextChanged), true);
                _textBox.AddHandler(TextBox.LostFocusEvent, new RoutedEventHandler(TextBox_LostFocus), true);

                if (this.SelectedDate == null)
                {
                    if (!string.IsNullOrEmpty(this._defaultText))
                    {
                        _textBox.Text = this._defaultText;
                        SetSelectedDate();
                    }
                }
                else
                {
                    _textBox.Text = this.DateTimeToString((DateTime)this.SelectedDate);
                }
            }
        }
Пример #28
0
 public static IObservable <EventPattern <MouseButtonEventArgs> > MouseDownObserver(this DatePickerTextBox This)
 {
     return(Observable.FromEventPattern <MouseButtonEventHandler, MouseButtonEventArgs>(h => This.MouseDown += h, h => This.MouseDown -= h));
 }
Пример #29
0
        /// <summary>
        /// Builds the visual tree for the DatePicker control when a new template is applied.
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            if (_popUp != null)
            {
                _popUp.Child = null;
            }

            _popUp = GetTemplateChild(ElementPopup) as Popup;

            if (_popUp != null)
            {
                if (this._outsideCanvas == null)
                {
                    _outsideCanvas                 = new Canvas();
                    _outsidePopupCanvas            = new Canvas();
                    _outsidePopupCanvas.Background = new SolidColorBrush(Colors.Transparent);
                    _outsideCanvas.Children.Add(this._outsidePopupCanvas);
                    _outsideCanvas.Children.Add(this._calendar);
                    _outsidePopupCanvas.MouseLeftButtonDown += new MouseButtonEventHandler(OutsidePopupCanvas_MouseLeftButtonDown);
                }

                _popUp.Child = this._outsideCanvas;
                _root        = GetTemplateChild(ElementRoot) as FrameworkElement;

                if (this.IsDropDownOpen)
                {
                    OpenDropDown();
                }
            }

            if (_dropDownButton != null)
            {
                _dropDownButton.Click -= new RoutedEventHandler(DropDownButton_Click);
            }

            _dropDownButton = GetTemplateChild(ElementButton) as Button;
            if (_dropDownButton != null)
            {
                _dropDownButton.Click    += new RoutedEventHandler(DropDownButton_Click);
                _dropDownButton.IsTabStop = false;

                //If the user does not provide a Content value in template, we provide a helper text that can be used in Accessibility
                //this text is not shown on the UI, just used for Accessibility purposes
                if (_dropDownButton.Content == null)
                {
                    _dropDownButton.Content = Resource.DatePicker_DropDownButtonName;
                }
            }

            if (_textBox != null)
            {
                _textBox.KeyDown     -= new KeyEventHandler(TextBox_KeyDown);
                _textBox.TextChanged -= new TextChangedEventHandler(TextBox_TextChanged);
                _textBox.GotFocus    -= new RoutedEventHandler(TextBox_GotFocus);
            }

            _textBox = GetTemplateChild(ElementTextBox) as DatePickerTextBox;

            UpdateDisabledVisual();
            if (this.SelectedDate == null)
            {
                SetWaterMarkText();
            }

            if (_textBox != null)
            {
                _textBox.KeyDown     += new KeyEventHandler(TextBox_KeyDown);
                _textBox.TextChanged += new TextChangedEventHandler(TextBox_TextChanged);
                _textBox.GotFocus    += new RoutedEventHandler(TextBox_GotFocus);

                if (this.SelectedDate == null)
                {
                    if (!string.IsNullOrEmpty(this._defaultText))
                    {
                        _textBox.Text = this._defaultText;
                        SetSelectedDate();
                    }
                }
                else
                {
                    _textBox.Text = this.DateTimeToString((DateTime)this.SelectedDate);
                }
            }
        }
Пример #30
0
        /// <summary>
        /// Builds the visual tree for the DatePicker control when a new template is applied.
        /// </summary>
        public override void OnApplyTemplate()
        {
            if (_popUp != null)
            {
                _popUp.RemoveHandler(PreviewMouseLeftButtonDownEvent, new MouseButtonEventHandler(PopUp_PreviewMouseLeftButtonDown));
                _popUp.Opened -= PopUp_Opened;
                _popUp.Closed -= PopUp_Closed;
                _popUp.Child   = null;
            }

            if (_dropDownButton != null)
            {
                _dropDownButton.Click -= DropDownButton_Click;
                _dropDownButton.RemoveHandler(MouseLeaveEvent, new MouseEventHandler(DropDownButton_MouseLeave));
            }

            if (_textBox != null)
            {
                _textBox.RemoveHandler(TextBox.KeyDownEvent, new KeyEventHandler(TextBox_KeyDown));
                _textBox.RemoveHandler(TextBox.TextChangedEvent, new TextChangedEventHandler(TextBox_TextChanged));
                _textBox.RemoveHandler(TextBox.LostFocusEvent, new RoutedEventHandler(TextBox_LostFocus));
            }

            base.OnApplyTemplate();

            _popUp = GetTemplateChild(ElementPopup) as Popup;

            if (_popUp != null)
            {
                _popUp.AddHandler(PreviewMouseLeftButtonDownEvent, new MouseButtonEventHandler(PopUp_PreviewMouseLeftButtonDown));
                _popUp.Opened += PopUp_Opened;
                _popUp.Closed += PopUp_Closed;
                _popUp.Child   = this._calendar;


                if (this.IsDropDownOpen)
                {
                    this._popUp.IsOpen = true;
                }
            }

            _dropDownButton = GetTemplateChild(ElementButton) as Button;
            if (_dropDownButton != null)
            {
                _dropDownButton.Click += DropDownButton_Click;
                _dropDownButton.AddHandler(MouseLeaveEvent, new MouseEventHandler(DropDownButton_MouseLeave), true);

                // If the user does not provide a Content value in template, we provide a helper text that can be used in Accessibility
                // this text is not shown on the UI, just used for Accessibility purposes
                if (_dropDownButton.Content == null)
                {
                    _dropDownButton.Content = SR.Get(SRID.DatePicker_DropDownButtonName);
                }
            }

            _textBox = GetTemplateChild(ElementTextBox) as DatePickerTextBox;

            UpdateDisabledVisual();
            if (this.SelectedDate == null)
            {
                SetWaterMarkText();
            }

            if (_textBox != null)
            {
                _textBox.AddHandler(TextBox.KeyDownEvent, new KeyEventHandler(TextBox_KeyDown), true);
                _textBox.AddHandler(TextBox.TextChangedEvent, new TextChangedEventHandler(TextBox_TextChanged), true);
                _textBox.AddHandler(TextBox.LostFocusEvent, new RoutedEventHandler(TextBox_LostFocus), true);

                if (this.SelectedDate == null)
                {
                    if (!string.IsNullOrEmpty(this._defaultText))
                    {
                        _textBox.Text = this._defaultText;
                        SetSelectedDate();
                    }
                }
                else
                {
                    _textBox.Text = this.DateTimeToString((DateTime)this.SelectedDate);
                }
            }

            if (IsButtonDynamicShow)
            {
                _dropDownButton.Visibility = System.Windows.Visibility.Collapsed;
                Grid oGrid = GetTemplateChild("PART_Root") as Grid;
                oGrid.MouseEnter += new MouseEventHandler(_popUp_MouseEnter);
                oGrid.MouseLeave += new MouseEventHandler(_popUp_MouseLeave);
            }
            if (HideTextBoxBackground)
            {
                _textBox.Watermark       = string.Empty;
                _textBox.BorderBrush     = null;
                _textBox.BorderThickness = new Thickness(0);
                _textBox.Background      = null;
                this.BorderThickness     = new Thickness(0);
            }
        }