1
我想实现在Winforms中模拟Errorprovider的验证机制。 我试图使用IDataErrorInfo,但问题是它试图对表单加载执行验证。我的要求是我需要执行验证点击页面上的按钮。 是否有任何其他更好的方法来执行WPF验证?在WPF中处理验证的最佳方式是什么?
我想实现在Winforms中模拟Errorprovider的验证机制。 我试图使用IDataErrorInfo,但问题是它试图对表单加载执行验证。我的要求是我需要执行验证点击页面上的按钮。 是否有任何其他更好的方法来执行WPF验证?在WPF中处理验证的最佳方式是什么?
听起来像是你有验证现在比较容易的部分想通了 - 防止它验证,直到需要。把一个布尔值属性上实现的IErrorInfo类:
public bool IsReadyToValidate {get;set;}
在每个属性
public object SomeProperty
{
get {return _someProperty;}
set
{
if(_someProperty == value) return;
_property = value;
if(IsReadyToValidate)
//do validation
,或者如果您使用基于属性的验证:
[CustomMethodValidator("ValidateSomeProperty")]
public string SomeProperty
{
if(_someProperty == value) return;
set
{
if(_property == value)
return;
_property = value;
RaisePropertyChanged(() => SomeProperty);
}
}
private bool ValidateSomeProperty()
{
bool isValid = true;
if(IsReadyToValidate)
//do validation
return is valid;
在CLI按钮的ck,将IsReadyToValidate设置为true并重新生效。这一切都取决于你的实现。 也许这样的方法?
public void Validate()
{
foreach (var prop in PropertiesWithValidators[GetType()])
ValidateProperty(prop.Name);
HasErrors = _errorDictionary.Count > 0;
RaisePropertyChanged(() => Error);
}
,或者你可以做一些野蛮的方式,像你这样的翻转标志验证
public void Validate()
{
IsReadyToValidate = true;
//now it will validate:
SomeProperty = SomeProperty;
注意你有一个非常开放的问题,有这么多的方式做到这一点后restuffing属性,所以这些只是一些想法让你去.. –