Jul Pod Jul Pod - 1 year ago 50
C# Question

Strange Auto-Date Format Datagrid

I got this XAML for my Datagrid Column:

<DataGridTemplateColumn SortMemberPath="LiefDat" Width="110" x:Name="columnLieferDatum" >
<DataGridTemplateColumn.Header >
<Grid>
<TextBlock Text="Lieferdatum"/>
</Grid>
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Width="110" x:Name="lblLiefDat" Content="{Binding LiefDat, StringFormat=\{0:dd.MM.yy HH:mm:ss\}}"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=DataGridRow}}"
Value="True">
<Setter TargetName="lblLiefDat" Property="Foreground" Value="white" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>


I read out the data from a XML which got this field in a format like: 2001-02-16T00:00:00+01:00

The Datagrid auto converts this value into: 2.16.2001, how i change this to 16.2.2001, already tried this

EDIT

With the tip of @Dominic Jonas i got the solution:

Content="{Binding LiefDat}" ContentStringFormat="dd.MM.yyyy"


Adding it like this in the XAML was the right way

Answer Source

If you set

StringFormat=\{0:dd.MM.yy HH:mm:ss\}

then a hard coded string format is given. Try it with:

StringFormat=d

Then the format is dynamicly generated. Here is a small list with examples: http://www.csharp-examples.net/string-format-datetime/

But that requires you to use a global language setting (like you already did).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download