Deterministic and asynchronous field validation in WPF

In my MVVM-based application, I need to check the fields in a data record. If possible, I would like to use the standard WPF validation binding using ErrorTemplates.

However, I would like the execution of the verification logic to be fully controlled / started by the ViewModel (click on View, and do not pull View) for the following reasons:

  • It should work asynchronously because the validation logic may take some time.
  • I need to be more deterministic and fine-grained when the verification logic needs to be executed (for example, only after the user clicks Apply or when the internal state changes so that the records suddenly become invalid)

I know that Silverlight has an INotifyDataErrorInfo that was introduced just for this purpose, but WPF does not. How can I still use my validation logic deterministically and asynchronously?

+6
source share
2 answers

I sent an answer to your other question, which apparently also answered this question.

Create a visual orientation for the control pattern in code

0
source

Built-in validation for WPF and Silverlight is designed for quick client-side validation (e.g. Regex, parsing values, etc.).

If you need to go to the server to perform the check (or validation takes a lot of time), I would do it my own way. For example, when you click the save button, etc.

So, say you have a Save method in the ViewModel (you do not specify which MVVM environment you are using):

 public void Save() { //Do your validation, this might start a new thread (I use Async CTP myself) //If validation is good, do your extra work, else display validation errors } 

I would do all the work required for this as part of the action in the ViewModel

0
source

Source: https://habr.com/ru/post/896469/


All Articles