WPF控件TEXTBOX增加输入限制,并且单个界面只需设置一次

 1 <Grid x:Name="Grid_Form">
 2                     <Grid.ColumnDefinitions>
 3                         <ColumnDefinition Width="0.2*"/>
 4                         <ColumnDefinition Width="0.7*"/>
 5                         <ColumnDefinition Width="0.1*"/>
 6 
 7                     </Grid.ColumnDefinitions>
 8                     <Grid.RowDefinitions>
 9                         <RowDefinition/>
10                         <RowDefinition/>
11                         <RowDefinition/>
12                         <RowDefinition/>
13                         <RowDefinition/>
14                         <RowDefinition/>
15                        
16 
17 
18                     </Grid.RowDefinitions>
19                     <TextBlock Grid.Row="0" Grid.Column="0" Style="{StaticResource TextBlock_Grid}" Text="{Binding LanguagePack.UM_UserNoLabel,FallbackValue=用户编号:}" />
20                     <TextBox Name="UserNoText" Style="{StaticResource textbox_Defalut}"  Grid.Row="0" Grid.Column="1" Width="390" Height="35"    Text="{Binding Model.UserCode}"
21                          Py:ControlProperty.IsRequired="True" 
22                          Py:ControlProperty.ErrorMsg="用户编号为必填项!" MaxLength="50"/>
23                     <Label Margin="0,9,0,0" Content="*" FontSize="30" Foreground="Red" Grid.Row="0" Grid.Column="2" HorizontalAlignment="Left" VerticalAlignment="Center" VerticalContentAlignment="Center"/>
24 
25                     <TextBlock Grid.Row="1" Grid.Column="0" Style="{StaticResource TextBlock_Grid}" Text="{Binding LanguagePack.UM_UserNameLabel,FallbackValue=用户名称:}" />
26                     <TextBox Name="UserNameText" Style="{StaticResource textbox_Defalut}" Grid.Row="1" Grid.Column="1" Width="390"   Text="{Binding Model.UserName}"
27                              Py:ControlProperty.IsRequired="True"
28                          Py:ControlProperty.ErrorMsg="用户名称为必填项!" MaxLength="50"/>
29                     <Label Margin="0,9,0,0" Content="*" FontSize="30" Foreground="Red" Grid.Row="1" Grid.Column="2" HorizontalAlignment="Left" VerticalAlignment="Center" VerticalContentAlignment="Center"/>
30 
31                     <TextBlock Grid.Row="2" Grid.Column="0" Style="{StaticResource TextBlock_Grid}" Text="{Binding LanguagePack.UM_UserRoleLabel,FallbackValue=职      务:}"/>
32                     <ComboBox Name="RoleComBox" Style="{StaticResource ComboBox_Grid}" Grid.Row="2" Grid.Column="1" Width="390" VerticalAlignment="Center" HorizontalAlignment="Center"  SelectedValue="{Binding Model.Role}"/>
33                     <Label Margin="0,9,0,0" Content="*" FontSize="30" Foreground="Red" Grid.Row="2" Grid.Column="2" HorizontalAlignment="Left" VerticalAlignment="Center" VerticalContentAlignment="Center"/>
34 
35                     <TextBlock Grid.Row="3" Grid.Column="0" Style="{StaticResource TextBlock_Grid}" Text="{Binding LanguagePack.UM_UserCardIdLabel,FallbackValue=身份证号:}"/>
36                     <TextBox Name="CIDText" Style="{StaticResource textbox_Defalut}"  Grid.Row="3" Grid.Column="1" Width="390" Margin="0,10,0,9"  Text="{Binding Model.CardNo}"  MaxLength="28"
37                              Py:ControlProperty.TB_Type="Int"/>
38                     <!--<Label Margin="0,9,0,0" Content="*" FontSize="30" Foreground="Red" Grid.Row="2" Grid.Column="2" HorizontalAlignment="Left" VerticalAlignment="Center" VerticalContentAlignment="Center"/>-->
39 
40                     <TextBlock Grid.Row="4" Grid.Column="0" Style="{StaticResource TextBlock_Grid}" Text="{Binding LanguagePack.UM_UserTelLabel,FallbackValue=联系电话:}" />
41                     <TextBox Name="TelText" Style="{StaticResource textbox_Defalut}" Grid.Row="4" Grid.Column="1" Width="390" Margin="0,10,0,9"   Text="{Binding Model.TEL}"/>
42 
43                     <TextBlock Grid.Row="5" Grid.Column="0" Style="{StaticResource TextBlock_Grid}" Text="{Binding LanguagePack.UM_UserEmileLabel,FallbackValue=邮箱地址:}" />
44                     <TextBox Name="EmText" Style="{StaticResource textbox_Defalut}" Grid.Row="5" Grid.Column="1" Width="390" Margin="0,10,0,9"   Text="{Binding Model.Email}"/>
45 
46 
47                 </Grid>

后台代码

 1 this.Grid_Form.AddHandler(TextBox.PreviewKeyDownEvent, new KeyEventHandler(CommonUI.TextBoxKeyDown));
 2 
 3 
 4 
 5         /// <summary>
 6         /// 文件框输入限制
 7         /// </summary>
 8         /// <param name="sender"></param>
 9         /// <param name="e"></param>
10         public static void TextBoxKeyDown(object sender, KeyEventArgs e)
11         {
12             TextBox txt = (TextBox)e.OriginalSource;
13             TextBoxType textBoxType = ControlProperty.GetTB_Type(txt);
14             if (textBoxType != TextBoxType.String)
15             {
16                 switch (textBoxType)
17                 {
18                     case TextBoxType.Double:
19                         if (e.Key == Key.OemPeriod || e.Key == Key.Decimal)
20                         {
21                             if (txt.Text.Contains("."))
22                             {
23                                 e.Handled = true;//不可输入
24                             }
25                         }
26                         else if (!((e.Key >= Key.D0 && e.Key <= Key.D9)             //大键盘0-9
27                || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9)   //小键盘0-9 
28                || e.Key == Key.Delete || e.Key == Key.Enter
29                || e.Key == Key.Back || e.Key == Key.Tab
30                || e.Key == Key.Right || e.Key == Key.Left))
31                         {
32                             e.Handled = true;//不可输入
33                         }
34                         break;
35                     case TextBoxType.Int:
36                         if (!((e.Key >= Key.D0 && e.Key <= Key.D9)             //大键盘0-9
37                || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9)   //小键盘0-9 
38                || e.Key == Key.Delete || e.Key == Key.Enter
39                || e.Key == Key.Back || e.Key == Key.Tab
40                || e.Key == Key.Right || e.Key == Key.Left))
41                         {
42                             e.Handled = true;//不可输入
43                         }
44                         break;
45                 }
46             }
47 
48         }
"Grid_Form"为前端grid定义的Name,"TextBox.PreviewKeyDownEvent"要添加的事件,"new KeyEventHandler"不同事件,NEW不同的事件处理。
本文参考自https://www.cnblogs.com/YYkun/p/6871736.html

上一篇:在网页添加时间(时钟方法)


下一篇:JS 中判断空值 undefined 和 null