wpf中文本框只能输入整数

 1 private void txtBarCodeNum_KeyUp(object sender, KeyEventArgs e)
 2         {
 3             TxtInt(sender as TextBox);
 4         }
 5 
 6         private void txtBarCodeNum_TextChanged(object sender, TextChangedEventArgs e)
 7         {
 8             TxtInt(sender as TextBox);
 9         }
10 
11         private void TxtInt(TextBox txt)
12         {
13             if (txt==null || string.IsNullOrEmpty(txt.Text))
14             {
15                 txt.Tag = "";
16                 return;
17             }
18             try
19             {
20                 txt.Text = txt.Text.Trim();
21                 txt.Tag = Convert.ToInt32(txt.Text); ;
22             }
23             catch
24             {
25                 if (txt.Tag==null)
26                 {
27                     txt.Text = "";
28                 }
29                 else
30                 {
31                     txt.Text = txt.Tag.ToString();
32                 }
33             }
34             txt.SelectionStart = txt.Text.Length;
35         }
原文地址:https://www.cnblogs.com/DoNetCShap/p/11040714.html