Beispiel #1
0
        /// <summary>
        /// TextProperty property changed handler.
        /// </summary>
        /// <param name="d">PersianDatePicker that changed its Text.</param>
        /// <param name="e">DependencyPropertyChangedEventArgs.</param>
        private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PersianDatePicker dp = d as PersianDatePicker;

            Debug.Assert(dp != null);

            if (!dp.IsHandlerSuspended(PersianDatePicker.TextProperty))
            {
                string newValue = e.NewValue as string;

                if (newValue != null)
                {
                    if (dp._textBox != null)
                    {
                        dp._textBox.Text = newValue;
                    }
                    else
                    {
                        dp._defaultText = newValue;
                    }

                    dp.SetSelectedDate();
                }
                else
                {
                    dp.SetValueNoCallback(PersianDatePicker.SelectedDateProperty, null);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// IsDropDownOpenProperty property changed handler.
        /// </summary>
        /// <param name="d">PersianDatePicker that changed its IsDropDownOpen.</param>
        /// <param name="e">DependencyPropertyChangedEventArgs.</param>
        private static void OnIsDropDownOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PersianDatePicker dp = d as PersianDatePicker;

            Debug.Assert(dp != null);

            bool newValue = (bool)e.NewValue;

            if (dp._popUp != null && dp._popUp.IsOpen != newValue)
            {
                dp._popUp.IsOpen = newValue;
                if (newValue)
                {
                    dp._originalSelectedDate = dp.SelectedDate;
                    // When the popup is opened set focus to the DisplayDate button.
                    // Do this asynchronously because the IsDropDownOpen could
                    // have been set even before the template for the PersianDatePicker is
                    // applied. And this would mean that the visuals wouldn't be available yet.

                    dp.Dispatcher.BeginInvoke(DispatcherPriority.Input, (Action) delegate()
                    {
                        // setting the focus to the calendar will focus the correct date.
                        dp._persianCalendar.Focus();
                    });
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// SelectedDateProperty property changed handler.
        /// </summary>
        /// <param name="d">PersianDatePicker that changed its SelectedDate.</param>
        /// <param name="e">DependencyPropertyChangedEventArgs.</param>
        private static void OnSelectedDateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PersianDatePicker dp = d as PersianDatePicker;

            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 PersianCalendar UI itself,
                // so, we shouldn't change the DisplayDate since it will automatically be changed by the PersianCalendar
                if ((day.Month != dp.DisplayDate.Month || day.Year != dp.DisplayDate.Year) && !dp._persianCalendar.DatePickerDisplayDateFlag)
                {
                    dp.DisplayDate = day;
                }

                dp._persianCalendar.DatePickerDisplayDateFlag = false;
            }
            else
            {
                dp.SetWaterMarkText();
            }

            if (addedDate.HasValue)
            {
                addedItems.Add(addedDate.Value);
            }

            if (removedDate.HasValue)
            {
                removedItems.Add(removedDate.Value);
            }

            dp.OnSelectedDateChanged(new CalendarSelectionChangedEventArgs(PersianDatePicker.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);
            }
        }
Beispiel #4
0
        private static object CoerceSelectedDate(DependencyObject d, object value)
        {
            PersianDatePicker dp = d as PersianDatePicker;

            // We set _persianCalendar.SelectedDate in order to get _persianCalendar to compute the coerced value
            dp._persianCalendar.SelectedDate = (DateTime?)value;
            return(dp._persianCalendar.SelectedDate);
        }
Beispiel #5
0
        private static object CoerceDisplayDateStart(DependencyObject d, object value)
        {
            PersianDatePicker dp = d as PersianDatePicker;

            // We set _persianCalendar.DisplayDateStart in order to get _persianCalendar to compute the coerced value
            dp._persianCalendar.DisplayDateStart = (DateTime?)value;
            return(dp._persianCalendar.DisplayDateStart);
        }
Beispiel #6
0
        /// <summary>
        /// DisplayDateEndProperty property changed handler.
        /// </summary>
        /// <param name="d">PersianDatePicker that changed its DisplayDateEnd.</param>
        /// <param name="e">DependencyPropertyChangedEventArgs.</param>
        private static void OnDisplayDateEndChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PersianDatePicker dp = d as PersianDatePicker;

            Debug.Assert(dp != null);

            dp.CoerceValue(DisplayDateProperty);
        }
Beispiel #7
0
        /// <summary>
        /// Called when the IsEnabled property changes.
        /// </summary>
        private static void OnIsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PersianDatePicker dp = d as PersianDatePicker;

            Debug.Assert(dp != null);

            dp.CoerceValue(IsDropDownOpenProperty);

            OnVisualStatePropertyChanged(dp);
        }
Beispiel #8
0
        private static object OnCoerceText(DependencyObject dObject, object baseValue)
        {
            PersianDatePicker dp = (PersianDatePicker)dObject;

            if (dp._shouldCoerceText)
            {
                dp._shouldCoerceText = false;
                return(dp._coercedTextValue);
            }

            return(baseValue);
        }
Beispiel #9
0
        private static object OnCoerceIsDropDownOpen(DependencyObject d, object baseValue)
        {
            PersianDatePicker dp = d as PersianDatePicker;

            Debug.Assert(dp != null);

            if (!dp.IsEnabled)
            {
                return(false);
            }

            return(baseValue);
        }
Beispiel #10
0
        /// <summary>
        ///     Called when this element gets focus.
        /// </summary>
        private static void OnGotFocus(object sender, RoutedEventArgs e)
        {
            // When Datepicker gets focus move it to the TextBox
            PersianDatePicker picker = (PersianDatePicker)sender;

            if ((!e.Handled) && (picker._textBox != null))
            {
                if (e.OriginalSource == picker)
                {
                    picker._textBox.Focus();
                    e.Handled = true;
                }
                else if (e.OriginalSource == picker._textBox)
                {
                    picker._textBox.SelectAll();
                    e.Handled = true;
                }
            }
        }
Beispiel #11
0
        private static void OnVisualStatePropertyChanged(PersianDatePicker dp)
        {
            if (Validation.GetHasError(dp))
            {
                if (dp.IsKeyboardFocused)
                {
                    VisualStateManager.GoToState(dp, VisualStates.StateInvalidFocused, true);
                }
                else
                {
                    VisualStateManager.GoToState(dp, VisualStates.StateInvalidUnfocused, true);
                }
            }
            else
            {
                VisualStateManager.GoToState(dp, VisualStates.StateValid, true);
            }

            // If you remove the following code you will be faced with a crazy component.
            VisualStateManager.GoToState(dp, dp.IsEnabled ? VisualStates.StateNormal : VisualStates.StateDisabled, true);
        }
Beispiel #12
0
        /// <summary>
        /// SelectedDateFormatProperty property changed handler.
        /// </summary>
        /// <param name="d">PersianDatePicker that changed its SelectedDateFormat.</param>
        /// <param name="e">DependencyPropertyChangedEventArgs.</param>
        private static void OnSelectedDateFormatChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PersianDatePicker dp = d as PersianDatePicker;

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