2011-02-17 71 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