private void txt_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
Regex re = new Regex("[^0-9.-]+");
e.Handled = re.IsMatch(e.Text);
var textBox = e.OriginalSource as TextBox;
if (textBox != null)
{
if (!string.IsNullOrWhiteSpace(textBox.Text))
{
if (textBox.Text.Substring(textBox.Text.Length - 1) != ".")
{
if (!textBox.Text.Contains("."))
{
textBox.MaxLength = 10;
}
if (decimal.TryParse(textBox.Text, out var anyAmount))
{
// Text成功转为deciml后逻辑
}
}
else
{
textBox.MaxLength = textBox.Text.Length + 2;
}
}
else
{
// Text为空逻辑
}
}
}



