title: Check if Hex string is correct in Converter
body: <p>I made a text box to input and show color Hex value.The binding is twoway to a color property of a parent.</p>
  
  <p>Everything is working but, I need to make sure, in case I enter manually a Hex in the text box, and if this a not correct string, then use and display the current Hex value of the color, rather than trying to change it.</p>
  
  <p>Here is what I tried but obviously it's not working, I'm a beginner and I have only a little experience with converter and WPF. If I write anything but not a valid Hex string, at the moment the textbox gets a red outline, but I wish that in this case, the Hex previous string reappears.</p>
  
  <pre><code>[ValueConversion(typeof(Color), typeof(String))]
  public class ColorToStringConverter : IValueConverter
  {
      public Object Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
      {
          Color colorValue = (Color)value;
          return ColorNames.GetColorName(colorValue);
      }
      public Object ConvertBack(Object value, Type targetType, Object parameter, CultureInfo culture)
      {
          throw new NotImplementedException();
      }
  }
  
  public class ColorHexConverter : IValueConverter
  {
      public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
      {
          var hexCode = System.Convert.ToString(value);
  
          //if (string.IsNullOrEmpty(hexCode))
          //    return null;
          try
          {
              var color = (Color)ColorConverter.ConvertFromString(hexCode);
              return color;
          }
          catch
          {
              return null;
          }
      }
      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
      {
          var hexCode = System.Convert.ToString(value);
          Regex myRegex = new Regex("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$");
          bool isValid = false;
          if (string.IsNullOrEmpty(hexCode))
          {
              isValid = false;
          }
          else
          {
              isValid = myRegex.IsMatch(hexCode);
          }
  
          try
          {
              return hexCode;
          }
          catch
          {
              return null;
          }
      }
  }
  </code></pre>
  
  <p>And the C# class for the TextBox</p>
  
  <pre><code>public class ColorHex : TextBox
  {
      protected override void OnKeyDown(KeyEventArgs e)
      {
          base.OnKeyDown(e);
  
          if (e.Key == Key.Enter)
          {
              BindingExpression bindingExpression = BindingOperations.GetBindingExpression(this, TextProperty);
              if (bindingExpression != null)
                  bindingExpression.UpdateSource();
          }
      }
  }
  </code></pre>
  
  <p>And its xaml in Generic.xaml</p>
  
  <pre><code>&lt;local:ColorHex x:Name="PART_ColorHex" Style="{StaticResource ColorPickerTextBox}" Text="{Binding SelectedColor, Converter={StaticResource ColorToHexConverter},  RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ColorPicker}}}" /&gt;
  </code></pre>
  
  <p>Any idea ?</p>
  
  <p>thank you</p>