使用IDataErrorInfo进行验证时,在每个文本框中显示所有错误消息

时间:2018-10-23 11:26:21

标签: c# wpf validation mvvm idataerrorinfo

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

文本框在开始时呈红色,并且每个文本框仅显示一条错误消息。当我在具有许多验证器的文本框中编写内容时,它们会依次出现。

enter image description here

我想要实现的目标:

  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>

1 个答案:

答案 0 :(得分:0)

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

您需要这样的东西...

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 !";
    }
}