Wpf IDataErrorInfo包含属性的多条错误消息

Wpf IDataErrorInfo包含属性的多条错误消息,wpf,idataerrorinfo,Wpf,Idataerrorinfo,似乎其他人有此问题: Validation.Error未使用最新的错误消息进行更新 它显示的是前一个错误,而不是上次实际调用的错误。当我记录每次返回时,返回的PropertyX大于或PropertyX小于,但它不会在我的工具提示中显示该消息。它将显示“必需” 我还发现,当返回PropertyX大于或PropertyX小于时,工具提示的转换器不会被调用 以下是验证代码: string this[string columnName] { get {

似乎其他人有此问题:

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;
        }
    }
string此[string columnName]
{
得到
{
开关(列名称)
{
案例“财产1”:
整数输出;
if(true==string.IsNullOrEmpty(this.Property1))
{
返回“必需”;
}else if(true==int.TryParse(this.Property1,输出))
{
返回“无效整数”;
}else if(true==this.Property1Int.HasValue&&
true==this.Property2Int.HasValue)
{
if(this.Property1Int.Valuethis.Property1Int.Value)
{
返回“Property2大于Property1”;
}
}
打破
};
返回字符串。空;
}
}

发生了什么事?

如果你像在其他问题中一样使用转换器,我敢肯定这不是最好的方法。特别是在动态环境中,如WPF

因此,我建议直接绑定到
(Validation.Errors).CurrentItem
,而不是像这里所述使用转换器:


如果您的绑定使用转换器,则可以通过卸下转换器并更改绑定来解决此问题

例如,假设XAML如下所示:


然后,通过将代码更新为以下内容,您将绕过转换器:



请在此处阅读更多信息:

这可能是不可能的,因为如果它在Silverlight中被允许,我们将有一个返回IEnumerable而不是单个字符串的方法。可能的重复您可以添加如何绑定此错误吗?它和复制的一样吗?是的,我用同样的方式绑定Validation.HasError。当我意识到HasError正在更新触发器时,我开始沿着这条路径。可惜大多数人用触发器而不是用这种方式。用WPF可以防止一些心痛。