我正在尝试在按下某个键时验证文本框中的文本。这是显示我正在尝试做的最短代码示例:
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBox FontSize="15" HorizontalAlignment="Left" Name="txtEmail" VerticalAlignment="Top" Width="135"
                 Text="{Binding ValidationRules.EmailAddress, ValidatesOnExceptions=True, UpdateSourceTrigger=PropertyChanged}"/>
    </Grid>
</Window>
“验证规则”类:
 class ValidationRules
    {
        string email = "";
        public string EmailAddress
        {
            get
            {
                return email;
            }
            set
            {
                Console.WriteLine("Setting!");
                //Only check if there is any text for now...
                if (String.IsNullOrWhiteSpace(value))
                {
                    throw new Exception();
                }
                email = value;
            }
        }
    }
当我开始在文本框中输入时,即使我使用的是UpdateSourceTrigger=PropertyChanged. 我已经完成了我的研究,但我能找到的所有例子都是漫长而令人困惑的。如果您能指出我在验证中遇到的任何其他错误,我也将不胜感激,但如果可能,请尝试用简单的术语来解释,因为我是 WPF 新手。