我正在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>
答案 0 :(得分:0)
感谢Blacktempel的建议,我创建了以下解决方案。我希望这很好。
struct RGBQ { uint8_t rgbBlue, rgbGreen, rgbRed, rgbReserved; };
int m_width = 61, m_height = 61;
int bitcount = 8;
int width_in_bytes = ((m_width * bitcount + 31) / 32) * 4;
int imagesize = width_in_bytes * m_height;
std::vector<RGBQ> color_table{ { 223, 223, 123 }, { 230, 0, 12 } };
std::vector<uint8_t> data(imagesize);
data[2] = 1;
FileHeader fileHeader = { 0 };
BitMapInfoHeader infoHeader = { 0 };
fileHeader.bfType = 0x4d42;
fileHeader.bfSize = sizeof(fileHeader) + sizeof(infoHeader) + color_table.size()
* sizeof(RGBQ) + data.size();
fileHeader.bfOffBits = sizeof(fileHeader) + sizeof(infoHeader);
infoHeader.biSize = 40;
infoHeader.biWidth = m_width;
infoHeader.biHeight = -m_height;
infoHeader.biPlanes = 1;
infoHeader.biBitCount = bitcount;
infoHeader.biClrUsed = color_table.size();
std::ofstream file2("out.bmp", std::ios::binary | std::ios::trunc);
file2.write((char*)&fileHeader, sizeof(fileHeader));
file2.write((char*)&infoHeader, sizeof(infoHeader));
file2.write((char*)color_table.data(), color_table.size() * sizeof(RGBQ));
file2.write((char*)data.data(), data.size());
file2.close();