Append default string to a label in WPF - 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}" />

Related

Why is AccessText not used when using ContentStringFormat?

When using a string value as Content of a ContentControl (like Label), using an _ inside the string allows using the following letter as access key, like this:
<Label Content="Text with _access key"/>
or even
<Label Content="{Binding Text}"/>
if Text is a string property containing an _.
However, when using ContentStringFormat which contains the _, it doesn't work any more:
<Label Content="{Binding Value}" ContentStringFormat="_Formatted value {0}"/>
I have seen in the debugger that no AccessText is used in this case.
As a workaround, I have used AccessText explicitly:
<Label>
<AccessText Text="{Binding Value, StringFormat=_Formatted value {0}"/>
</Label>
It works that way but I still want to know why it doesn't when using ContentStringFormat.
Maybe This topic will give you some insight about the AccesText.

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

Is it possible to conditionally display some text using the StringFormat attribute?

I have a nullable field in the database, called Generation. It specifies things like "Jr.", "II" and so on. I want a mean of conditionally specifying the generation of the client, if it isn't null, otherwise not displaying it at all. I thought that the following would work:
<TextBlock Text="{Binding LastName}" />
<TextBlock Text="{Binding Generation, StringFormat= {0}}" />
<TextBlock Text=", " />
However I'm getting an error saying that "0 is not supported in a Windows Presentation Foundation (WPF) project". The field value Generation is a varchar field. Can I do what I want with the StringFormat attribute of the TextBlock class, or do I need to use a converter?
You need to write this as:
<TextBlock Text="{Binding Generation, StringFormat={}{0}}" />
This is due to the nature of using the Markup Extension, which gives special meaning to {}. By adding this at the beginning (when you have no text before your first format specifier), it has the effect of "escaping" the string format specification for you, similar to how # is used to handle string literals in C#.

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.

XAML text binding

I would bind a string property to text property like this: Text="{Binding propertyName}.
I also want to append a hardcoded string to this like Text="{Binding propertyName} appendedName. How to do this?
Text="{Binding propertyName,StringFormat='Your property is: {}{0}'}"
You could use Run Text:
<TextBlock>
<Run Text="{Binding YourBinding}"/>
<Run Text="Suffix"/>
</TextBlock>
If you want to use it like this several times I would recommend a TemplatedControl where you have a Suffix DependencyProperty and a Text DependencyProperty.
You should create new property that returns text + appendedName.
Another way is to use several text blocks.

Resources