Do I always have to write a converter for date-formatting?

converters, data-binding, format, wpf, xaml

Solution

.NET 3.5 SP1 has StringFormatter.

<TextBox Name="Date" Text="{Binding Date, StringFormat='{}{0:MM/dd/yyyy}'}"/>

Result: 02/02/2010

<TextBox Name="Date" Text="{Binding Date, StringFormat='{}{0:D}'}"/>

Result: Tuesday, February 02, 2010

But result may also vary depending on system default DateTime format.

Problem

I often use textboxes in my wpf-projects which are bound to datetime-propertys. I want to format the dates into the german format `dd.MM.yyyy`. Currently I do this with a self-written converter, which I can give the needed dateformat. For example like this: ``` <TextBox Name="Date" Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type prj:MyBar}}, Path=Date, Converter={StaticResource dateConverter}, ConverterParameter=dd.MM.yyyy}" /> ``` The only thing the converter does, is to call the `ToString(string formatString)`-method of DateTime. Is there a 'smarter' way to format a date with data-binding. The best would be if there is no need to write C#-code. Perhaps there is any existing class in the microsoft-libs, that could do such date-conversion for data-binding, but I did not found it yet. Would be great if there are any advices, greetings, Martin

Original source