2009-03-11 9 views
1

私はテキストボックスを含むユーザーコントロールを持っています。 Iは、IDataErrorInfoインターフェイスを実装する人というクラスた:IDataErrorInfo:入力を検証していません

public partial class TxtUserControl : UserControl 
    {   
     private Binding _binding; 

     public void SetSource(string path,Object source) 
     { 
      _binding = new Binding(path); 
      _binding.Source = source; 
      ValidationRule rule; 
      rule = new DataErrorValidationRule(); 
      rule.ValidatesOnTargetUpdated = true;    
      _binding.ValidationRules.Add(rule); 
      _binding.ValidatesOnDataErrors = true; 
      _binding.UpdateSourceTrigger = UpdateSourceTrigger.LostFocus; 
      _binding.NotifyOnValidationError = true;    
      _binding.ValidatesOnExceptions = true; 
      txtNumeric.SetBinding(TextBox.TextProperty, _binding);     
     } 
... 
} 

ユーザーコントロールを含むWPFウィンドウがあります。今、ユーザーコントロールコードバインディングを設定し、それを通してSetSourceというメソッドを

class Person : IDataErrorInfo 
{ 
private bool hasErrors = false; 
#region IDataErrorInfo Members 

     public string Error 
     {    
      get 
      { 
       string error = null; 
       if (hasErrors) 
       { 
        error = "xxThe form contains one or more validation errors"; 
       } 
       return error; 
      } 
     } 

     public string this[string columnName] 
     { 
      get 
      { 
       return DoValidation(columnName); 
      } 
     } 
     #endregion 
} 

を露出させます次のコード:

このコードでは、バインドは正常に動作していますが、検証は実行されません。このコードで何が間違っていますか?

答えて

3

AgeクラスはIDataErrorInfoを実装する必要があります。あなたのPersonクラスはAgeのプロパティを検証するよう求められません。

これはオプションではない場合、私はこれをサポートするのに十分な拡張性があるWPFの検証システムを作成しました。 URLはこちらです:ZIPで

それを記述したワード文書です。

編集:

class Constraint 
{ 
    public string Message { get; set; } 
    public Func<bool> Validate; 
    public string Name { get; set; } 
} 

class Age : IDataErrorInfo 
{ 
    private readonly List<Constraint> _constraints = new List<Constraint>(); 

    public string this[string columnName] 
    { 
     get 
     { 
      var constraint = _constrains.Where(c => c.Name == columnName).FirstOrDefault(); 
      if (constraint != null) 
      { 
       if (!constraint.Validate()) 
       { 
        return constraint.Message; 
       } 
      } 
      return string.Empty; 
     } 
    } 
} 

class Person 
{ 
    private Age _age; 

    public Person() 
    { 
     _age = new Age(
      new Constraint("Value", "Value must be greater than 28",() => Age > 28); 
    } 
} 

はまた、このリンクを参照してください:ここでの年齢があまりにもスマートされることなくIDataErrorInfoを実装できる一つの方法だ

http://www.codeproject.com/KB/cs/DelegateBusinessObjects.aspx

+0

Paul、 開発したフレームワークを見てきました。これを行う他の方法はありますか?ここでいくつかのポインタを提供できる場合は、 – Ngm

+0

Have AgeがIDataErrorInfoを実装するか、WPF ValidationRule(UIレベルの検証)を使用してください - これは私が考えることができる唯一の回避策です。 –

+0

私の検証フレームワークでは、UIコントロールをルール名(たとえば "AgeValue")に関連付けるだけで、IDataErrorInfoでは "AgeValue"ルールの検証を処理します。私のフレームワークでは、WPFの検証の方法と同じように、プロパティ名と検証ルール名の間に結合はありません。 –

0

別のオプションは、年齢のクラスにIDataErrorInfoを実装することができPersonクラスによってキャプチャされるべきイベントをOnValidationRequestedのように作成します。この方法で、Personクラスの他のプロパティに基づいてAgeフィールドを検証できます。