[英]How do I properly use .NET Data Annotations for data validation in WPF with a WCF backend?
Question title pretty much explains what I am trying to do.问题标题几乎解释了我想做什么。
Simplification of my code for example purpose:出于示例目的简化我的代码:
Bits of an example WCF Service:位示例 WCF 服务:
pulic class Restaurant
{
//RegEx to only allow alpha characters with a max length of 40
//Pardon if my regex is slightly off
[RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$")]
public string Name { get; set; }
}
public class RestaurantService
{
List<Restaurant> restaurants = new List<Restaurant>();
public AddRestaurant(string name)
{
Restaurant restaurant = new Restaurant();
restaurant.Name = name;
restaurants.Add(restaurant);
}
}
Bits of example XAML:示例 XAML 的位:
<TextBox name="txt1" Text="{Binding Restaurant.Name, ValidatesOnDataErrors=True}"/>
How do I make my view do something when my data annotation is violated?当我的数据注释被违反时,如何让我的视图执行某些操作?
All of the examples I can find here and elsewhere are either not exactly what I am looking for or that have to do with ASP.NET. I don't know enough about WPF and Data Annotations and I am very green with WCF.我在这里和其他地方可以找到的所有示例要么不是我正在寻找的,要么与 ASP.NET 有关。我对 WPF 和数据注释知之甚少,我对 WCF 非常陌生。
I have tried implementing the IDataErrorInfo interface, but I can't seem to get anything to fire in it.我已经尝试实现 IDataErrorInfo 接口,但我似乎无法在其中触发任何东西。 I found this code in another different question on StackOverflow.
我在 StackOverflow 上的另一个不同问题中找到了这段代码。 I implemented this in my Restaurant class in the WCF service.
我在我的餐厅 class 的 WCF 服务中实现了这个。
public string this[string columnName]
{
get
{
if (columnName == "Name")
{
return ValidateProperty(this.Name, columnName);
}
return null;
}
}
protected string ValidateProperty(object value, string propertyName)
{
var info = this.GetType().GetProperty(propertyName);
IEnumerable<string> errorInfos =
(from va in info.GetCustomAttributes(true).OfType<ValidationAttribute>()
where !va.IsValid(value)
select va.FormatErrorMessage(string.Empty)).ToList();
if (errorInfos.Count() > 0)
{
return errorInfos.FirstOrDefault<string>();
}
return null;
}
Classes that are to be bound in XAML must inherit from INotifyDataErrorInfo or IDataErrorInfo interface. XAML 中要绑定的类必须继承INotifyDataErrorInfo 或IDataErrorInfo 接口。 To my knowledge, INotifyDataErrorInfo does not exist in WPF (4) but only in Silverlight and.Net 4.5.
据我所知,INotifyDataErrorInfo在WPF(4)中不存在,只存在于Silverlight和.Net 4.5中。
To answer your question - your class must inherit from IDataErrorInfo to make WPF react where there is an error (any error) in your class. So you have to have要回答你的问题 - 你的 class 必须继承自 IDataErrorInfo 才能使 WPF 在你的 class 中出现错误(任何错误)时做出反应。所以你必须有
public class Restaurant : IDataErrorInfo
{...}
Implemented.实施的。 Server classes can be annotated with ValidationAttribute but this will not flow if you simply add Service Reference.
服务器类可以用 ValidationAttribute 注释,但如果您只是添加服务引用,这将不会流动。 If you can share the DLL across the client and the service then you should have a working solution as long as your class inherits from IDataErrorInfo.
如果您可以在客户端和服务之间共享 DLL,那么只要您的 class 继承自 IDataErrorInfo,您就应该有一个有效的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.