wpf アプリケーションの開発に MVVM (prism) を使用しています。
私のモデル クラス「StandardContact」の 1 つは、ビューに直接バインドされたプロパティを持っています。IDataErrorInfo を使用して、モデルにエラーがあるかどうかを追跡して通知します。モデルにエラーがある場合は、「保存」コマンドを無効にします。
ユーザーがデータを入力したら、StandardContact.PropertyChanged ハンドラーを使用して、「保存」コマンドを実行できるかどうか (つまり、ユーザーが入力したモデル データが有効かどうか) を確認します。問題は、StandardContact.PropertyChanged ハンドラーが IDataErrorInfo の検証コードの前に呼び出されるため、「保存」コマンドの CanExecute がコマンドを実行できるかどうかを正しく反映しないことです。私が探しているのは、CanExecute が実行される前に IDataErrorInfo 検証を実行して、CanExecute がモデル内の最新のデータに対してクエリを実行し、それが有効かどうかを判断することです。これが私が使用しているサンプルコードです
モデル:
public class StandardContact :EntityBase, IDataErrorInfo
{
public virtual string Name
{
get { return _name; }
set { SetField(ref _name, value, () => Name); }
}
//...
//Validators
public string this[string propertyName]
{
get
{
string error = null;
//....
}
ビューモデル
public class SContactEditViewModel : NotificationObject, INavigationAware
{
//....
StandardContact.PropertyChanged +=
new PropertyChangedEventHandler(StandardContact_PropertyChanged);
void StandardContact_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
//Requery if command can execute
SaveNewCommand.RaiseCanExecuteChanged();
}
}