C# 如何使用文本框UWP输入货币
我有一个限定为小数的文本框?这是类的一部分:C# 如何使用文本框UWP输入货币,c#,uwp,textbox,C#,Uwp,Textbox,我有一个限定为小数的文本框?这是类的一部分: <TextBox PlaceholderText="Fee" Text="{x:Bind ClassObject.Fee, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> 但是,这仍然允许我输入字母字符,并且不会更新类中的十进制值 我应该如何处理十进制的输入?是否粘贴到文本框中?不确定这是否是您要查找的内容,但此代码只允许数字/数字,并防止粘贴到文本框中 XAML: 这就是如何在
<TextBox PlaceholderText="Fee" Text="{x:Bind ClassObject.Fee, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
但是,这仍然允许我输入字母字符,并且不会更新类中的十进制值
我应该如何处理十进制的输入?是否粘贴到文本框中?不确定这是否是您要查找的内容,但此代码只允许数字/数字,并防止粘贴到文本框中 XAML:
这就是如何在WPF中实现它。我希望在UWP中也是如此。
<TextBox PreviewTextInput="OnlyAllowNumbers" CommandManager.PreviewExecuted="PreventPasteIntoTextbox"
</TextBox>
private void OnlyAllowNumbers(object sender, TextCompositionEventArgs e)
{
Regex regex = new Regex("[^0-9]+");
e.Handled = regex.IsMatch(e.Text);
regex = null; //optional
GC.Collect(); //optional
}
private void PreventPasteIntoTextbox(object sender, ExecutedRoutedEventArgs e)
{
if (e.Command == ApplicationCommands.Copy ||
e.Command == ApplicationCommands.Cut ||
e.Command == ApplicationCommands.Paste)
{
e.Handled = true;
}
}