开发者

IDataErrorInfo With Multiple Error Messages for a Property

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?


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/


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜