/// <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); } } }