I'm using custom DateTimeToString : IValueConverter
In my ConvertBack method I'm throwing 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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With