Custom date format in silverlight binding - silverlight

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.

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}" />

How to format the date string in GanttView from Telerik for WPF?

As the question says, I want to know how I can format the date string of the start or end column in a GanttView from Telerik for WPF.
I already tried this to options:
<t:ColumnDefinition MemberBinding="{Binding Path=Start, StringFormat='{}{0:dd-MM-yyyy}'}"
Width="AutoHeaderAndContent"/>
and:
<t:ColumnDefinition MemberBinding="{Binding Path=Start}"
Width="AutoHeaderAndContent">
<t:ColumnDefinition.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=FormattedValue, StringFormat='{}{0:dd-MM-yyyy}'}"/>
</DataTemplate>
</t:ColumnDefinition.CellTemplate>
Does anyone know how I could do this?
Thanks in advance!
For a normal TextBlock in WPF, this will work:
<TextBlock Text="{Binding Path=FormattedValue, StringFormat=dd-MM-yyyy}"/>
You can add time to it as well:
<TextBlock Text="{Binding Path=FormattedValue, StringFormat=dd-MM-yyyy - HH:mm:ss}"/>
But now that I am looking in the link you provided in the comments, it might be that you have to play with the DataFormatString in ColumnDefinition:
<t:ColumnDefinition MemberBinding="{Binding Path=Start}"
DataFormatString="dd-MM-yyyy" Width="AutoHeaderAndContent">
<t:ColumnDefinition.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=FormattedValue}"/>
</DataTemplate>
</t:ColumnDefinition.CellTemplate>
See the following examples:
https://docs.telerik.com/devtools/wpf/controls/raddatetimepicker/how-to/use-in-grid
https://docs.telerik.com/devtools/wpf/controls/radgridview/columns/data-formatting

Append default string to a label in WPF

I need to display Time inside a Li.e. '20 min'. My data is stored as integer so 'min' should be appended. Is there a way to append a default string 'min' to my bound value?
You can use Run like below
<TextBlock><Run Text="{Binding Time}"/><Run Text=" min"/></TextBlock>
You can simply use the Binding.StringFormat property to format or append additional information to your data bound value:
<TextBlock Text="{Binding Time, StringFormat={}{0} min}" />
When Time has a value of 25, this renders like so:
You can also try using ' marks, but you'll have to leave an initial space:
<TextBlock Text="{Binding Time, StringFormat=' {0} min'}" />
UPDATE >>>
Thanks to #Krishna for the following information:
To use the string format with a Label control, you must use the ContentStringFormat property instead:
<Label Content="{Binding Time}" ContentStringFormat="{}{0} min}" />

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 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