/// <summary> /// SelectedDateFormatProperty property changed handler. /// </summary> /// <param name="d">DatePicker that changed its SelectedDateFormat.</param> /// <param name="e">DependencyPropertyChangedEventArgs.</param> private static void OnSelectedDateFormatChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { DatePicker datePicker = d as DatePicker; Debug.Assert(datePicker != null); if (IsValidSelectedDateFormat((DatePickerFormat)e.NewValue)) { if (datePicker._textBox != null) { //Update DatePickerTextBox.Text if (string.IsNullOrEmpty(datePicker._textBox.Text)) { datePicker.SetWaterMarkText(); } else { DateTime?date = datePicker.ParseText(datePicker._textBox.Text); if (date != null) { string s = datePicker.DateTimeToString((DateTime)date); datePicker.Text = s; } } } } else { throw new ArgumentOutOfRangeException("d", Resource.DatePicker_OnSelectedDateFormatChanged_InvalidValue); } }
/// <summary> /// SelectedDateProperty property changed handler. /// </summary> /// <param name="d">DatePicker that changed its SelectedDate.</param> /// <param name="e">DependencyPropertyChangedEventArgs.</param> private static void OnSelectedDateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { DatePicker dp = d as DatePicker; Debug.Assert(dp != null); Collection <DateTime> addedItems = new Collection <DateTime>(); Collection <DateTime> removedItems = new Collection <DateTime>(); DateTime?addedDate; DateTime?removedDate; dp.CoerceValue(DisplayDateStartProperty); dp.CoerceValue(DisplayDateEndProperty); dp.CoerceValue(DisplayDateProperty); addedDate = (DateTime?)e.NewValue; removedDate = (DateTime?)e.OldValue; if (dp.SelectedDate.HasValue) { DateTime day = dp.SelectedDate.Value; dp.SetTextInternal(dp.DateTimeToString(day)); // When DatePickerDisplayDateFlag is TRUE, the SelectedDate change is coming from the Calendar UI itself, // so, we shouldn't change the DisplayDate since it will automatically be changed by the Calendar if ((day.Month != dp.DisplayDate.Month || day.Year != dp.DisplayDate.Year) && !dp._calendar.DatePickerDisplayDateFlag) { dp.SetCurrentValueInternal(DisplayDateProperty, day); } dp._calendar.DatePickerDisplayDateFlag = false; } else { dp.SetWaterMarkText(); } if (addedDate.HasValue) { addedItems.Add(addedDate.Value); } if (removedDate.HasValue) { removedItems.Add(removedDate.Value); } dp.OnSelectedDateChanged(new CalendarSelectionChangedEventArgs(DatePicker.SelectedDateChangedEvent, removedItems, addedItems)); DatePickerAutomationPeer peer = UIElementAutomationPeer.FromElement(dp) as DatePickerAutomationPeer; // Raise the propetyChangeEvent for Value if Automation Peer exist if (peer != null) { string addedDateString = addedDate.HasValue ? dp.DateTimeToString(addedDate.Value) : ""; string removedDateString = removedDate.HasValue ? dp.DateTimeToString(removedDate.Value) : ""; peer.RaiseValuePropertyChangedEvent(removedDateString, addedDateString); } }
/// <summary> /// SelectedDateProperty property changed handler. /// </summary> /// <param name="d">DatePicker that changed its SelectedDate.</param> /// <param name="e">DependencyPropertyChangedEventArgs.</param> private static void OnSelectedDateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { Collection <DateTime> addedItems = new Collection <DateTime>(); Collection <DateTime> removedItems = new Collection <DateTime>(); DateTime? addedDate; DateTime? removedDate; DatePicker datePicker = d as DatePicker; Debug.Assert(datePicker != null); addedDate = (DateTime?)e.NewValue; removedDate = (DateTime?)e.OldValue; if (addedDate.HasValue) { addedItems.Add(addedDate.Value); } if (removedDate.HasValue) { removedItems.Add(removedDate.Value); } if (addedDate != datePicker._calendar.SelectedDate) { datePicker._calendar.SelectedDate = addedDate; } if (datePicker.SelectedDate != null) { DateTime day = (DateTime)datePicker.SelectedDate; //When the SelectedDateProperty change is done from OnTextPropertyChanged method, two-way binding breaks //if BeginInvoke is not used: datePicker.Dispatcher.BeginInvoke(delegate { datePicker._settingSelectedDate = true; datePicker.Text = datePicker.DateTimeToString(day); datePicker._settingSelectedDate = false; datePicker.OnDateSelected(new SelectionChangedEventArgs(removedItems, addedItems)); }); //When DatePickerDisplayDateFlag is TRUE, the SelectedDate change is coming from the Calendar UI itself, //so, we shouldn't change the DisplayDate since it will automatically be changed by the Calendar if ((day.Month != datePicker.DisplayDate.Month || day.Year != datePicker.DisplayDate.Year) && !datePicker._calendar.DatePickerDisplayDateFlag) { datePicker.DisplayDate = day; } datePicker._calendar.DatePickerDisplayDateFlag = false; } else { datePicker._settingSelectedDate = true; datePicker.SetWaterMarkText(); datePicker._settingSelectedDate = false; datePicker.OnDateSelected(new SelectionChangedEventArgs(removedItems, addedItems)); } }
/// <summary> /// SelectedDateFormatProperty property changed handler. /// </summary> /// <param name="d">DatePicker that changed its SelectedDateFormat.</param> /// <param name="e">DependencyPropertyChangedEventArgs.</param> private static void OnSelectedDateFormatChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { DatePicker dp = d as DatePicker; Debug.Assert(dp != null); if (dp._textBox != null) { // Update DatePickerTextBox.Text if (string.IsNullOrEmpty(dp._textBox.Text)) { dp.SetWaterMarkText(); } else { DateTime?date = dp.ParseText(dp._textBox.Text); if (date != null) { dp.SetTextInternal(dp.DateTimeToString((DateTime)date)); } } } }