WPF IDataErrorInfo多字段验证 [英] WPF IDataErrorInfo multiple field validation
问题描述
将多字段验证与IDataErrorInfo集成的好方法是什么?
What is a good way to integrate multiple field validation with IDataErrorInfo?
假设我有一个包含3个文本框的ftp信息对话框
Let say that I have a dialog with 3 textboxes for ftp information
- URL
- 用户名
- 密码
我已经在字段上放置了Required属性(假设普通的TextBox用作密码).
I have put the Required attribute on the fields (assume a normal TextBox for the password).
当用户按确定"时,我将验证ftp连接.目前,我显示一个对话框,但是如果我可以触发ftp连接错误时的Validation错误样式,那就太好了.
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.
我看过Validation.MarkInvalid,但不知道如何使用它.
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)
推荐答案
您在这里有点担心.验证用于在基本级别上验证用户输入.进行一些后验证的方法应有所不同,并且通常要比您在验证"区域中想要的要复杂得多.当这样的事情很难的时候,通常有一个原因,这就是原因.
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.
这篇关于WPF IDataErrorInfo多字段验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!