2011-02-05 2 views
1

こんにちは私のモデルクラスではエラーの検証を行います。モデルの検証でエラーが発生した場合はボタンを無効にします

public class CurrentUser:IDataErrorInfo, INotifyPropertyChanged 
    { 
//... 

     private string _validationResult; 
     private string _nick; 

     public string Nick 
     { 
      get { return _nick; } 
      set 
      { 
       _nick = value; 
       NotifyPropertyChanged("Nick"); 
      } 
     } 

     public string ValidationResult 
     { 
      get { return _validationResult; } 
      private set 
      { 
       _validationResult = value; 
       NotifyPropertyChanged("ValidationResult"); 
      } 
     } 

     #region Implementation of INotifyPropertyChanged 

     public event PropertyChangedEventHandler PropertyChanged; 

     private void NotifyPropertyChanged(String info) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(info)); 
      } 
     } 

     #endregion 

     #region Implementation of IDataErrorInfo 

     private string NickValid() 
     { 
      if (string.IsNullOrEmpty(Nick)) 
      { 
       return NickNull; 
      } 
      if (Regex.IsMatch(Nick, "[^a-zA-Z0-9-_.]")) 
      { 
       return NickInvalidCharacters; 
      } 
      return string.Empty; 
     } 

     public string Error 
     { 
      get { throw new NotImplementedException(); } 
     } 

     public string this[string propertyName] 
     { 

      get 
      { 
       ValidationResult = string.Empty; 

       switch (propertyName) 
       { 
        case "Nick": 
         ValidationResult = NickValid(); 
         break; 
        default: 
         break; 
       } 
       return ValidationResult; 

      } 
     } 

     #endregion 

    } 

私はこのモデルクラスをビューモデルで使用し、モデルクラスのNickプロパティをcomboBoxコントロールのTextプロパティにバインドします。

また、ビューのボタンクリックイベントのビューモデルクラスからメソッドLogOnをバインドします。

ビューモデル::

[Export(typeof(ILogOnViewModel))] 
[PartCreationPolicy(CreationPolicy.NonShared)] 
public class LogOnViewModel : Screen, ILogOnViewModel, 
    IPartImportsSatisfiedNotification 
{ 

    public CurrentUser CurrentUser { get; set; } 

    public bool CanLogOn 
    { 
     get 
     { 
      return string.IsNullOrWhiteSpace(CurrentUser.ValidationResult); 
     } 
    } 

    //bind on button click event 
    public void LogOn() 
    {} 
} 

ソリューションがあるCurrentUser(オブジェクト)プロパティでの検証がエラーを持っている場合はfalseに簡単なセットCanLogOnプロパティをあるモデルクラスでの検証がエラーを持っている場合、私はdisabaleボタンをしたいと思います。

しかし、CanLogOnにモデルクラスでエラーではないことを通知する方法はありません。私はアプリを実行し、ボタンはまだ無効になっています。

私はモデルでこの動作をachive必要があります。

public string ValidationResult 
    { 
     get { return _validationResult; } 
     private set 
     { 
      _validationResult = value; 
      NotifyPropertyChanged("ValidationResult"); 
      //notify property CanLogOn in view model class 
     } 
    } 

任意のアドバイスはありますか?感謝。

答えて

0

は、あなたのviewmodelで、ユーザーのPropertyChangedイベントにイベントハンドラをアタッチ:

void CurrentUser_PropertyChanged(object sender, PropertyChangedEventArgs e) 
{ 
    if (e.PropertyName == "ValidationResult") NotifyPropertyChanged("CanLogOn"); 
} 

注:

CurrentUser.PropertyChanged += new PropertyChangedEventHandler(CurrentUser_PropertyChanged); 

ValidationResult変更された場合に通知を送っ追加ご参照場合CurrentUser新しいオブジェクトにイベントハンドラを追加する必要があります。あなたはCurrentUserのセッターにアタッチメントコードを置くことでこれを行うことができます。

関連する問題