WPF Textbox max length for fractional value

371 views Asked by At

I have a textbox in wpf and want to restrict it to enter max 6 digits before decimal and only two digits after decimal. eg 123456.25. I know MaxLength is there but how to handle the fractional part? Any suggestion pls?

1

There are 1 answers

0
markyxl On

If you are using DataBinding for the TextBox content, it can be simply achieved by specifying StringFormat to your binding, for instance:

<TextBox Text="{Binding YourProperty, StringFormat=\{0:F2\}}"/>

See number formatting documentation at MSDN.

If you're not using DataBinding, I'm sure someone else will provide an answer for you.