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
.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With