C# 如何动态更正文本框';一旦失去焦点,输入验证将立即停止?

C# 如何动态更正文本框';一旦失去焦点,输入验证将立即停止?,c#,regex,wpf,windows-phone-8,textbox,C#,Regex,Wpf,Windows Phone 8,Textbox,我对我的if语句感到困惑,它说如果textbox为null、有空格、空字符串、包含特定字符或有正则表达式字符,它将验证为正确。但相反,当我在失去焦点之前输入文本时,它验证为不正确,正如if语句所要求的那样。我做错了什么?我注意到添加if(!(…)会使它以相反的方式工作,但这不是正确的逻辑,我感到困惑 // firstNameTB Textbox to dynamically check validation private void firstNameTB_LostFocus(o

我对我的if语句感到困惑,它说如果textbox为null、有空格、空字符串、包含特定字符或有正则表达式字符,它将验证为正确。但相反,当我在失去焦点之前输入文本时,它验证为不正确,正如if语句所要求的那样。我做错了什么?我注意到添加if(!(…)会使它以相反的方式工作,但这不是正确的逻辑,我感到困惑

    // firstNameTB Textbox to dynamically check validation
    private void firstNameTB_LostFocus(object sender, RoutedEventArgs e)
    {
        if (string.IsNullOrWhiteSpace(firstNameTB.Text) || firstNameTB.Text == "" || firstNameTB.Text.Contains("") || !Regex.IsMatch(firstNameTB.Text, @"^[A-Z]{1}[a-z]+$"))
        {
            firstNameTBL.Text = "First Name: *";
            firstNameTBL.Foreground = new SolidColorBrush(Colors.Red);
            firstNameTBL.FontWeight = FontWeights.Bold;
        }
        else
        {
            // set back to default layout
            this.firstNameTBL.ClearValue(TextBlock.ForegroundProperty);
            this.firstNameTBL.ClearValue(TextBlock.FontWeightProperty);
            this.firstNameTBL.Text = "First Name:";
        }
    }

    // lastNameTB Textbox to dynamically check validation
    private void lastNameTB_LostFocus(object sender, RoutedEventArgs e)
    {
        if (string.IsNullOrWhiteSpace(lastNameTB.Text) || lastNameTB.Text == "" || lastNameTB.Text.Contains("") || !Regex.IsMatch(lastNameTB.Text, @"^[A-Z]{1}[a-z]+$"))
        {
            lastNameTBL.Text = "Last Name: *";
            lastNameTBL.Foreground = new SolidColorBrush(Colors.Red);
            lastNameTBL.FontWeight = FontWeights.Bold;
        }
        else
        {
            // set back to default layout
            this.lastNameTBL.ClearValue(TextBlock.ForegroundProperty);
            this.lastNameTBL.ClearValue(TextBlock.FontWeightProperty);
            this.lastNameTBL.Text = "Last Name:";
        }
    }

    // emailAddressTB Textbox to dynamically check validation
    private void emailAddressTB_LostFocus(object sender, RoutedEventArgs e)
    {
        if (string.IsNullOrWhiteSpace(emailAddressTB.Text) || emailAddressTB.Text == "" || !(emailAddressTB.Text.Contains("@") && emailAddressTB.Text.Contains(".")))
        {
            emailAddressTBL.Text = "Email Address: *";
            emailAddressTBL.Foreground = new SolidColorBrush(Colors.Red);
            emailAddressTBL.FontWeight = FontWeights.Bold;
        }
        else
        {
            // set back to default layout
            this.emailAddressTBL.ClearValue(TextBlock.ForegroundProperty);
            this.emailAddressTBL.ClearValue(TextBlock.FontWeightProperty);
            this.emailAddressTBL.Text = "Email Address:";
        }
    }

问题在于检查文本框是否包含空字符串,例如:

... || lastNameTB.Text.Contains("") || ...
... || firstNameTB.Text.Contains("") || ...
因为它们的计算结果总是
true
-您得到了所描述的行为。

要解决此问题,只需从if语句中删除这些条件。

问题在于检查文本框是否包含空字符串,例如:

... || lastNameTB.Text.Contains("") || ...
... || firstNameTB.Text.Contains("") || ...
因为它们的计算结果总是
true
-您得到了所描述的行为。

要解决此问题,只需从if语句中删除这些条件。

您可以使用
String.Empty
检查字符串是否为空:

if (string.IsNullOrWhiteSpace(lastNameTB.Text) ||
    lastNameTB.Text == String.Empty || 
    !Regex.IsMatch(lastNameTB.Text, @"^[A-Z]{1}[a-z]+$"))
{

}

您可以使用
String.Empty
检查字符串是否为空:

if (string.IsNullOrWhiteSpace(lastNameTB.Text) ||
    lastNameTB.Text == String.Empty || 
    !Regex.IsMatch(lastNameTB.Text, @"^[A-Z]{1}[a-z]+$"))
{

}