0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

任意の時点でデータの検証を実行できるINotifyDataErrorInfoインターフェイス

0
Last updated at Posted at 2025-12-15

参考

レガシーな IDataErrorInfo でできないこと

複数の入力項目に依存する検証動作を実装できない

IDataErrorInfoは、対象となるプロパティがビューから入力された場合に、検証動作を実行する。

例えば、対象となるプロパティ A の検証結果が、そのプロパティ以外の値 B にも依存している場合を考える。
B の値が変化したタイミングでは A の検証動作が実行されない。
したがって、A の検証結果が更新されず、検証結果が正しくなくなる。

INotifyDataErrorInfo でできること

任意のタイミングでの検証動作を実行できる

event EventHandler<DataErrorsChangedEventArgs> ErrorsChangedを通じて、検証結果が変化したことを通知できる。

前述のような場合には、B の値が変化したタイミングでErrorsChangedイベントにより、通知をすればよい。

XAMLでの記述が容易

INotifyDataErrorInfoを使う場合、検証を行うことを指示するBinding.ValidatesOnNotifyDataErrorsがデフォルトでtrueなので、XAMLに追加の記述は不要である。

一方、レガシーなIDataErrorInfoは、検証を行うことを指示するBinding.ValidatesOnDataErrorstrueにする必要がある。

付記

INotifyDataErrorInfoは、Visual StudioのCopilotチャットに作業を指示した際、作業結果に使われていたもので、言わば「AIに教えてもらった」機能です。

また、この記事は、AIが下書きしてもらったものを、私が編集して、作成しました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?