Display currency in two decimal places, but keep all available decimal places

A quick (hopefully) question. I have this DataGrid containing multiple currency columns. I can't seem to find how to display the currency in 2 decimal places. But maintaining a possible 3 or 4 decimal value in a cell. If you tried some string formatting but that actually changed the meaning. And, of course, IValueConverter. So what am I missing? or can anyone point me in the right direction?

EDIT: After the suggestion, I tried something like this.

 <Style x:Key="DecimalTextBlockStyle" TargetType="{x:Type DataGridCell}">
        <Setter Property="TextBlock.Text" Value="{Binding StringFormat={}{0:C}}"/>
        <Setter Property="TextBlock.Foreground" Value="Red"/>
        <Setter Property="Background" Value="AliceBlue"/>
    </Style>

      

and apply here:

if(e.PropertyType.FullName.Contains("Decimal"))
        {
            var cl = e.Column as DataGridTextColumn;
            if (cl != null)
            {
                //cl.Binding.StringFormat = "C2";
                Style Stl = (Style)FindResource("DecimalTextBlockStyle");
                cl.CellStyle = Stl;

            }}

      

But what I don't get, WHY! this value in these columns is nice and red text, blue background .. but unformatted string, just shows 0.000 instead of € 0.00

+3


source to share


2 answers


Use StringFormat

or one-way Converter

should not change the actual value. Both are used for display purposes only.

For example, this will show the decimal value in currency format with two decimal places, but it will not truncate SomeDecimal

to two decimal places

<TextBox Text="{Binding SomeDecimal, StringFormat=C2}" />

      

You may want to display the value using 2 digits, but edit it using 4 digits. If so, I would suggest using a stylish trigger to show the unformatted value while you edit it.



<Style x:Key="DecimalTextBoxStyle" TargetType="{x:Type TextBox}">
    <Setter Property="Text" Value="{Binding SomeDecimal, StringFormat=C2}" />
    <Style.Triggers>
        <Trigger Property="IsKeyboardFocusWithin" Value="True">
            <Setter Property="Text" Value="{Binding SomeDecimal}" />
        </Trigger>
    </Style.Triggers>
</Style>

      

Edit

As per your request below, to apply this style to DataGridCell

, you can use DataGridTemplateColumn

and place a TextBox with this style in it, or you can use properties ElementStyle

and EditingElementStyle

for set the binding format

<DataGridTextColumn>
    <DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Text" Value="{Binding SomeDecimal, StringFormat=C2}" />
        </Style>
    </DataGridTextColumn.ElementStyle>
    <DataGridTextColumn.EditingElementStyle>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Text" Value="{Binding SomeDecimal}" />
        </Style>
    </DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>

      

+6


source


My first instinct is to just have a two decimal place string and use whatever structure you use to populate the datagrid if you want full precision.



If you really want both of the values ​​available from the DataGrid, I would create two columns, one being a decimal and full precision, and the other a string with two decimal places. You can show / hide columns as needed so that the corresponding version is displayed at the appropriate time.

-1


source







All Articles