So I have a textbox with a data binding to it, but I want to add static text in my xaml code.
<TextBlock Text="{Binding Preptime}"></TextBlock>
This will only show the number of minutes, I want it to be displayed as: "Preparation time: 55 minutes"
public String Preparation
{
get { return "Preparation time: " + Preptime + " minutes"; }
}
I know I can use a getter for this which would be a clean solution but there has to be a way to write this directly into my xaml?
Thanks in advance!
Use the property StringFormat
on the binding.
<TextBlock Text="{Binding Preptime, StringFormat=Preparation time: {0} minutes}"></TextBlock>
It behaves the same as String.Format
You can use StringFormat directly on TextBlock's Text property, just like you used string.format in your .cs
After some additional searching I found that using runs might be the easiest solution. More info here: Windows Phone 8.1 XAML StringFormat
<TextBlock>
<Run Text="Preparation time: "></Run>
<Run Text="{Binding Preptime}"></Run>
<Run Text=" minutes."></Run>
</TextBlock>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With