0

ウィンドウに ShowDialog として表示するウィンドウがあります。INotifyPropertyChanges と IDataErrorInfo を実装するオブジェクトにバインドするテキスト ボックスがいくつかあります。すべての xtboxes が検証された場合にのみ [OK] ボタンが有効になり、ユーザーが [OK] ボタンをクリックした場合に次の移動が発生するようにしたいと考えています。

ボタンを ICommand にバインドし、CanExcute() でテキストボックスの検証を確認できますが、Excute で何ができるでしょうか? オブジェクトはウィンドウについて知りません。また、テキストボックスの検証を確認してから、すべて有効なイベントを発生させて [OK] ボタンを有効にすることもできますが、IDataErrorInfo 実装で既にチェックしているため、重複したコードが存在します。

では、正しい方法は何ですか?

前もって感謝します

4

1 に答える 1

0

CanExecute は次のようになります。

   public bool CanExecuteOK        
   {
        get
        {
              if (DataModelToValidate.Error == null && DataModelToValidate.Errors.Count == 0) return true;
              else return false;
        }
   }

ここで、Error および Errors プロパティは、this[string propertyName] (IDataErrorInfo に対して暗黙的に実装されている) に対する Wrapper に他なりません。

サンプルモデルクラスは次のとおりです。

    public class SampleModel: IDataErrorInfo, INotifyPropertyChanged
    {
        public SampleModel()
        {
            this.Errors = new System.Collections.ObjectModel.ObservableCollection<string>();
        }
        private string _SomeProperty = string.Empty;
        public string SomeProperty
        {
            get
            {
                return _SomeProperty;

            }
            set
            {
                if (value != _SomeProperty)
                {
                    _SomeProperty= value;
                    RaisePropertyChanged("SomeProperty");
                }
            }
        }
....
....
        //this keeps track of all errors in current data model object
        public System.Collections.ObjectModel.ObservableCollection<string> Errors { get; private set; }
        //Implicit for IDataErrorInfo
        public string Error
        {
            get
            {
                return this[string.Empty];
            }
        }

        public string this[string propertyName]
        {
            get
            {

                string result = string.Empty;
                propertyName = propertyName ?? string.Empty;

                if (propertyName == string.Empty || propertyName == "SomeProperty")
                {
                    if (string.IsNullOrEmpty(this.SomeProperty))
                    {
                        result = "SomeProperty cannot be blank";
                        if (!this.Errors.Contains(result)) this.Errors.Add(result);
                    }
                    else
                    {
                        if (this.Errors.Contains("SomeProperty cannot be blank")) this.Errors.Remove("SomeProperty cannot be blank");
                    }
                }
......
      return result;
    }
于 2011-07-12T02:08:06.163 に答える