C# 通过WPF文本框上的reg exp进行用户输入验证

C# 通过WPF文本框上的reg exp进行用户输入验证,c#,regex,wpf,validation,user-input,C#,Regex,Wpf,Validation,User Input,我有一个输入验证数组。 数组的每一行表示一个输入验证;正则表达式检查字符串和验证失败时为用户显示的字符串: public class myClass { public static string[][] inputsInfo = new string[4][]; static myClass() { // ID - 9 digits inputsInfo[0] = new string[2] { "^[0-9]{9}$", "exactly 9

我有一个输入验证数组。 数组的每一行表示一个输入验证;正则表达式检查字符串和验证失败时为用户显示的字符串:

public class myClass
{
     public static string[][] inputsInfo = new string[4][];

     static myClass()
     {
     // ID - 9 digits
     inputsInfo[0] = new string[2] { "^[0-9]{9}$", "exactly 9 digits (0-9)" };

     // only letters and possibly more than one word
     inputsInfo[1] = new string[2] { "^[A-Za-z]{2,}(( )[A-Za-z]{2,})*$", "only letters (A-Z) or (a-z)" };

     // Number - unlimited digits
     inputsInfo[2] = new string[2] { "^[0-9]+$", "only digits (0-9)" };

     // username, password
     inputsInfo[3] = new string[2] { "^[A-Za-z0-9]{6,}$", "at least 6 characters.\nOnly letters (A-Z) or (a-z) and digits (0-9) are allowed" };

     }
..............
..............
}
我有包含WPF文本框的窗口。有些字段具有相同的输入验证,这就是为什么我要将所有输入验证保存在数组中,这样我就可以选择目前需要的验证

我有以下表格:

...............

        <TextBlock Grid.Row="2" Grid.Column="0" Text="First name"/>
        <TextBox x:Name="firstName" Grid.Row="2" Grid.Column="1"/>
        <Button Grid.Row="2" Grid.Column="2" Content="Search"/>

        <TextBlock Grid.Row="3" Grid.Column="0" Text="Last name"/>
        <TextBox x:Name="lastName" Grid.Row="3" Grid.Column="1"/>
        <Button Grid.Row="3" Grid.Column="2" Content="Search"/>

        <TextBlock Grid.Row="4" Grid.Column="0" Text="ID number"/>
        <TextBox x:Name="ID" Grid.Row="4" Grid.Column="1"/>
        <Button Grid.Row="4" Grid.Column="2" Content="Search"/>

...............
。。。。。。。。。。。。。。。
...............
每个文本框都有一个带有单击事件的“近”按钮。 如何通过单击按钮执行输入验证

有没有一种通过XAML代码实现的方法? 还是只在c#代码后面的代码中

任何帮助都将不胜感激

如何通过单击按钮执行输入验证

为什么不在ViewModel上创建布尔标志来监视目标textbox的绑定文本属性以进行验证呢。范例

虚拟机:

XAML


有人有解决方案吗?
public string FirstName { get { return _firstName; }
                          set { _firstname = value; 
                                PropertyChanged("IsFirstNameValid");
                                PropertyChanged("FirstName");
                              }
                        }

public bool IsFirstNameValid { get { return Regex.IsMatch( FirstName,
                                                           ValidationPatternFirstName); }}
<TextBox x:Name="firstName" 
         Text={Binding FirstName, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" />