/// <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();
        }
Esempio n. 2
0
        /// <inheritdoc/>
        protected override void SetSelectedDateTime()
        {
            if (textBox is null)
            {
                return;
            }

            const DateTimeStyles dateTimeParseStyle = DateTimeStyles.AllowWhiteSpaces
                                                      & DateTimeStyles.AssumeLocal
                                                      & DateTimeStyles.NoCurrentDateDefault;

            if (DateTime.TryParse(textBox.Text, SpecificCultureInfo, dateTimeParseStyle, out var timeSpan))
            {
                SetCurrentValue(SelectedDateTimeProperty, SelectedDateTime.GetValueOrDefault().Date + timeSpan.TimeOfDay);
            }
            else
            {
                SetCurrentValue(SelectedDateTimeProperty, null);
                if (SelectedDateTime == null)
                {
                    // if already null, overwrite wrong data in TextBox
                    WriteValueToTextBox();
                }
            }
        }