/// <summary> /// Raises the <see cref="E:Parsing"/> event, to allow easily hooking /// into the parse logic. /// </summary> /// <param name="e">The <see cref="System.Windows.Controls.UpDownParsingEventArgs<T>"/> /// instance containing the event data.</param> protected virtual void OnParsing(UpDownParsingEventArgs <T> e) { EventHandler <UpDownParsingEventArgs <T> > handler = Parsing; if (handler != null) { handler(this, e); } }
protected internal virtual void ApplyValue(string text) { UpDownParsingEventArgs <T> parsingArgs = new UpDownParsingEventArgs <T>(text); Exception ParsingException = null; T regularParsedValue = default(T); try { // parse value might throw an exception. regularParsedValue = ParseValue(text); parsingArgs.Value = regularParsedValue; } catch (Exception error) { ParsingException = error; } try { OnParsing(parsingArgs); } catch (Exception) { // swallow any exception during custom parsing. } if (ParsingException == null) { // if no error, regularParsedValue must be initialized. T newValue = parsingArgs.Handled ? parsingArgs.Value : regularParsedValue; if (Value.Equals(newValue)) { // although value might be the same, still format the value. SetTextBoxText(); } Value = newValue; } else { if (parsingArgs.Handled) { if (Value.Equals(parsingArgs.Value)) { // although value might be the same, still format the value. SetTextBoxText(); } Value = parsingArgs.Value; } else { UpDownParseErrorEventArgs args = new UpDownParseErrorEventArgs(text, ParsingException); OnParseError(args); if (!args.Handled) { // default error handling is to discard user input and revert to the old text SetTextBoxText(); } } } }