Я создаю простое приложение на C # MVVM, и мне нужна ваша помощь (совет). Я не знаю, что это можно сделать, но вы можете знать, как это сделать. Итак, для проверки моих текстовых полей я использую интерфейс IDataErrorInfo. Это работает довольно круто, но у меня есть один вопрос. Есть ли возможность поместить сообщения об ошибках в список для отображения в виде списка? Я хочу избежать "\ r \ n" в моих сообщениях об ошибках. Заранее спасибо:)
Ниже мой код:
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>