Exemple #1
0
        /// <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));
            }
        }