在验证期间使用 IDataErrorInfo 在每个文本框中显示所有错误消息

Display all error messages at each textbox during validation using IDataErrorInfo

我在根据一个 属性(文本框)显示所有验证时遇到一点问题。现在我的验证工作如下:

文本框的边框在开始时是红色的,每个文本框只显示一条错误消息。当我在有很多验证器的文本框中写东西时,它们一个接一个地出现。

我想达到的目标:

  1. 文本框的边框在开始时为红色,并在文本框下方显示所有错误消息。
  2. 验证成功后,适当的验证应该消失。

示例: 我使用 EmpolyeeSalary。当我在文本框中写入的值小于 10 和 100 时,它应该显示所有错误消息。当我写 10 时,相应的错误消息应该会消失。

你能帮帮我吗?下面我放置了模型的必要部分和 xaml 文件。

Employee.cs

public string Error
    {
        get
        {
            return error;
        }
    }
    public string this[string columnName]
    {
        get
        {
            int output;
            error = string.Empty;

        if (columnName == "DynamicSearchEmployeeName" && string.IsNullOrWhiteSpace(DynamicSearchEmployeeName))
        {
            error = "Employee Name is required to add a new Employee !";
        }
        else if (columnName == "DynamicSearchEmployeeSalary" && string.IsNullOrWhiteSpace(DynamicSearchEmployeeSalary))
        {
            error = "Employee Salary is required to add a new Employee !";
        }
        else if (columnName == "DynamicSearchEmployeeSalary" && !Int32.TryParse(dynamicSearchEmployeeSalary, out output))
        {
            error = "Employee Salary has to be number !";
        }
        else if (columnName == "DynamicSearchEmployeeSalary" && EmployeeSalary < 10)
        {
            error = "Employee Salary cannot be less than 10 !";
        }
        else if (columnName == "DynamicSearchEmployeeSalary" && EmployeeSalary < 100)
        {
            error = "Employee Salary cannot be less than 100 !";
        }
        else if (columnName == "DynamicSearchEmployeeDesigner" && string.IsNullOrWhiteSpace(DynamicSearchEmployeeDesigner))
        {
            error = "Employee Designer is required to add a new Employee !";
        }

        return error;
    }
}

MainWindow.xaml

 <ControlTemplate x:Key="ErrorToolTipTemplate_1">
            <ControlTemplate.Resources>
                <Style x:Key="textblockErrorTooltip" TargetType="TextBlock">
                    <Setter Property="HorizontalAlignment" Value="Center" />
                    <Setter Property="VerticalAlignment" Value="Center" />
                    <Setter Property="FontWeight" Value="Bold" />
                    <Setter Property="Foreground" Value="White" />
                    <Setter Property="Margin" Value="0 0 0 0" />
                </Style>
            </ControlTemplate.Resources>
            <DockPanel LastChildFill="true">
                <Border Height="Auto"
   Margin="0,0,0,0"
   Background="#DC000C"
   CornerRadius="0"
   DockPanel.Dock="Bottom">
                    <TextBlock Style="{StaticResource textblockErrorTooltip}" 
Text="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" />
                </Border>
                <AdornedElementPlaceholder Name="customAdorner">
                    <Border BorderBrush="#DC000C" BorderThickness="1.3" />
                </AdornedElementPlaceholder>
            </DockPanel>
        </ControlTemplate>

        <Style TargetType="TextBox">
            <Setter Property="HorizontalAlignment" Value="Right" />
            <Setter Property="VerticalAlignment" Value="Top" />
            <Setter Property="Width" Value="150" />
            <Setter Property="Height" Value="30" />
            <Setter Property="Validation.ErrorTemplate" 
                 Value="{DynamicResource ErrorToolTipTemplate_1}" />
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="ToolTip" 
Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
                </Trigger>
            </Style.Triggers>
        </Style>

您的构造将只允许设置一个错误消息值。

您需要这样的东西...

if (columnName == "DynamicSearchEmployeeSalary")
{ 
    if (string.IsNullOrWhiteSpace(DynamicSearchEmployeeSalary))
    {
        error += "Employee Salary is required to add a new Employee !";
    }

    if (!Int32.TryParse(dynamicSearchEmployeeSalary, out output))
    {
        error += "\r\nEmployee Salary has to be number !";
    }

    if (EmployeeSalary < 10)
    {
        error += "\r\nEmployee Salary cannot be less than 10 !";
    }

    if (EmployeeSalary < 100)
    {
        error += "\r\nEmployee Salary cannot be less than 100 !";
    }
}