【发布时间】:2014-03-28 05:00:55
【问题描述】:
我正在使用 WPF、C# 和 EntityFramework 4.0 编写应用程序。
我必须验证一个日期,如果日期在今天之后,我想向用户显示如下内容:
http://www.nbdtech.com/images/blog/20100621/NiceValidation.png
我的代码是:
在 LoanWindow.xaml 中:
<!-- In my window resources -->
<Style x:Key="datoNoValido" TargetType="{x:Type DatePicker}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="Wrong Date"/>
</Trigger>
</Style.Triggers>
</Style>
<!-- After some code in the Grid -->
<DatePicker Style="{StaticResource datoNoValido}"
Name="fecha_SalidaDatePicker"
SelectedDate="{Binding Path=Fecha_Salidad, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true, UpdateSourceTrigger=PropertyChanged}" />
我的部分课程:
public partial class Loan : IValidatableObject
{
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (DateTime.Compare(MyDate, DateTime.Now.Date) < 0)
{
ValidationResult vr = new ValidationResult("the message", new[] { "MyDate" });
this.validationErrors.Add(vr);
}
return this.validationErrors;
}
}
在 LoanWindow.xaml.cs 中:
//Some code and after
var errors = p.Validate(null);
foreach (var item in errors)
{
MessageBox.Show(item.ErrorMessage);
}
部分类中定义的消息(与实体框架类的名称匹配)显示出来,但数据选择器从不显示红线。我做错了什么?我该怎么做?
谢谢。
【问题讨论】:
-
我只看到了 WPF 中使用的 IDataErrorInfo,所以我查了一下:nujk.com/idataerrorinfo-vs-ivalidatableobject 这是如何在 WPF 中使用 IDataErrorInfo 的链接:codeblitz.wordpress.com/2009/05/08/…
-
谢谢@LeeO。我马上去看看
标签: c# .net wpf xaml entity-framework-4