deterministic and asynchronous field validation in WPF

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-21 13:11:09

问题


In my MVVM based application I need to validate fields in a data entry from. If possible, I would like to use the standard WPF validation binding with ErrorTemplates.

However I would like the execution of the validation logic to be completely driven/triggered by the ViewModel (push to the View, not pull by the View) for the following reasons:

  1. It must work asynchronously because validation logic might take a while to execute.
  2. I need to be more deterministic and fine grained when validation logic is to be executed (e.g. only after the user clicks "Apply" or when the internal state changed in a way that entries suddenly become invalid)

I know Silverlight has INotifyDataErrorInfo which was introduced for exactly this purpose, but WPF doesn't. How can I still have my validation logic exectuted deterministically and asynchronously?


回答1:


I posted an answer on your other question that apparently answered this one too.

Create a visualtree off of a control template in code




回答2:


The built in validation for WPF and Silverlight is meant for quick client-side validation (such as Regex, parsing values, etc.).

If you need to go to a server to perform validation (or validation takes a long time), I would do that in a custom way. Such as when clicking a save button, etc.

So say you have a Save method in a ViewModel (you don't mention which MVVM framework you use):

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 just do all the work required for this within an action in your ViewModel



来源:https://stackoverflow.com/questions/7281264/deterministic-and-asynchronous-field-validation-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!