Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format a Label to have a Display Format String?

I have a Label:

<Label Name="lblBilledDate"
       Content="{Binding Path=BilledDate, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">
</Label>

It is bound to a DateTime value.

How can i change the label to display the value to this: DisplayFormatString="dd MMM yyyy"

Currently the Label just dispalys: 1/1/2010

I need it to dispaly: 1 Jan 2010

like image 848
Willem Avatar asked Mar 09 '11 17:03

Willem


People also ask

What is %s in string format?

%s specifically is used to perform concatenation of strings together. It allows us to format a value inside a string.

How can we create a format string in Java?

In java, String format() method returns a formatted string using the given locale, specified format string, and arguments. We can concatenate the strings using this method and at the same time, we can format the output concatenated string. Parameter: The locale value to be applied on the format() method.

What is data format string?

A date and time format string is a string of text used to interpret data values containing date and time information. Each format string consists of a combination of formats from an available format type. Some examples of format types are day of week, month, hour, and second.


2 Answers

Use the ContentStringFormat attribute.

<Label x:Name="SomeLabel"
       Content="{Binding BilledDate}"
       ContentStringFormat="dd MMM yyyy" />

This is because Label inherits from ContentControl. Any ContentControl contains the ContentStringFormat attribute. Additionally, ItemsControl has ItemStringFormat and BindingBase has StringFormat.

like image 120
user7116 Avatar answered Oct 24 '22 08:10

user7116


What about this one?

<Label name="lblSomeLabel">
    <Binding Path="Date" StringFormat="{}{0:dd MMM yyyyy}"/>
</Label>
like image 22
Vlad Bezden Avatar answered Oct 24 '22 09:10

Vlad Bezden