WPF DateTime format in TextBlock?

I have a TextBlock associated with a DateTime property. How to set date format?

+55
formatting wpf textblock
Aug 26 '09 at 7:58
source share
3 answers

When declaring a binding, a string format property is available:

 <TextBox Text="{Binding Path=DateTimeValue, StringFormat=dd-MM-yyyy}" /> 

(You must be on .NET 3.5 SP1 for this property to exist)

+113
Aug 26 '09 at 8:02
source share

If you want to use a common format string between links, you can declare a binding as follows:

 <Textbox Text={Binding Path=DateTimeValue, StringFormat={x:Static local:Constants.DateTimeUiFormat}} /> 

With your const class as follows:

 public static class Constants { public const string DateTimeUiFormat = "dd/MM/yyyy"; //etc... } 
+26
Sep 26
source share

May be useful to someone:

 <TextBlock Text="{Binding Source={x:Static sys:DateTime.Now}, StringFormat='{}{0: Today is dddd, MMMM dd, yyyy, hh:mm:ss}'}"/> 

or 24h and 2digits format of the month and year:

 <TextBlock Text="{Binding Source={x:Static sys:DateTime.Now}, StringFormat='{}{0: Today is dddd, MM.dd.yy, HH:mm:ss}'}"/> 
+16
Sep 20 '16 at 11:47
source share



All Articles