こんにちは私のモデルクラスではエラーの検証を行います。モデルの検証でエラーが発生した場合はボタンを無効にします
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
}
}
任意のアドバイスはありますか?感謝。