开发者

IValueConverter and validation on exception

I'm using custom DateTimeToString : IValueConverter

In my ConvertBack method I'm t开发者_运维知识库hrowing Exception when conversion fails, however it is not displayed as validation failure (it is an unhandled application exception), and I want to show it as validation problem (red border).

In short I want it to work like DateTime+Texbox when it shows validation message ("input string was in incorrect format") but with my custom IValueConverter.


Although I agree with winSharp93's answer https://stackoverflow.com/a/6439620/29491 in principle, I've found that if you return a ValidationResult from the ConvertBack method, you will get the expected Validation behaviour.

You will need to use the TryParse or TryParseExact methods as indicated below or catch the FormatException if you are using the Parse methods.

DateTime result;
if (DateTime.TryParseExact(dateString, dateFormat, culture, DateTimeStyles.None, out result))
{
    return result;
}
else 
{
    return new ValidationResult("Date string format error");
}


Just found out this is a known behavior of ivalueconverter. It is because ivalueConverter isn't part of the 'Validation pipeline' in Silverlight. Because the ivalueConverter throws exceptions before it gets to the Validation logic, it isn't treated as a Validation error. There is a post in Silverlight forum for the same problem. Some one has started a request at dotnet.uservoice. Personally I think this should be fixed/improved because converter is a logical place for validation error. After all, how often we get a conversion error? A lot!


Although using ValueConverters is possible, I would not recommend it.

Better take a look at the MVVM-Pattern (Also see: Thought: MVVM eliminates 99% of the need for ValueConverters ). Then, you can implement IDataErrorInfo in your ViewModels and validation gets as easy as it should be.

Staying with ValueConverts will only give you more headaches according to my experience.


I had faced a similar problem while using a value convertor and MVVM pattern. The problem was related to setting of the value in the databinding in view model. The bindings are fired and the propertychangedevent was getting raised. As such the value was already changed and then the convertor was getting called.

If you raise property changed event and then the convertor gets called it throws an unhandled exception even though you have specified ValidatesOnException to true. That is because of the fact that the binding have already been updated as a result of firing the property changed event. Then the convertor gets triggered and throws an exception but the control is unable to catch it.

I moved the logic from converter to do the validation in the setter of the ViewModel bound property. Only if the data was valid I would fire the NotifyPropertyChangedEvent. Otherwise I would throw the exception which would be shown by the UI using ValidatesOnException property of the binding.

Hope this helps.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜