title: Unable to enter "." in a text box with decimal binding
body: <p>I have a textbox bound to a decimal value (property) and when I type the &quot;3.&quot; it does not allow me to enter as it parses it back to 3 and hence I lose the &quot;.&quot;. I tried the solutions like Delay/LostFocus etc but that did not work for me, from <a href="https://stackoverflow.com/questions/21004951/wpf-validation-rule-preventing-decimal-entry-in-textbox">WPF validation rule preventing decimal entry in textbox?</a>.</p>
  <p>I have now written a IValueConverter class and I am obviously not doing it correctly. Here is the code:</p>
  <pre><code>[ValueConversion(typeof(decimal), typeof(string))]
  public class DecimalToStringConverter : IValueConverter
  {
      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
          //return Decimal.Parse(value.ToString());
          if (value == null) return Decimal.Zero;
          if (!(value is string)) return value;
          string s = (string)value;
          int dotCount = s.Count(f =&gt; f == '.');
          Decimal d;
          bool parseValid = Decimal.TryParse(s, out d);
          System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(&quot;^[-+]?([0-9]*?[.])?[0-9]*$&quot;);
          bool b = regex.IsMatch(s);
          if (dotCount == 1 &amp;&amp; b)
          {
              return s;
          }
          return d;
      }
  
      public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
          return value != null ? value.ToString() : &quot;&quot;;
      }
  }
  </code></pre>
  <p>XAML</p>
  <pre><code>&lt;TextBox x:Name=&quot;ValueTextBox&quot; Grid.Column=&quot;0&quot; Grid.RowSpan=&quot;2&quot; VerticalContentAlignment=&quot;Center&quot; Text =&quot;{Binding Value, Converter={StaticResource DecimalToStringConverter}}&quot; PreviewTextInput=&quot;ValueTextBox_OnPreviewTextInput&quot;  TextWrapping=&quot;Wrap&quot; MouseWheel=&quot;ValueTextBox_MouseWheel&quot; PreviewKeyDown=&quot;ValueTextBox_PreviewKeyDown&quot; PreviewKeyUp=&quot;ValueTextBox_PreviewKeyUp&quot; TextChanged=&quot;ValueTextBox_TextChanged&quot; BorderThickness=&quot;1,1,0,1&quot;/&gt;
  </code></pre>
  <p>Please advise how I can correct this...
  Thanks</p>