0
votes

I have a little issue with displaying all validations according to one property (textbox). Now my validation works as follow:

Textbox's borded is red at start and displaying ONLY ONE error message per textbox. When I write something in textbox which has many validators, they appear one after another.

enter image description here

What I want to achieve:

  1. Textbox's border is red at start and displaying ALL error messages below textbox.
  2. when validation is successful, appriopriate validation should disappear.

Example: I use EmpolyeeSalary. When value which I write in textbox is less than 10 and 100 it should show ALL error messages. When I write 10, appriopriate error message should disappear.

Could you help me? Below I placed necessary parts of my model and xaml files.

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

1 Answers

0
votes

Your construct will only allow one error message value to be set.

Your need something like this ...

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