Lalit_vicky
Lalit_vicky

Reputation: 295

WPF Textbox max length for fractional value

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?

Upvotes: 1

Views: 424

Answers (1)

markyxl
markyxl

Reputation: 81

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.

Upvotes: 3

Related Questions