WPF IDataErrorInfo multiple field validation

时光怂恿深爱的人放手 提交于 2019-12-12 05:14:00

问题


What is a good way to integrate multiple field validation with IDataErrorInfo?

Let say that I have a dialog with 3 textboxes for ftp information

  • URL
  • Username
  • Password

I have put the Required attribute on the fields (assume a normal TextBox for the password).

I validate the ftp connection when the user press "OK". At the moment I show a dialog but it would be nice if I could trigger the Validation error style on ftp connection errors.

I have looked at Validation.MarkInvalid but don't understand how to use it.

var be = GetBindingExpression(xamlURLField);
Validation.MarkInvalid(be, new ValidationError(-- WhatValidationRuleToPutHere --, be, "Can't connect to ftp", null)

回答1:


You are mixing concerns a little here. Validation is for validating user input on a basic level. Doing some post-verification should be handled differently and is generally more complex than you'd want to encompass in the area of "Validation". When something like this is hard, there is usually a reason and this is the reason.

I would treat trying to connect as a separate step in your user interaction and display a message manually.



来源:https://stackoverflow.com/questions/1838300/wpf-idataerrorinfo-multiple-field-validation

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