简体   繁体   English

如何在后端为 WCF 的 WPF 中正确使用 .NET 数据注释进行数据验证?

[英]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,您就应该有一个有效的解决方案。

You can see an example here你可以在这里看到一个例子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在WPF中使用数据注释为文本框创建输入验证? - How to use data annotations to create an input-validation for a textbox in WPF? 如何使用数据注释在WPF中进行单位转换 - How to use data annotations to do units conversion in WPF 如何使用IDataErrorInfo和数据注释进行简单验证 - How to do a simple validation with IDataErrorInfo and data annotations 我可以将WCF数据注释与OperationContract的参数一起使用吗? - Can I use WCF Data Annotations with OperationContract's arguments? 在WPF中通过单击按钮进行数据注释的屏幕验证 - Screen Validation With Data Annotations on Button Click in WPF 如何使用.NET Framework 3.5中的数据注释对C#类进行属性验证? - How does one do property validation of a C# class using Data Annotations in .NET Framework 3.5? 在WCF数据服务中,如何将SetServiceOperationAccessRule与远程程序集一起使用 - In WCF Data Services, how do I use SetServiceOperationAccessRule with remote Assemblies 如何通过WCF在Chrome扩展程序和.NET(WPF / C#)之间中继数据? - How can I relay data between Chrome Extension and .NET (WPF/C#) via WCF? 如何正确托管连接到IIS中的SQLServer的WCF数据服务? 为什么我会收到错误? - How do I properly host a WCF Data Service that connects to SQLServer in IIS? Why am I getting errors? ASP NET 4 如何使用带有 MinLength 属性的 DataType Password 的 Data Annotations? - ASP NET 4 How can I use Data Annotations with DataType Password with a MinLength property?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM