使用WPF和entity framework的DataAnnotationsvalidation数据?

有什么方法来validation在WPF和entity framework中使用DataAnnotations?

您可以使用DataAnnotations.Validator类,如下所述:

http://johan.driessen.se/archive/2009/11/18/testing-dataannotation-based-validation-in-asp.net-mvc.aspx

但是,如果您使用的是“好友”类的元数据,则需要在validation之前注册该事实,如下所述:

http://forums.silverlight.net/forums/p/149264/377212.aspx

TypeDescriptor.AddProviderTransparent( new AssociatedMetadataTypeTypeDescriptionProvider(typeof(myEntity), typeof(myEntityMetadataClass)), typeof(myEntity)); List<ValidationResult> results = new List<ValidationResult>(); ValidationContext context = new ValidationContext(myEntity, null, null) bool valid = Validator.TryValidateObject(myEntity, context, results, true); 

[添加以下回应Shimmy的评论]

我写了一个通用的方法来实现上面的逻辑,以便任何对象都可以调用它:

 // If the class to be validated does not have a separate metadata class, pass // the same type for both typeparams. public static bool IsValid<T, U>(this T obj, ref Dictionary<string, string> errors) { //If metadata class type has been passed in that's different from the class to be validated, register the association if (typeof(T) != typeof(U)) { TypeDescriptor.AddProviderTransparent(new AssociatedMetadataTypeTypeDescriptionProvider(typeof(T), typeof(U)), typeof(T)); } var validationContext = new ValidationContext(obj, null, null); var validationResults = new List<ValidationResult>(); Validator.TryValidateObject(obj, validationContext, validationResults, true); if (validationResults.Count > 0 && errors == null) errors = new Dictionary<string, string>(validationResults.Count); foreach (var validationResult in validationResults) { errors.Add(validationResult.MemberNames.First(), validationResult.ErrorMessage); } if (validationResults.Count > 0) return false; else return true; } 

在每个需要validation的对象中,我添加一个对这个方法的调用:

 [MetadataType(typeof(Employee.Metadata))] public partial class Employee { private sealed class Metadata { [DisplayName("Email")] [Email(ErrorMessage = "Please enter a valid email address.")] public string EmailAddress { get; set; } } public bool IsValid(ref Dictionary<string, string> errors) { return this.IsValid<Employee, Metadata>(ref errors); //If the Employee class didn't have a buddy class, //I'd just pass Employee twice: //return this.IsValid<Employee, Employee>(ref errors); } } 

我认为Craigs所缺less的是如何真正检查是否存在validation错误。 这是Steve Sanderson为那些想要在传送层中运行validation检查然后呈现( http://blog.codeville.net/category/xval/ ,代码在示例项目中)的DataAnnotationvalidation运行器:

 public static IEnumerable<ErrorInfo> GetErrors(object instance) { var metadataAttrib = instance.GetType().GetCustomAttributes (typeof(MetadataTypeAttribute), true). OfType<MetadataTypeAttribute>().FirstOrDefault(); var buddyClassOrModelClass = metadataAttrib != null ? metadataAttrib.MetadataClassType : instance.GetType(); var buddyClassProperties = TypeDescriptor.GetProperties (buddyClassOrModelClass).Cast<PropertyDescriptor>(); var modelClassProperties = TypeDescriptor.GetProperties (instance.GetType()).Cast<PropertyDescriptor>(); return from buddyProp in buddyClassProperties join modelProp in modelClassProperties on buddyProp.Name equals modelProp.Name from attribute in buddyProp.Attributes. OfType<ValidationAttribute>() where !attribute.IsValid(modelProp.GetValue(instance)) select new ErrorInfo(buddyProp.Name, attribute.FormatErrorMessage(string.Empty), instance); } 

我不熟悉WPF(不知道是否有一些现成的解决scheme为你提出的问题),但也许你可以使用它。

另外,在他的博客上有一些评论,在某些情况下,它没有正确评估validation规则,但对我来说却从未失败过。

您可能对WPF应用程序框架(WAF)BookLibrary示例应用程序感兴趣。 它正是你所要求的:在WPF和entity framework中使用DataAnnotations。

我有同样的问题,并发现以下想法:

  • 通知模式
  • 将Silverlight的DataForm控件移植到CodePlex上的WPF

使用“好友类”。 第4号在这个如何做 。

我已经写了一个基于贡献者的validation器,其中包括一个DataAnnotationvalidation贡献者,并且还检查破坏的绑定(用户input了错误的types)

http://adammills.wordpress.com/2010/07/21/mvvm-validation-and-type-checking/

在.NET 4中,在使用此扩展的Entity-Framework中有validation支持,请查看: http : //blogs.msdn.com/adonet/archive/2010/01/13/introducing-the-portable-extensible-metadata。 ASPX

我不确定它是否使用DataAnnotations tho。

UPDATE
我尝试了VB.NET,它不工作,我认为它只支持C#项目。