【问题标题】:IDataErrorInfo With Multiple Error Messages for a Property带有多个错误消息的 IDataErrorInfo 属性
【发布时间】:2011-03-31 00:54:07
【问题描述】:

似乎其他人遇到了这个问题: Validation.HasError does not trigger again if new error comes in while already 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;
        }
    }

发生了什么事?

【问题讨论】:

标签: wpf idataerrorinfo


【解决方案1】:

如果您在其他问题中使用转换器,我很确定这不是做事的最佳方式。尤其是在像 WPF 这样的动态环境中。

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

http://joshsmithonwpf.wordpress.com/2008/10/08/binding-to-validationerrors0-without-creating-debug-spew/

【讨论】:

  • 当我意识到 HasError 正在更新触发器时,这就是我开始走的路。太糟糕了,大多数人都使用触发器而不是这种方式。它可以防止 WPF 带来一些心痛。
【解决方案2】:

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

例如,假设 XAML 如下所示:

<Setter Property="ToolTip" Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors), Converter={StaticResource yourConverter}}" />

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

<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)/ErrorContent}" />

在此处了解更多信息:https://docs.microsoft.com/en-us/dotnet/api/system.windows.data.relativesource.self?view=netframework-4.8

【讨论】:

    猜你喜欢
    • 2011-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-28
    • 1970-01-01
    • 2015-08-19
    • 1970-01-01
    • 2017-02-28
    相关资源
    最近更新 更多