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.
- 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
1 Answer
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
版权声明:本文标题:avaloniaui - How to only allow numbers (while typing) in a Numeric UpDown? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741971686a2407879.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论