WPF format DateTime in TextBlock? - wpf

I have a TextBlock that is bound to a DateTime property. How do I configure the format of the date?

There is a string format property available when you are declaring the binding:
<TextBox Text="{Binding Path=DateTimeValue, StringFormat=dd-MM-yyyy}" />
(You need to be on .NET 3.5 SP1 for this property to exist)

If you want to use a common format string between bindings, you could declare the binding like this:
<Textbox Text={Binding Path=DateTimeValue, StringFormat={x:Static local:Constants.DateTimeUiFormat}} />
With your constants class like this:
public static class Constants
{
public const string DateTimeUiFormat = "dd/MM/yyyy";
//etc...
}

May be helpful 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 month and year format:
<TextBlock Text="{Binding Source={x:Static sys:DateTime.Now},
StringFormat='{}{0: Today is dddd, MM.dd.yy, HH:mm:ss}'}"/>

Related

Stringformat WPF with Date and additional string

I want to format a Datetime in my textbox like this:
08.05.2018 12:18 Uhr
Text="{Binding CreationDate, StringFormat=dd.MM.yy hh:mm}"
This is what I've got until now, but the "Uhr" is missing.
This worked for me
<TextBlock Text="{Binding MyDate,StringFormat=dd.MM.yyyy hh:mm U\\hr}" />

date string format binding not working

I am trying to get date format as "MM/dd/yyyy", however, the current date format is coming with time too, I tried to format it with the following codes, however it is not changing, could you please correct me as follows:
<DataGridTextColumn Header="Bill Date" Binding="{Binding BillDate, StringFormat={}{0:MM/dd/yyyy}}" Width="90" IsReadOnly="True" />
and
<DataGridTemplateColumn Header="Bill Date" Width="90">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding BillDate}" ContentStringFormat="MM/dd/yyyy" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
my property code is as follows:
Property BillDate As Date
Get
Return _BillDate
End Get
Set(Value As Date)
If Not _BillDate = Value Then
_BillDate = Value
NotifyPropertyChanged("BillDate")
End If
End Set
End Property
I dont want to correct at property level, I only want to display the format at XAML, the above format is not working although it is correct to my knowledge.
EDIT:
After many trails and few experients, I came to know that the solution is not building any new code. So, the problem now is - new, modified xaml is not getting built. Could you please help me in this.
Try
"{Binding BillDate, StringFormat=d}"
which should give you month/day/year as output.
Update
The control Label's Content property is of type object and does not apply formatting like a string does. Here are your options,
Use the ContentStringFormat property on the label:
<Label ContentStringFormat="d">
<system:DateTime>2015/3/4 13:6:55</system:DateTime>
</Label>
Or the TextBox control, the Text property is a string and it takes formatting parameters.
The type of your property should be DateTime, not Date:
Property BillDate As DateTime
...
End Property
Then this works:
<Label Content="{Binding BillDate}" ContentStringFormat="MM/dd/yyyy"/>
But you might perhaps better use a TextBlock instead of a Label:
<TextBlock Text="{Binding BillDate, StringFormat=MM/dd/yyyy}"/>

Custom date format in silverlight binding

Trying to format a datetime bound value in my XAML using Silverlight 5 like so:
<TextBlock Text="{Binding ContactDate, Mode=OneWay, StringFormat={}{0:'dd/MM/yyyy'}}" Margin="5,0" />
I'm getting the following error:
Unexpected Token after end of Markup Extension.
This is driving me insane!
Try:
Text="{Binding ContactDate, Mode=OneWay, StringFormat='{}{0:dd/MM/yyyy}'}"
Unless you mean that you want singlequotes before and after the date string.

WPF: What is the correct way to format a dependencyproperty as string to display in a textblock?

This is a follow up based on the answer of a previous question. I've managed to come up with a DependencyProperty which will be updated using a Timer to always have the latest date time, and a textblock which shows the datetime. Since it's a DependencyProperty, whenever the timer updates the value, the textblock will show the latest DateTime as well.
The dependency object
public class TestDependency : DependencyObject
{
public static readonly DependencyProperty TestDateTimeProperty =
DependencyProperty.Register("TestDateTime", typeof(DateTime), typeof(TestDependency),
new PropertyMetadata(DateTime.Now));
DispatcherTimer timer;
public TestDependency()
{
timer = new DispatcherTimer(new TimeSpan(0,0,1), DispatcherPriority.DataBind, new EventHandler(Callback), Application.Current.Dispatcher);
timer.Start();
}
public DateTime TestDateTime
{
get { return (DateTime)GetValue(TestDateTimeProperty); }
set { SetValue(TestDateTimeProperty, value); }
}
private void Callback(object ignore, EventArgs ex)
{
TestDateTime = DateTime.Now;
}
}
The Window Xaml
<Window.DataContext>
<local:TestDependency/>
</Window.DataContext>
<Grid>
<TextBlock Text="{Binding TestDateTime}" />
</Grid>
This works very well, but i'm wondering, what do I have to do if I wanna format the time string in a different way, is there a way to call a ToString(formatter) on the date time before displaying it in the textblock, while maintaining the ability to auto update the textblock using the DependencyProperty? What is the correct way to do this in code behind, and the correct way to do this in Xaml, if it's even possible?
And, if I have Multiple textboxes to show, each with a different date time format, what is the proper way to make use of only 1 timer to display all the different date time formats in different textboxes, do I have to create a DependencyProperty for each single format?
You can use string format for this:
<Window.DataContext>
<wpfGridMisc:TestDependency/>
</Window.DataContext>
<StackPanel>
<TextBlock Text="{Binding TestDateTime, StringFormat=HH:mm:ss}"/>
<TextBlock Text="{Binding TestDateTime, StringFormat=MM/dd/yyyy}"/>
<TextBlock Text="{Binding TestDateTime, StringFormat=MM/dd/yyyy hh:mm tt}"/>
<TextBlock Text="{Binding TestDateTime, StringFormat=hh:mm tt}"/>
<TextBlock Text="{Binding TestDateTime, StringFormat=hh:mm}"/>
<TextBlock Text="{Binding TestDateTime, StringFormat=hh:mm:ss}"/>
</StackPanel>
Also i think you should use SetCurrentValue() when updating the DependencyProperty, you can read why here
private void Callback(object ignore, EventArgs ex)
{
SetCurrentValue(TestDateTimeProperty, DateTime.Now);
}
Binding has a StringFormat property:
<TextBlock Text="{Binding TestDateTime, StringFormat=HH:mm:ss}"/>
See also Standard Date and Time Format Strings and Custom Date and Time Format Strings.

Custom DateTime stringformat in WPF

I can't get my custom DateTime string format to work in my binding. I want the format to be "mmmm, yyyy" (e.g. "June, 2012").
The following does not work. I get a short date format (m/d/yyyy).
<TextBlock Text="{Binding ElementName=ThisWindow,
Path=Date,
StringFormat={}{0:MMMM\, yyyy}"/>
I've considered using a converter, but I prefer a pure XAML approach.
Edit:
For clarity, I have a Window with a dependency property Date of type DateTime. In my XAML, I've named the window 'Thiswindow'.
Edit 2:
I looked back at my actual code, and I had a Label, not a TextBlock. I changed it to TextBlock and it works fine.
<Label Content="{Binding ElementName=ThisWindow,
Path=Date,
StringFormat={}{0:MMMM\, yyyy}"/>
Anyone know why it doesn't work with Label?
Thanks.
ContentControls have a ContentStringFormat property which overrides the original formatting.
(When i saw your question i expected this to be the problem actually but was surprised to find a TextBlock at first)
Your month needs to be in uppercase:
{Binding Source={x:Static sys:DateTime.Now}, StringFormat={}{0:MMMM\, yyyy}}
EDIT:
The Label problem is probably because Label has Content, not Text.
Change the Text="{Binding ...}" to Content="{Binding ...}"

Resources