2011-02-17 72 views
1

我有这样定义的属性业务对象:在业务对象集合属性WPF数据验证

/// <summary> 
    /// Gets the Interest Payments dates Collection. 
    /// </summary> 
    public BindingList<DateTime> InterestPaymentDatesCollection 
    { 
     get 
     { 
      return this._interestPaymentDatesCollection; 
     } 
    } 

这是一个WPF应用程序使用(我是一个ASP.Net开发人员,进行了手这个项目) - 基本上我需要确保_interestPaymentDatesCollection有一个值设置 - 否则我需要通知用户该字段是必需的,等等。作为WPF新手我不熟悉如何做到这一点。我尝试阅读关于使用IDataErrorInfo的示例,但无法拼凑出如何在集合属性上执行此操作。

建议感激!

回答

1

你的类,用于保存集合将实现IDataErrorInfo,你会用一个验证错误覆盖this[string.columnName]财产

有实现验证的方法很多,但这里有一个简单的例子:

public class TestModel : IDataErrorInfo 
{ 
    public List<DateTime> MyCollection {get; set;} 

    public string this[string columnName] 
    { 
     get { return this.GetValidationError(propertyName); } 
    } 

    public string GetValidationError(string propertyName) 
    { 
     switch (propertyName) 
     { 
      case "MyCollection": 
       // Do validation here and return a string if an error is found 
       if (MyCollection == null) return "Collection not Initialized"; 
     } 
     return null; 
    } 
} 
+0

这个看起来很棒。我很感激。我会把你放在船上,但我缺乏代表点。不过,你的代码示例是值得赞赏的。 – RatBoyStl 2011-02-17 16:52:16