Search code examples
number-formattingavaloniauiavalonia

How to only allow numbers (while typing) in a Numeric UpDown?


I'm trying to come up with a better approach for my other problem: Avalonia: Get Numeric UpDown to parse the text on lost focus
In short: I'd like a TextBox that ignores non-numeric characters while typing. If I am using numeric updowns like this:

<NumericUpDown Value="{Binding Samples}" ParsingNumberStyle="Integer" ShowButtonSpinner="False"/>

The user can type in non-numeric characters, which makes the box reset to it's former value, when I click outside of it. However pressing Return does not work unless the text is a proper integer (see link above). I guess I could use

OnMyValueChanging(int oldValue, int new Value)
{
  //validate
}

However the value is an integer, so I can't verify it while typing. I could use a regular TextBox, but then I'd have to write the OnValueChanging for every occurence of my numeric inputs. Maybe I can use Styles for that? But I don't know how to point to a project-wide function, that can be shared across multiple view models.


Solution

  • @Karan Shishoo thank you for the link,there are a lot of answers some incompatible with Avalonia, but looks like I figured most of it out:

    <NumericUpDown KeyDown="HandleNonNumericInput">
    

    And in the code behind

    private void HandleNonNumericInput(object? sender, KeyEventArgs? e)
    {
        string? letter = e.KeySymbol;
        bool rejectKey;
        if (string.IsNullOrEmpty(letter))
        {
            rejectKey = true;
        }
        else if (e.Key == Key.Enter || e.Key == Key.Return || e.Key == Key.Escape)
        {
            TextBox? tb = e.Source as TextBox;
            TopLevel? tl = TopLevel.GetTopLevel(this);
            tl!.Focus();
            rejectKey = true;
        }
        else
        {
            rejectKey = !char.IsNumber(letter[0]);
        }
        Debug.WriteLine($"Key: {e.Key}, Symbol: <{letter}>, rejected: {rejectKey}");
        e.Handled = rejectKey;char.IsNumber(e.KeySymbol[0]);
    }
    

    I may have forgotten to check for something, if I realize, I'll update. One big problem remains, but that's probably for another question: If at any time the input string is "" there is an exception below the TextBox that doesn't go away.