TextBox masks values ​​from 2 to 4 decimal places in WPF C#

Question:

I would like my TextBox to have the following "mask" When I put two places after the comma:

0.00

or when I put three:

0.000

or when I put 4:

0.0000

My code in WPF C# looks like this:

 <TextBox  x:Name="ValorProdutoTextBox"  
  Text="{Binding ValorProduto, NotifyOnValidationError=true,
  ValidatesOnExceptions=true,  ConverterCulture='pt-BR'}"
  UpdateSourceTrigger="ValorLostFocus"
   Height="20"
   Width="100" Margin="1" />

The result is always coming as determined by the 'Binding ValorProduto' that is in the database (DECIMAL 15.4)

0.0000

Answer:

Here is the code example:

<TextBox Text="{Binding Value, StringFormat=N2}" />
<TextBox Text="{Binding Value, StringFormat={}{0:#,#.00}}" />

Original answer: StackOverFlow English

Scroll to Top