Hodaya Shalom
Hodaya Shalom

Reputation: 4417

Set format to the data that presented in DataGrid -WPF

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

Answers (2)

Colin Smith
Colin Smith

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

Andrey Gordeev
Andrey Gordeev

Reputation: 32449

You can set StringFormat in XAML:

<TextBox Text="{Binding paymentAmount, StringFormat={0:C2}}"/>

Here is a great article about that

Upvotes: 1

Related Questions