title: Display an error if a latitude or longitude has improper formatting
body: <p>My WPF application has a dialog where the user can enter a latitude &amp; a longitude.  I have written an <code>IValueConverter</code> which can handle values entered as decimal degrees or in degree-minute-second format:</p>
  
  <pre><code>[ValueConversion( typeof( double? ), typeof( string ) )]
  public class CoordinateConverter : IValueConverter {
  
      #region IValueConverter Members
  
      public object Convert( object value, Type targetType, object parameter, CultureInfo culture ) {
          double angle = 0.0;
  
          if ( value is string ) {
              if ( !double.TryParse( value as string, NumberStyles.Float, CultureInfo.InvariantCulture, out angle ) ) {
                  return value;
              }
  
          } else if ( value is double || value is double? ) {
              angle = (double) value;
  
          } else {
              return value.ToString();
          }
  
          bool isNegative = angle &lt; 0;
          if ( isNegative ) angle = -angle;
  
          double degrees   = Math.Truncate( angle );
          double remainder = ( angle - degrees ) * 60.0;
          double minutes   = Math.Truncate( remainder );
          double seconds   = ( remainder - minutes ) * 60.0;
  
          string result = degrees.ToString( "##0", culture.NumberFormat ) + "° " +
                          minutes.ToString( "#0", culture.NumberFormat ) + "' " +
                          seconds.ToString( "#0.00", culture.NumberFormat ) + "\" ";
  
          // The parameter contains "NS" for Latitudes and "EW" for Longitudes.
          if ( parameter != null ) {
              result += ( (string) parameter ).Substring( ( isNegative ? 1 : 0 ), 1 );
          } else {
              result = ( isNegative ? "-" : string.Empty ) + result;
          }
  
          return result;
      }
  
      public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture ) {
          string strValue = value as string;
  
          if ( string.IsNullOrEmpty( strValue ) ) {
              return null;
          }
  
          double adjustForSign = 1.0;
          if ( strValue.IndexOf( "-" ) &gt;= 0 ) {
              adjustForSign = -1.0;
              strValue = strValue.Substring( strValue.IndexOf( "-" ) + 1 );
          }
  
          // Parse the value in the field.  It's in three parts:  Degrees, minutes &amp; seconds
          int degreeSymbol = strValue.IndexOf( "°" );
          int minuteSymbol = strValue.IndexOf( "'" );
          int secondSymbol = strValue.IndexOf( '"' );
  
          string degrees = null, minutes = null, seconds = null;
          double angle, d, m, s;
  
          if ( degreeSymbol &lt; 0 ) {
              if ( double.TryParse( strValue, NumberStyles.Number, culture.NumberFormat, out angle ) ) {
                  return angle;
              } else {
                  return value;
              }
  
          } else {
              degrees = strValue.Substring( 0, degreeSymbol );
  
              if ( minuteSymbol &gt;= 0 ) {
                  minutes = strValue.Substring( degreeSymbol + 2, minuteSymbol - degreeSymbol - 2 );
              }
              if ( secondSymbol &lt; 0 ) {
                  seconds = "0" + culture.NumberFormat.NumberDecimalSeparator + "0";
              } else {
                  seconds = strValue.Substring( minuteSymbol + 2, secondSymbol - minuteSymbol - 2 );
              }
          }
  
          if ( !double.TryParse( degrees, NumberStyles.Integer, culture.NumberFormat, out d ) ) return value;
          if ( !double.TryParse( minutes, NumberStyles.Integer, culture.NumberFormat, out m ) ) return value;
          if ( !double.TryParse( seconds, NumberStyles.Float  , culture.NumberFormat, out s ) ) return value;
          angle = d + m / 60.0 + s / 3600.0;
  
          if ( parameter != null ) {
              if ( strValue.Contains( ( (string) parameter ).Substring( 1, 1 ) ) ) {
                  angle = -angle;
              }
          } else {
              angle *= adjustForSign;
          }
          return angle;
      }
  </code></pre>
  
  <p>In the dialog, I'm using this <code>ControlTemplate</code> to display errors:</p>
  
  <pre><code>&lt;ControlTemplate x:Key="InputErrorTemplate"&gt;
      &lt;DockPanel LastChildFill="True"&gt;
          &lt;Image DockPanel.Dock="Right"
                 Height="20"
                 Margin="-30,0,0,0"
                 Source="{StaticResource ErrorImage}"
                 ToolTip="{x:Static res:Car.Common_InvalidData}"
                 VerticalAlignment="Center"
                 Width="20" /&gt;
          &lt;Border BorderBrush="Red"
                  BorderThickness="5"
                  Margin="5,0,30,0"&gt;
              &lt;AdornedElementPlaceholder /&gt;
          &lt;/Border&gt;
      &lt;/DockPanel&gt;
  &lt;/ControlTemplate&gt;
  </code></pre>
  
  <p>If one of the <code>TextBoxes</code> has a string in it like , it doesn't parse, no exceptions are thrown, and my dialog's error template is displayed.  When I hover the mouse over the <code>TextBox</code>, the error message displayed is "Input string is not in a correct format"</p>
  
  <p>I have a couple of questions:</p>
  
  <ol>
  <li>Where is that error message coming from?  It's not in my code and if the converter throws an exception, the program dies as it's unhandled.  </li>
  <li>In the end, I want to display an error if the value in the <code>TextBox</code> can't be parsed.  The dialog's View Model object does implement <code>IDataErrorInfo</code>, but the conversion from string to double isn't being done by that object.  How do I make that happen?</li>
  </ol>