1212
1313namespace Walkabout . WpfConverters
1414{
15+ public class ValueConverterException : Exception
16+ {
17+ public ValueConverterException ( string message ) : base ( message ) { }
18+ }
19+
1520 // Extracts the first letter of the Category name.
1621 public class CategoryTypeLetterConverter : IValueConverter
1722 {
@@ -478,43 +483,51 @@ public object ConvertBack(object value, Type targetType, object parameter, Cultu
478483 {
479484 if ( value != null && value . GetType ( ) != typeof ( string ) )
480485 {
481- throw new Exception ( "Unexpected value type passed to PreserveDecimalDigitsValueConverter.ConvertBack : " + value . GetType ( ) . Name ) ;
486+ throw new ValueConverterException ( "Unexpected value type passed to PreserveDecimalDigitsValueConverter.ConvertBack : " + value . GetType ( ) . Name ) ;
482487 }
483- string s = ( string ) value ;
484-
485- if ( targetType == typeof ( SqlDecimal ) )
488+ try
486489 {
487- if ( string . IsNullOrWhiteSpace ( s ) )
490+ string s = ( string ) value ;
491+
492+ if ( targetType == typeof ( SqlDecimal ) )
488493 {
489- return new SqlDecimal ( ) ;
494+ if ( string . IsNullOrWhiteSpace ( s ) )
495+ {
496+ return new SqlDecimal ( ) ;
497+ }
498+ return SqlDecimal . Parse ( s ) ;
490499 }
491- return SqlDecimal . Parse ( s ) ;
492- }
493- else if ( targetType == typeof ( decimal ) )
494- {
495- if ( string . IsNullOrWhiteSpace ( s ) )
500+ else if ( targetType == typeof ( decimal ) )
496501 {
497- return 0D ;
502+ if ( string . IsNullOrWhiteSpace ( s ) )
503+ {
504+ return 0D ;
505+ }
506+
507+ return decimal . Parse ( s ) ;
498508 }
509+ else if ( targetType == typeof ( DateTime ) )
510+ {
511+ if ( string . IsNullOrWhiteSpace ( s ) )
512+ {
513+ return DateTime . Now ;
514+ }
499515
500- return decimal . Parse ( s ) ;
501- }
502- else if ( targetType == typeof ( DateTime ) )
503- {
504- if ( string . IsNullOrWhiteSpace ( s ) )
516+ return DateTime . Parse ( s ) ;
517+ }
518+ else if ( targetType == typeof ( string ) )
505519 {
506- return DateTime . Now ;
520+ return s ;
521+ }
522+ else
523+ {
524+ throw new Exception ( "Unexpected target type passed to PreserveDecimalDigitsValueConverter.ConvertBack : " + value . GetType ( ) . Name ) ;
507525 }
508-
509- return DateTime . Parse ( s ) ;
510- }
511- else if ( targetType == typeof ( string ) )
512- {
513- return s ;
514526 }
515- else
527+ catch ( Exception ex )
516528 {
517- throw new Exception ( "Unexpected target type passed to PreserveDecimalDigitsValueConverter.ConvertBack : " + value . GetType ( ) . Name ) ;
529+ // Need to wrap it in something we can recognize later in HandleUnhandledException
530+ throw new ValueConverterException ( ex . Message ) ;
518531 }
519532 }
520533 }
@@ -537,19 +550,27 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
537550
538551 public object ConvertBack ( object value , Type targetType , object parameter , CultureInfo culture )
539552 {
540- if ( value is string )
553+ try
541554 {
542- string s = ( string ) value ;
543- if ( string . IsNullOrWhiteSpace ( s ) )
555+ if ( value is string )
544556 {
545- return SqlDecimal . Null ;
557+ string s = ( string ) value ;
558+ if ( string . IsNullOrWhiteSpace ( s ) )
559+ {
560+ return SqlDecimal . Null ;
561+ }
562+ return new SqlDecimal ( System . Convert . ToDecimal ( value ) ) ;
546563 }
547- return new SqlDecimal ( System . Convert . ToDecimal ( value ) ) ;
548- }
549564
550- if ( value is decimal )
565+ if ( value is decimal )
566+ {
567+ return new SqlDecimal ( ( decimal ) value ) ;
568+ }
569+ }
570+ catch ( Exception ex )
551571 {
552- return new SqlDecimal ( ( decimal ) value ) ;
572+ // Need to wrap it in something we can recognize later in HandleUnhandledException
573+ throw new ValueConverterException ( ex . Message ) ;
553574 }
554575 return new SqlDecimal ( 0 ) ;
555576 }
@@ -571,19 +592,26 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
571592
572593 public object ConvertBack ( object value , Type targetType , object parameter , CultureInfo culture )
573594 {
574-
575- if ( value is string )
595+ try
576596 {
577- string stringVal = value as string ;
578-
579- decimal d = 0 ;
580- if ( ! string . IsNullOrWhiteSpace ( stringVal ) &&
581- false == decimal . TryParse ( stringVal , NumberStyles . Currency , CultureInfo . CurrentCulture , out d ) )
597+ if ( value is string )
582598 {
583- d = System . Convert . ToDecimal ( stringVal , CultureInfo . GetCultureInfo ( "en-US" ) ) ;
584- }
599+ string stringVal = value as string ;
600+
601+ decimal d = 0 ;
602+ if ( ! string . IsNullOrWhiteSpace ( stringVal ) &&
603+ false == decimal . TryParse ( stringVal , NumberStyles . Currency , CultureInfo . CurrentCulture , out d ) )
604+ {
605+ d = System . Convert . ToDecimal ( stringVal , CultureInfo . GetCultureInfo ( "en-US" ) ) ;
606+ }
585607
586- return d ;
608+ return d ;
609+ }
610+ }
611+ catch ( Exception ex )
612+ {
613+ // Need to wrap it in something we can recognize later in HandleUnhandledException
614+ throw new ValueConverterException ( ex . Message ) ;
587615 }
588616 return value ;
589617 }
0 commit comments