WPF TextBox 输入限制小数点后两位
2021-05-30 07:18
标签:tar send amount code parse else ima 小数点 private WPF TextBox 输入限制小数点后两位 标签:tar send amount code parse else ima 小数点 private 原文地址:https://www.cnblogs.com/MingsonZheng/p/11059701.htmlprivate void TextBox_OnPreviewKeyUp(object sender, KeyEventArgs e)
{
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为空逻辑
}
}
}