Reputation: 4417
I want to set format to columns of the DataGrid in xaml, the columns contain double data.
Until now I did it in the code as follows:
string format = "{0:F3}";
double nExm= 4.24;
string newExm= string.Format(format, nExm);
The DataGrid:
<DataGrid x:Name="DG" ItemsSource="{Binding List}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="{x:Static p:Resources.Exm}" Binding="{Binding Exm}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
newExm it varies that is included within the class X that contains the list that binding to the DataGrid
X Class:
private string exm;
public string Exm
{
get { return exm; }
set
{
exm= value;
NotifyPropertyChanged("Exm");
}
}
Is there a way to set the format via XAML? (I want to turn variable to double and set the format on columns)
Upvotes: 1
Views: 2358
Reputation: 12530
You can use StringFormat
on your Binding
to format it:
Binding="{Binding YourDoublePropertyHere, StringFormat=F3}"
If StringFormat
doesn't give you all your formatting needs (in this case it should be adequate), or you have more sophisticated conversion logic then you could write a Converter
to massage the data.
Upvotes: 1
Reputation: 32449
You can set StringFormat
in XAML:
<TextBox Text="{Binding paymentAmount, StringFormat={0:C2}}"/>
Here is a great article about that
Upvotes: 1