Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set binding stringformat to all DataGridTextColumn controls?

This is one of my DataGridTextColumn controls looks like :

<DataGridTextColumn x:Name="contractStartDateColumn" Header="Start Date" Binding="{Binding Path=StartDate, StringFormat={}\{0:dd/MM/yyyy\}}" />

Then how can I set StringFormat={}{0:dd/MM/yyyy} to all of DataGridTextColumn controls instead of setting every single one ?

like image 484
JatSing Avatar asked Jan 19 '23 04:01

JatSing


1 Answers

You can create custom binding class that sets StringFormat and use it to bind values:

public class CustomBinding : Binding
{
    public CustomBinding(string path) : base(path)
    {
        this.StringFormat = @"{0:dd/MM/yyyy}";
    }
}

And in XAML:

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding TimeList}">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{local:CustomBinding StartDate}" />
    </DataGrid.Columns>
</DataGrid>
like image 93
icebat Avatar answered Jan 21 '23 10:01

icebat