I implemented type CustomDoubleConverter
which implements IValueConverter
.
I included it Converter={StaticResource customDoubleConverter}
and corresponding resource in xaml
-file.
It works fine.
The question is about error handling.
I would like to check if UI string represents correct double
. if no then show one of two messages on label depending on invalid input: empty string or other non-double string.
Which approach should be used to show custom error messages on UI form when error happens during type converting from string?
I tried to do via exceptions, but received unhandled exception. Tip: Do not throw an exception in a IValueConverter convinced me not to try exceptions any more.
I was able to check double after converting with correct handling of IDataErrorInfo
interface in view model of MVVM. But it could be done after successful string to double conversion, which is not the case described above.
I have also ValidatesOnDataErrors="True"
and ValidatesOnExceptions="True"
for my text box.
I use MVVM approach for design if it is helpful (similar to the one described in WPF Apps With The Model-View-ViewModel Design Pattern).
In short:
I want to parse double from TextBox
and show one of three error messages if any on UI label:
- empty string (mentioned above),
- invalid double string (mentioned above), and
- negative number (does not mentioned above, but I handled it via
IDataErrorInfo
- it is not an issue).