The InvalidEntry event args, occurs when the datepicker can't parse user input string correctly
Inheritance: System.Windows.RoutedEventArgs
Beispiel #1
0
        /// <summary>
        /// Parse the input string, if the input string is a valid date, return the Date, else return null
        /// </summary>
        /// <param name="text">the input string</param>
        /// <remarks>
        /// If the input entry equals NullValueText, Value will be set to null
        /// If the input entry is a valid date, Value will be set to the input date
        /// If the input entry isn't a valid date, InvalidEntry event will be fired, Value will still keep the old value (don't set to null)
        /// </remarks>
        /// <returns>true if the parsing was successful</returns>
        private bool DoParse(string text)
        {
            if (GetFlag(Flags.IsParsing))
            {
                return false;
            }

            DateTime? date = null;
            bool isValidDate = true;
            SetFlag(Flags.IsTextChanged, false);
            SetFlag(Flags.IsParsing, true);

            if (text == NullValueText)
            {
                SetFlag(Flags.IsParsing, false);
            }
            else
            {
                //If user provides DateConverter, use it to parse; if not, use default converter
                object ret = null;
                try
                {
                    CultureInfo cultureInfo = Language != null ? Language.GetSpecificCulture() : null;
                    if (DateConverter != null)
                    {
                        ret = DateConverter.ConvertBack(text, typeof(DateTime), null, cultureInfo);
                    }
                    else
                    {
                        ret = _defaultDateConverter.ConvertBack(text, typeof(DateTime), null, cultureInfo);
                    }
                }
                catch (FormatException)
                {
                    isValidDate = false;
                }
                finally
                {
                    SetFlag(Flags.IsParsing, false);
                }

                if (ret is DateTime)
                {
                    date = new DateTime?((DateTime)ret);
                }
            }

            if (isValidDate)
            {
                //If the input entry is a valid date
                //Note: Since DatePicker use coercion for Value/MaxDate/MinDate
                //Value = date can't change the Value if date exceeds the range of Max/MinDate
                //But in this case, we need to update the EditableTextBox.Text to Value(call UpdateEditableTextBox(DoFormat(Value)))
                DateTime? oldValue = Value;
                Value = date;
                if (oldValue == Value)
                {
                    DoFormat();
                }
            }
            else
            {
                //If the input entry isn't a valid date, fire InvalidEntry event
                SetFlag(Flags.IgnoreUpdateEditableTextBox, true);
                try
                {
                    Value = null;
                }
                finally
                {
                    SetFlag(Flags.IgnoreUpdateEditableTextBox, false);
                }

                if (EditableTextBoxSite != null)
                {
                    if (!EditableTextBoxSite.IsFocused)
                    {
                        EditableTextBoxSite.Focus();
                    }
                    EditableTextBoxSite.SelectAll();
                }

                InvalidEntryEventArgs args = new InvalidEntryEventArgs(InvalidEntryEvent, text);
                OnInvalidEntry(args);
            }
            return isValidDate;
        }
Beispiel #2
0
 /// <summary>
 /// This event is invoked when datepicker can't parse the input string correctly
 /// </summary>
 protected virtual void OnInvalidEntry(InvalidEntryEventArgs e)
 {
     RaiseEvent(e);
 }