Пример #1
0
 /// <summary>
 /// This event is invoked when datepicker can't parse the input string correctly
 /// </summary>
 protected virtual void OnInvalidEntry(InvalidEntryEventArgs e)
 {
     RaiseEvent(e);
 }
Пример #2
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>
        private void DoParse(string text)
        {
            if (GetFlag(Flags.IsParsing))
            {
                return;
            }

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