Should I always write a converter to format the date?

I often use text fields in my wpf projects that are bound to datetime properties. I want to format dates in German dd.MM.yyyy format. I am currently doing this with a converter recorder, which I can provide the desired date format.

For example, for example:

 <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 call the ToString(string formatString) DateTime method.

Is there a smarter way to format dates with data binding. It would be best if you do not need to write a C # code. Perhaps there is some existing class in microsoft-libs that could do such a date conversion for data binding, but I haven't found it yet.

It would be great if there were any tips,

Hi, Martin

+4
source share
1 answer

.NET 3.5 SP1 has a 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 the result may also vary depending on the default DateTime format.

+9
source

Source: https://habr.com/ru/post/1300042/


All Articles