public override void OnApplyTemplate() { base.OnApplyTemplate(); DatePicker = GetTemplateChild("PART_DatePicker") as DatePicker; if (DatePicker == null) { return; } DatePicker.SelectedDateChanged -= DatePicker_SelectedDateChanged; DatePicker.SelectedDateChanged += DatePicker_SelectedDateChanged; this.HourTextBox = DatePicker.FindChildrenFromTemplate <TextBox>("PART_Hours_TB"); if (HourTextBox == null) { return; } this.HourTextBox.GotMouseCapture -= OnTextBox_GotMouseCapture; this.HourTextBox.GotMouseCapture += OnTextBox_GotMouseCapture; this.HourTextBox.LostKeyboardFocus -= HourTextBox_LostKeyboardFocus; this.HourTextBox.LostKeyboardFocus += HourTextBox_LostKeyboardFocus; this.HourTextBox.PreviewKeyDown -= OnHourTextBox_PreviewKeyDown; this.HourTextBox.PreviewKeyDown += OnHourTextBox_PreviewKeyDown; this.HourTextBox.KeyUp -= HourTextBox_KeyUp; this.HourTextBox.KeyUp += HourTextBox_KeyUp; this.MinuteTextBox = DatePicker.FindChildrenFromTemplate <TextBox>("PART_Minutes_TB"); if (MinuteTextBox == null) { return; } this.MinuteTextBox.GotMouseCapture -= OnTextBox_GotMouseCapture; this.MinuteTextBox.GotMouseCapture += OnTextBox_GotMouseCapture; this.MinuteTextBox.PreviewKeyDown -= OnSecondOrMinuteTextBox_PreviewKeyDown; this.MinuteTextBox.PreviewKeyDown += OnSecondOrMinuteTextBox_PreviewKeyDown; this.MinuteTextBox.KeyUp -= MinuteTextBox_KeyUp; this.MinuteTextBox.KeyUp += MinuteTextBox_KeyUp; this.MinuteTextBox.LostKeyboardFocus -= MinuteTextBox_LostKeyboardFocus; this.MinuteTextBox.LostKeyboardFocus += MinuteTextBox_LostKeyboardFocus; this.SecondsTextBox = DatePicker.FindChildrenFromTemplate <TextBox>("PART_Seconds_TB"); if (this.SecondsTextBox == null) { return; } this.SecondsTextBox.GotMouseCapture -= OnTextBox_GotMouseCapture; this.SecondsTextBox.GotMouseCapture += OnTextBox_GotMouseCapture; this.SecondsTextBox.PreviewKeyDown -= OnSecondOrMinuteTextBox_PreviewKeyDown; this.SecondsTextBox.PreviewKeyDown += OnSecondOrMinuteTextBox_PreviewKeyDown; this.SecondsTextBox.KeyUp -= SecondsTextBox_KeyUp; this.SecondsTextBox.KeyUp += SecondsTextBox_KeyUp; this.SecondsTextBox.LostKeyboardFocus -= SecondsTextBox_LostKeyboardFocus; this.SecondsTextBox.LostKeyboardFocus += SecondsTextBox_LostKeyboardFocus; var popup = DatePicker.FindChildrenFromTemplate <Popup>("PART_Popup"); if (popup == null) { return; } popup.Opened -= OnDatePickerPopupOpened; popup.Opened += OnDatePickerPopupOpened; popup.Closed -= OnDatePickerPopupClosed; popup.Closed += OnDatePickerPopupClosed; var calendar = popup.Child as Calendar; var calendarItem = calendar.FindChildrenFromTemplate <CalendarItem>("PART_CalendarItem"); if (calendarItem == null) { return; } HourNumericDown = calendarItem.FindChildrenFromTemplate <NumericUpDown>("PART_Hours_NumericUpDown"); if (HourNumericDown == null) { return; } HourNumericDown.ValueChanged -= NumericDown_ValueChanged; HourNumericDown.ValueChanged += NumericDown_ValueChanged; MinuteNumericDown = calendarItem.FindChildrenFromTemplate <NumericUpDown>("PART_Minutes_NumericUpDown"); if (MinuteNumericDown == null) { return; } MinuteNumericDown.ValueChanged -= NumericDown_ValueChanged; MinuteNumericDown.ValueChanged += NumericDown_ValueChanged; SecondsNumericDown = calendarItem.FindChildrenFromTemplate <NumericUpDown>("PART_Seconds_NumericUpDown"); if (SecondsNumericDown == null) { return; } SecondsNumericDown.ValueChanged -= NumericDown_ValueChanged; SecondsNumericDown.ValueChanged += NumericDown_ValueChanged; }