具有属性的多个错误消息的 IDataErrorInfo
看来其他人也遇到了这个问题: Validation.HasError 确实如果已经为 true 时出现新错误,则不会再次触发
Validation.Error 不会使用最新的错误消息进行更新。
它显示前一个错误,而不是最后实际调用的错误。当我记录每次返回时,返回的 PropertyX 大于或 PropertyX 小于,但它不会在我的工具提示中显示该消息。它将显示“必填”。
我还发现,当返回 PropertyX 大于或 PropertyX 小于时,不会调用工具提示转换器。
这是验证代码:
string this[string columnName]
{
get
{
switch(columnName)
{
case "Property1":
int output;
if (true == string.IsNullOrEmpty(this.Property1))
{
return "Required";
} else if (true == int.TryParse(this.Property1, out output))
{
return "Invalid integer";
} else if (true == this.Property1Int.HasValue &&
true == this.Property2Int.HasValue)
{
if (this.Property1Int.Value < this.Property2Int.Value)
{
return "Property2 is greater than Property1";
}
}
break;
case "Property2":
int output;
if (true == string.IsNullOrEmpty(this.Property2))
{
return "Required";
} else if (true == int.TryParse(this.Property2, out output))
{
return "Invalid integer";
} else if (true == this.Property1Int.HasValue &&
true == this.Property2Int.HasValue)
{
if (this.Property2Int.Value > this.Property1Int.Value)
{
return "Property2 is greater than Property1";
}
}
break;
};
return string.Empty;
}
}
这是怎么回事?
It appears someone else is having this issue:
Validation.HasError does not trigger again if new error comes in while already true
The Validation.Error is not updating with the latest error message.
It shows the previous error not the one that actually got called last. When I log each return, the PropertyX is greater than or PropertyX is less than is returned, but it does not display that message in my tooltip. It will displays "Required".
I also found that my converter for the tooltip does not get called when the PropertyX is greater than or PropertyX is less than is returned.
Here is the validation code:
string this[string columnName]
{
get
{
switch(columnName)
{
case "Property1":
int output;
if (true == string.IsNullOrEmpty(this.Property1))
{
return "Required";
} else if (true == int.TryParse(this.Property1, out output))
{
return "Invalid integer";
} else if (true == this.Property1Int.HasValue &&
true == this.Property2Int.HasValue)
{
if (this.Property1Int.Value < this.Property2Int.Value)
{
return "Property2 is greater than Property1";
}
}
break;
case "Property2":
int output;
if (true == string.IsNullOrEmpty(this.Property2))
{
return "Required";
} else if (true == int.TryParse(this.Property2, out output))
{
return "Invalid integer";
} else if (true == this.Property1Int.HasValue &&
true == this.Property2Int.HasValue)
{
if (this.Property2Int.Value > this.Property1Int.Value)
{
return "Property2 is greater than Property1";
}
}
break;
};
return string.Empty;
}
}
What is going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您像其他问题一样使用转换器,我很确定这不是最好的方法。特别是在像 WPF 这样的动态环境中。
因此,我建议直接绑定到
(Validation.Errors).CurrentItem
而不是使用转换器,如下所述:http://joshsmithonwpf.wordpress.com/2008/10/08/binding-to-validationerrors0-没有-创建-调试-spew /
If you are using converter as in other question, them I'm prety sure its not the best way to do things. Especialy in dynamic enviorment like WPF.
So I would recomend binding to
(Validation.Errors).CurrentItem
directly instead of using converter as described here:http://joshsmithonwpf.wordpress.com/2008/10/08/binding-to-validationerrors0-without-creating-debug-spew/
如果您的绑定使用转换器,则可以通过删除转换器并更改绑定来解决此问题。
例如,假设 XAML 如下所示:
然后通过将代码更新为以下内容,您将绕过转换器:
在此处阅读有关它的更多信息:https://learn.microsoft.com/en-us/dotnet/api/system.windows .data.relativesource.self?view=netframework-4.8
If your binding is using a converter, then this problem could be solved by removing the converter and changing the binding.
For example, say that the XAML looks like the following:
Then by updating the code to the following, you will bypass the converter:
Read more about it here: https://learn.microsoft.com/en-us/dotnet/api/system.windows.data.relativesource.self?view=netframework-4.8