I am creating a simple application in C# MVVM and I need your help (advice). I do not know it is a possible to do but you may be know how to do it. So, to validate my textboxes I use IDataErrorInfo interface. It works pretty cool but I have one question. Is there any possibility to place error messages in a list to display in a listview? I want to avoid "\\r\\n" in my error messages. Thank you in advance :)
Below is my code:
Empoloyee.cs
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 !";
}
if (columnName == "DynamicSearchEmployeeSalary" && SelectedEmployee == null)
{
if (string.IsNullOrWhiteSpace(DynamicSearchEmployeeSalary))
{
error = "Employee Salary is required to add a new Employee !\r\n";
}
if (!Int32.TryParse(dynamicSearchEmployeeSalary, out output))
{
error += "Employee Salary has to be number !\r\nEmployee Salary cannot be less than 5 !\r\nEmployee Salary cannot be less than 10 !\r\nEmployee Salary cannot be less than 100 !";
}
if (Int32.TryParse(dynamicSearchEmployeeSalary, out output))
{
if (string.IsNullOrWhiteSpace(DynamicSearchEmployeeSalary) || EmployeeSalary < 5)
{
error += "Employee Salary cannot be less than 5 !\r\n";
}
if (string.IsNullOrWhiteSpace(DynamicSearchEmployeeSalary) || EmployeeSalary < 10)
{
error += "Employee Salary cannot be less than 10 !\r\n";
}
if (string.IsNullOrWhiteSpace(DynamicSearchEmployeeSalary) || EmployeeSalary < 100)
{
error += "Employee Salary cannot be less than 100 !";
}
}
}
if (columnName == "DynamicSearchEmployeeSalary" && SelectedEmployee != null)
{
if (string.IsNullOrWhiteSpace(DynamicSearchEmployeeSalary))
{
error = "Employee Salary is required to add a new Employee !\r\n";
}
if (!Int32.TryParse(DynamicSearchEmployeeSalary, out output))
{
error += "Employee Salary has to be number !\r\nEmployee Salary cannot be less than 5 !\r\nEmployee Salary cannot be less than 10 !\r\nEmployee Salary cannot be less than 100 !";
}
if (Int32.TryParse(DynamicSearchEmployeeSalary, out output))
{
if (string.IsNullOrWhiteSpace(DynamicSearchEmployeeSalary) || Convert.ToInt32(DynamicSearchEmployeeSalary) < 5)
{
error += "Employee Salary cannot be less than 5 !\r\n";
}
if (string.IsNullOrWhiteSpace(DynamicSearchEmployeeSalary) || Convert.ToInt32(DynamicSearchEmployeeSalary) < 10)
{
error += "Employee Salary cannot be less than 10 !\r\n";
}
if (string.IsNullOrWhiteSpace(DynamicSearchEmployeeSalary) || Convert.ToInt32(DynamicSearchEmployeeSalary) < 100)
{
error += "Employee Salary cannot be less than 100 !";
}
}
}
if (columnName == "DynamicSearchEmployeeDesigner" && string.IsNullOrWhiteSpace(DynamicSearchEmployeeDesigner))
{
error = "Employee Designer is required to add a new Employee !";
}
return error;
}
}
MainWindow.xaml
<Window.Resources>
<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>
</Window.Resources>
Thank to Blacktempel advice I created following solution. I hope it is good.
public string Error
{
get { return error; }
}
public string this[string columnName]
{
get
{
int output;
List <string> validateErrorList = new List<string>();
error = string.Empty;
if (columnName == "DynamicSearchEmployeeName")
{
if (string.IsNullOrWhiteSpace(DynamicSearchEmployeeName))
{
validateErrorList.Add("Employee Name is required to add a new Employee !");
}
if ((!Regex.IsMatch(DynamicSearchEmployeeName, @"^[a-zA-Z]+$")))
{
validateErrorList.Add("Employee Name has to contain only a-z, A-Z letters!");
}
}
if (columnName == "DynamicSearchEmployeeSalary" && SelectedEmployee == null)
{
if (string.IsNullOrWhiteSpace(DynamicSearchEmployeeSalary))
{
validateErrorList.Add("Employee Salary is required to add a new Employee !");
}
if (!Int32.TryParse(dynamicSearchEmployeeSalary, out output))
{
validateErrorList.Add("Employee Salary has to be number !");
validateErrorList.Add("Employee Salary cannot be less than 5 !");
validateErrorList.Add("Employee Salary cannot be less than 10 !");
validateErrorList.Add("Employee Salary cannot be less than 100 !");
}
if (Int32.TryParse(dynamicSearchEmployeeSalary, out output))
{
if (string.IsNullOrWhiteSpace(DynamicSearchEmployeeSalary) || EmployeeSalary < 5)
{
validateErrorList.Add("Employee Salary cannot be less than 5 !");
}
if (string.IsNullOrWhiteSpace(DynamicSearchEmployeeSalary) || EmployeeSalary < 10)
{
validateErrorList.Add("Employee Salary cannot be less than 10 !");
}
if (string.IsNullOrWhiteSpace(DynamicSearchEmployeeSalary) || EmployeeSalary < 100)
{
validateErrorList.Add("Employee Salary cannot be less than 100 !");
}
}
}
if (columnName == "DynamicSearchEmployeeSalary" && SelectedEmployee != null)
{
if (string.IsNullOrWhiteSpace(DynamicSearchEmployeeSalary))
{
validateErrorList.Add("Employee Salary is required to add a new Employee !");
}
if (!Int32.TryParse(DynamicSearchEmployeeSalary, out output))
{
validateErrorList.Add("Employee Salary has to be number !");
validateErrorList.Add("Employee Salary cannot be less than 5 !");
validateErrorList.Add("Employee Salary cannot be less than 10 !");
validateErrorList.Add("Employee Salary cannot be less than 100 !");
}
if (Int32.TryParse(DynamicSearchEmployeeSalary, out output))
{
if (string.IsNullOrWhiteSpace(DynamicSearchEmployeeSalary) || Convert.ToInt32(DynamicSearchEmployeeSalary) < 5)
{
validateErrorList.Add("Employee Salary cannot be less than 5 !");
}
if (string.IsNullOrWhiteSpace(DynamicSearchEmployeeSalary) || Convert.ToInt32(DynamicSearchEmployeeSalary) < 10)
{
validateErrorList.Add("Employee Salary cannot be less than 10 !");
}
if (string.IsNullOrWhiteSpace(DynamicSearchEmployeeSalary) || Convert.ToInt32(DynamicSearchEmployeeSalary) < 100)
{
validateErrorList.Add("Employee Salary cannot be less than 100 !");
}
}
}
if (columnName == "DynamicSearchEmployeeDesigner" && string.IsNullOrWhiteSpace(DynamicSearchEmployeeDesigner))
{
validateErrorList.Add("Employee Designer is required to add a new Employee !");
}
foreach (var validateerroritem in validateErrorList)
{
error += validateerroritem+"\r\n";
}
error = error.ToString().TrimEnd('\r', '\n');
if(error == string.Empty)
{
IsValidated = true;
}
else if (error != string.Empty)
{
IsValidated = false;
}
return error;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.