我首先使用实体框架 6、.Net 框架 4 和代码。
我可以通过使用GetValidationResult
方法获得验证错误。但我无法显示如下图所示的验证消息。如何做到这一点?
我的代码:
<Label Content="Name" />
<Grid Grid.Row="0" Grid.Column="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBox x:Name="txtName"
Width="200"
Margin="8,0,0,0"
MaxLength="150"
Text="{Binding Path=dfc_Name,
ValidatesOnDataErrors=True}" />
</Grid>
<Label Grid.Row="4"
Grid.Column="0"
Content="Description" />
<TextBox x:Name="txtDescription"
Grid.Row="4"
Grid.Column="2"
Width="300"
Height="80"
Margin="8,0,0,0"
HorizontalAlignment="Left"
VerticalContentAlignment="Top"
AcceptsReturn="True"
Text="{Binding Path=dfc_Description,
ValidatesOnDataErrors=True}"
TextWrapping="WrapWithOverflow" />
</Grid>
代码背后:
private readonly Item OItem = new Item();
public ItemView()
{
InitializeComponent();
this.DataContext = OItem;
if (context.Entry(OItem).GetValidationResult().IsValid)
{
}
else
{
}
}