具有属性的多个错误消息的 IDataErrorInfo

发布于 2024-10-28 11:36:15 字数 2175 浏览 1 评论 0原文

看来其他人也遇到了这个问题: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紧拥背影 2024-11-04 11:36:15

如果您像其他问题一样使用转换器,我很确定这不是最好的方法。特别是在像 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/

撩人痒 2024-11-04 11:36:15

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

例如,假设 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://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:

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

Then by updating the code to the following, you will bypass the converter:

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

Read more about it here: https://learn.microsoft.com/en-us/dotnet/api/system.windows.data.relativesource.self?view=netframework-4.8

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文