xceedsoftware / wpftoolkit

All the controls missing in WPF. Over 1 million downloads.
Other
3.89k stars 877 forks source link

IntegerUpDown discards input when it includes FormatString formatting #965

Open xceedsoftware opened 7 years ago

xceedsoftware commented 7 years ago

cmbcodeplex[CodePlex]
Precondition: The IntegerUpDown has a FormatString such as quot{}0%quot or quot{}0°quot.

Repro: In the input box select only the numeric part, such as quot10quot when quot10%quot is shown, then enter a new value. The new value is discarded.

I would have liked to workaround this temporarily by handling TextChanged or InputValidationError but this was not possible due to #22017. Instead I had to review IntegerUpDown's implementation and override ConvertTextToValue(). Here is my implementation for reference: public class IntegerUpDown : Xceed.Wpf.Toolkit.IntegerUpDown { protected string suffix = quotquot;

public string Suffix
{
    get { return suffix; }
    set
    {
        suffix = value ?? quotquot;
        FormatString = suffix == null ? quotquot : (quot0quot + suffix);
    }
}

protected override int? ConvertTextToValue (string text)
{
    if (string.IsNullOrEmpty(text))
    {
        return null;
    }

    string currentValueText = ConvertValueToText();
    if (object.Equals(currentValueText, text))
    {
        return Value;
    }

    // skip user supplied suffix
    if (!string.IsNullOrEmpty(Suffix) ampamp text.EndsWith(Suffix))
    {
        text = text.Slice(-Suffix.Length);
    }

    // if FormatException then caller will keep both text and original 
    // value and signal validation error
    //
    int newValue = int.Parse(text, ParsingNumberStyle, CultureInfo);

    int maximum = Maximum.HasValue ? Maximum.Value : int.MaxValue;
    int minimum = Minimum.HasValue ? Minimum.Value : int.MinValue;
    if (ClipValueToMinMax)
    {
        return MathUtil.Clamp(newValue, minimum, maximum);
    }

    if (!object.Equals(newValue, DefaultValue) ampamp 
        newValue != MathUtil.Clamp(newValue, minimum, maximum))
    {
        throw new ArgumentOutOfRangeException(quotValuequot, 
            quotValue must be between {} and {}quot.With(minimum, maximum));
    }
    return newValue;
}

}

xceedsoftware commented 7 years ago

BoucherS[CodePlex]
Hi,

This will be fixed in v3.0. Modifying the numeric part of a NumericUpDown, with a FormatString containing non-numeric characters, won't discard the changes anymore.

ShannonZ commented 5 years ago

It's v3.5 now. But the issue still not be fixed. @xceedsoftware

XceedBoucherS commented 5 years ago

Hi, I've try v3.5. with the preceding steps, which are :

"Precondition: The IntegerUpDown has a FormatString such as quot{}0%quot or quot{}0°quot.

Repro: In the input box select only the numeric part, such as quot10quot when quot10%quot is shown, then enter a new value. The new value is discarded."

This do not happens anymore. Please specify. Thank you.


Get more controls, features, updates and technical support with Xceed Toolkit Plus for WPF