80864

TextBox Won't Allow me to enter in a decimal

Question:

I can Enter Numbers into the text box but it won't allow me to enter decimal values. May I ask why is this?

<TextBox Text="{Binding SebAmountPer, Mode=Twoway, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" Grid.Row="2" TextWrapping="Wrap"/> public decimal? SebAmountPer { get { return _seb.SEBAmountPer; } set { _seb.SEBAmountPer = value; OnPropertyChanged("SebAmountPer"); OnPropertyChanged("SebTotal"); } }

Answer1:

A cheap work-around (if you still want to keep the built-in validation and binding to a nullable property), is to add a small Delay within the binding. This allows you to actually enter in a 'decimal' point, and after the 'delay' it binds, then evaluates the value as correct.

Example:

<TextBox Text="{Binding SebAmountPer, UpdateSourceTrigger=PropertyChanged, TargetNullValue='', Delay=350}" Height="75" Width="300" TextWrapping="Wrap"/>

Answer2:

You have a two-way binding to a numeric datatype. The trigger is <em>property change</em> which means after every key stroke. Try binding to a string first or change update trigger.

You enter "2." and he updates the binding to 2.0 and converts it back and just steals away your dot ;)

Answer3:

If you have .NET 4.5 or newer add to the App.xaml.cs file System.Windows.FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false; like shown below:

public partial class App : Application { public App() { System.Windows.FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false; } }

Answer4:

It is binding value validation issue, you just need to set

UpdateSourceTrigger=LostFocus

and in the TextBox_PreviewTextInput use 123 456 789 0's answer:

private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e) { Regex regex = new Regex("^[.][0-9]+$|^[0-9]*[.]{0,1}[0-9]*$"); e.Handled = !regex.IsMatch((sender as TextBox).Text.Insert((sender as TextBox).SelectionStart,e.Text)); }

Answer5:

This behavior is perfectly correct because the dot - "." standing alone does not have any meaning as a digit.

<strong>Following are valid digits:</strong> 1, 3.3, 23.124, 1., 23.0

<strong>Following are invalid digit:</strong> . -->(i meant dot), .12

so use the following code if you don't want to do that.

private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e) { Regex regex = new Regex("^[.][0-9]+$|^[0-9]*[.]{0,1}[0-9]*$"); e.Handled = !regex.IsMatch((sender as TextBox).Text.Insert((sender as TextBox).SelectionStart,e.Text)); }

Recommend

  • Tracking property changes with PostSharp
  • Inverse Relationships and UI-Update not working?
  • How to use ObservableCollection with Fluent nHibernate?
  • Binding MedialElement from the view (xaml) to a ViewModel's property
  • x:Bind and nested class in a UWP application
  • Incremental Loading and snapped visual state
  • How does Task Parallel Library scale on a terminal server or in a web application?
  • Audio Sound Too Low in Android App
  • Dijkstra with a heap. How to update the heap after relaxation?
  • Is it faster to create a new object reference if it will only be used twice?
  • Bind an ObservableCollection to Lines
  • Xamarin Forms Disposed Textview
  • How to mock springSecurityService in unit tests using grails
  • Mapping ManyToMany with composite Primary key and Annotation:
  • UITableView takes much longer to load when numberOfRows returns a large number
  • Return null in boolean to checkbox state converter in XAML
  • Multiple Left Join LINQ-to-entities
  • countdown bar android example
  • Consuming a WCF service in a Java Client using wsHttpBinding
  • How to pass nginx proxy url for socket
  • Force show.bind execution
  • SharedPreferences or SQLite Database?
  • Database structure design with variable amounts of fields
  • Get one-time binding to work for ng-if
  • Update CALayer sublayers immediately
  • Using $this when not in object context
  • Android screen density dpi vs ppi
  • Uncaught Error: Could not find module `ember-load-initializers`
  • How do I fake an specific browser client when using Java's Net library?
  • How to get a value (ex: baseURL) in every Karate feature?
  • How would I use PHP exceptions to define a redirect?
  • How to extract text from Word files using C#?
  • QuartzCore.framework for Mono Develop
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • need help with bizarre java.net.HttpURLConnection behavior
  • Android Google Maps API OnLocationChanged only called once
  • Checking variable from a different class in C#
  • Programmatically clearing map cache
  • UserPrincipal.Current returns apppool on IIS