views:

57

answers:

1

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

+4  A: 

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

Tri Q
Standard Date/Time formatting has been in all the .Net versions, see http://msdn.microsoft.com/en-us/library/az4se3k1(VS.71).aspx (version 1.1)
HadleyHope
Is there a possibility to this in .NET 3.0, since I haven't migrated my project to 3.5 yet.
martin
Just make a string format converter and pass the formatting string as the converter parameter. Then you only need to make one converter for all of them and it's easy to update when/if you do move to 3.5sp1.
Bryan Anderson
If you're stuck with .Net 3.0 then the only way is to write custom converters. Consider moving to .Net 4.0 when its available and you'll be set =)
Tri Q