Jul Pod
Jul Pod

Reputation: 396

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

Upvotes: 0

Views: 71

Answers (2)

Dominic Jonas
Dominic Jonas

Reputation: 5005

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).

Upvotes: 1

ivica.moke
ivica.moke

Reputation: 1064

You can do something like this. Make DateTimeConvertor, something like:

public class DateTimeConvertor : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var dateTime = (DateTime?)value;

        if (dateTime.HasValue)
        {
            return dateTime.Value.ToString("dd.MM.yyyy");
        }
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

introduce it to Window/UserControlResources:

<Window.Resources>
    <local:DateTimeConvertor x:Key="DateTimeConvertor" />
</Window.Resources>

and use it like this:

<Label x:Name="lblLiefDat"
       Content="{Binding LiefDat, Converter={StaticResource DateTimeConvertor}}"
       Margin="204,259,54,0" />

It works just fine on my side. I hope it helps.

For LiefDat(property named as in your case) i have made this, just to show purpose.

public DateTime LiefDat { get; set; }
public TestVM()
{
    LiefDat = DateTime.Now;
}

Upvotes: 1

Related Questions