Reputation: 630
Quick(I hope) question. I've got this DataGrid, containing a few currency column's. What I can't seem to find is how to display the currency in 2 decimals. But maintaining a possible 3 or 4 decimal value in the cell. If tried some string-formatting but that actually changes the value. And so does a IValueConverter of course. So what am I missing? or could anybody 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'd 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 Dont Get is WHY! are the value's in those columns nice and red text, blue background.. but unformated string, just shows 0,000 in stead of €0,00
Upvotes: 3
Views: 6979
Reputation: 132548
Using StringFormat
or a one-way Converter
should not change the actual value. Both of those are only used for display purposes.
For example, this will show the decimal value in currency format with 2 decimal places, but it will not trim SomeDecimal
to two-decimal places
<TextBox Text="{Binding SomeDecimal, StringFormat=C2}" />
Perhaps you are looking to display a value using 2 digits, but edit it using the full 4 digits. If this is the case, I would suggest using a Style Trigger to show the unformatted value while it's being edited.
<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
Per your request below to apply this style to a DataGridCell
, you can either use a DataGridTemplateColumn
and place a TextBox inside it with this style applied, or you can use the ElementStyle
and EditingElementStyle
properties to 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>
Upvotes: 6
Reputation: 203802
My first instinct is to just have a string with two decimals and use whatever structure you use to populate the datagrid if you need the full precision.
If you really need both values accessible from the DataGrid, I'd create two columns, one that's a decimal and has full precision, and another that's a string with two decimals. You can show/hide the columns as needed so that the appropriate version is displayed at the appropriate time.
Upvotes: -1