WPF验证,正则表达式

安德烈

我在wpf中验证存在问题。我有一条短信textbox

<TextBox x:Name="Login"  Grid.Column="2" Grid.Row="2">
        <TextBox.Text>
            <Binding  Mode="TwoWay" Path="Email"  UpdateSourceTrigger="PropertyChanged">
                <Binding.ValidationRules>
                    <DataErrorValidationRule />
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
        <TextBox.Style>
                <Setter Property="Validation.ErrorTemplate">
                    <Setter.Value>
                        <ControlTemplate>
                            <DockPanel LastChildFill="True">
                                <TextBlock DockPanel.Dock="Bottom" Foreground="Maroon" FontSize="10pt"
                                           Text="{Binding ElementName=Emael, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
                                </TextBlock>
                                <Border BorderBrush="DarkRed" BorderThickness="1">
                                    <AdornedElementPlaceholder Name="Emael" />
                                </Border>
                            </DockPanel>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </TextBox.Style>
    </TextBox>

和属性

 public string Error => throw new NotImplementedException();

    public string this[string columnName]
    {
        get
        {
            string error = String.Empty;

            switch (columnName)
            {
                case "Email": 
                    if (Email == null || Email == string.Empty)
                    {
                        error = "Field login must be required";
                    }
                    break;
            }
            return error;
        }
    }

我不知道如何在这里使用正则表达式,在后端我使用 dataAnnotation

[Required(ErrorMessage = "This field should be filled in")]
[RegularExpression(@"\w+(@)[a-zA-z]+(\.)[a-zA-z]+", ErrorMessage = ("Use the right email format"))]

如何以表格形式编写相同的逻辑(客户端验证)。编写自己的验证类并以某种方式与属性连接可能会更好?有什么更好的办法吗?

毫米8

您可以将注释复制到视图模型中,并实现为您验证它们的方法:

[Required(ErrorMessage = "This field should be filled in")]
[RegularExpression(@"\w+(@)[a-zA-z]+(\.)[a-zA-z]+", ErrorMessage = ("Use the right email format"))]
public string Email { get; set; }

public string Error => throw new NotImplementedException();

public string this[string columnName]
{
    get
    {
        string error = String.Empty;

        switch (columnName)
        {
            case "Email":
                return ValidateModelProperty(Email, columnName);
        }
        return error;
    }
}

private string ValidateModelProperty(object value, string propertyName)
{
    ICollection<ValidationResult> validationResults = new List<ValidationResult>();
    ValidationContext validationContext =
        new ValidationContext(this, null, null) { MemberName = propertyName };
    if (!Validator.TryValidateProperty(value, validationContext, validationResults))
        foreach (ValidationResult validationResult in validationResults)
            return validationResult.ErrorMessage;
    return null;
}

请参阅博客文章以获取更多信息。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章