1

LoB アプリケーションに Prism+MVVM+C#+WPF を使用しています。すべてのビューモデルに継承される ViewModelBase クラスを作成しました。この基本クラスは以下を実装します。

  • IViewModel (私の基本インターフェースの 1 つ)
  • IDataErrorInfo (ViewModel の検証を許可するため)

それは私の IDataErrorInfo 実装に従います:

#region IDataErrorInfo members

    public string this[string propertyName]
    {
        get { return this.Validate(propertyName); }
    }

    public string Error { get; private set; }

    #endregion

    protected string Validate(string propertyName)
    {
        this.Error = null;

        PropertyInfo property = this.GetType().GetProperty(propertyName);
        if (property == null)
            throw new ArgumentException("propertyName");

        foreach (ValidationAttribute attribute in property.GetCustomAttributes(typeof(ValidationAttribute), true))
        {
            try
            {
                object currentValue = property.GetValue(this, null);
                attribute.Validate(currentValue, propertyName);
            }
            catch (ValidationException ex)
            {
                string errorMessage = (!string.IsNullOrWhiteSpace(attribute.ErrorMessage) ? attribute.ErrorMessage: ex.Message);

                if (this.Error == null)
                    this.Error = errorMessage;
                else
                    this.Error += string.Format("\r\n{0}", errorMessage);
            }
        }

        return this.Error;
    }

アプリケーションの特定の時点で、次のように View と ViewModel を構築して関連付けています。

IViewModel viewModel = this.ServiceLocator.GetInstance(typeof(IMyViewModel)) as IMyViewModel;
IView view = this.ServiceLocator.GetInstance(type) as IMyView;

view.ViewModel = viewModel;
this.GlobalRegionManager.Regions[RegionNames.InnerRegion].Add(view);

ビューがviewModelプロパティの読み取りを開始すると、問題が発生します。「this[string propertyName]」が呼び出され、検証関数が実行されます...

ビューでは、検証が必要なプロパティのバインディングは次のように定義されています。

Text="{Binding SourceFileName, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"

初期検証を防ぐ方法についてアドバイスをいただけますか?

前もってありがとう、ジャンルカ

4

0 に答える 0