IDataErrorInfo With Multiple Error Messages for a Property

為{幸葍}努か 提交于 2019-12-11 07:35:19

问题


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?


回答1:


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/



来源:https://stackoverflow.com/questions/5494200/idataerrorinfo-with-multiple-error-messages-for-a-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!