WPF TextBox 输入限制小数点后两位
Posted mingsonzheng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF TextBox 输入限制小数点后两位相关的知识,希望对你有一定的参考价值。
private 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为空逻辑
以上是关于WPF TextBox 输入限制小数点后两位的主要内容,如果未能解决你的问题,请参考以下文章
WPF TextBox 正则验证 大于等于0 小于等于1 的两位小数