1

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

到底是怎么回事?

4

2 回答 2

3

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

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

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

于 2011-04-01T13:11:39.623 回答
0

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

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

于 2020-03-30T14:10:08.973 回答