WPF C# String Format

Asked

Viewed 1,251 times

2

I have a usercontrol with the following configuration:

<UserControl x:Class="PainelPendencias.View.PendenciaConsulta"
             x:Name="uCPendenciaConsulta"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             xml:lang="pt-BR"
             d:DesignHeight="720"  d:DesignWidth="1024">

In a Datagrid column I do the stringformat below:

<DataGridTextColumn Header="Valor" Binding="{Binding OperacaoValor, StringFormat=N}" IsReadOnly="True">

The value received to be formatted is: 100543.67M.

The problem is that some machines are being shown correctly: 100,543.67 and others are adding zeros: 10,054,367.00.

[Update] On the machine with incorrect format I went to the regional settings and removed the addition of 2 decimal places [palliative]. But in the machines that work this addition exists.

2 answers

3


Have you tried other number formatting media? See more examples.

<DataGridTextColumn Header="Valor" Binding="{Binding OperacaoValor, StringFormat={}{0:#,#.00}}" IsReadOnly="True">
<DataGridTextColumn Header="Valor" Binding="{Binding OperacaoValor, StringFormat={0:#,0} {1:#,0}}" IsReadOnly="True">
<DataGridTextColumn Header="Valor" Binding="{Binding OperacaoValor, StringFormat=\{0:N0\}}" IsReadOnly="True">
<DataGridTextColumn Header="Valor" Binding="{Binding OperacaoValor, StringFormat={}{0:N0}}" IsReadOnly="True">

Response update

<DataGridTextColumn Header="Valor" Binding="{Binding OperacaoValor, StringFormat='#,##0.00', ConverterCulture='pt-BR'}" IsReadOnly="True">
  • The first 2 are not compiled, I get the following message: "Type 'not found. Check that there is no missing Assembly reference and that all referenced assemblies have been created.". The last two are thus formatting "2.065.000" a value that was to be like this "2.065.000,00". The value I passed in the question is coming out like this: 100,544

  • @Denis updated the answer, see if this format + force the culture to pt-BR is the way requested - with dot for thousands and comma decimally.

  • Perfect! Thanks to Ismael!!

1

Try Converterculture=en-BR like this:

<DataGridTextColumn Header="Valor" Binding="{Binding OperacaoValor, ConverterCulture=pt-BR}" IsReadOnly="True">

Browser other questions tagged

You are not signed in. Login or sign up in order to post.