admin管理员组

文章数量:1315101

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.

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.

Share Improve this question edited Jan 30 at 10:49 DarkBee 15.6k8 gold badges72 silver badges117 bronze badges asked Jan 30 at 10:47 Frederik SteinmetzFrederik Steinmetz 2312 silver badges8 bronze badges 1
  • You can do this in XAML itself, given that avalonia allows you setup code behind xaml files, this answer should help you: stackoverflow/questions/1268552/… – Karan Shishoo Commented Jan 30 at 11:01
Add a comment  | 

1 Answer 1

Reset to default 0

@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 fotten 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.

本文标签: avaloniauiHow to only allow numbers (while typing) in a Numeric UpDownStack Overflow