Concatenated data binding and static text in C #

So, I have a text field with data binding to it, but I want to add static text to my xaml code.

<TextBlock Text="{Binding Preptime}"></TextBlock> 

This will only show the number of minutes, I want it to display as: "Preparation time: 55 minutes"

  public String Preparation { get { return "Preparation time: " + Preptime + " minutes"; } } 

I know that I can use getter for this, which would be a clean solution, but should there be a way to write this directly to my xaml?

Thanks in advance!

+7
c # data-binding
source share
3 answers

After some additional searching, I found that using runs might be the easiest solution. Read more here: Windows Phone 8.1 XAML StringFormat

  <TextBlock> <Run Text="Preparation time: "></Run> <Run Text="{Binding Preptime}"></Run> <Run Text=" minutes."></Run> </TextBlock> 
+1
source share

Use the StringFormat property to bind.

 <TextBlock Text="{Binding Preptime, StringFormat=Preparation time: {0} minutes}"></TextBlock> 

It behaves the same as String.Format

+7
source share

You can use StringFormat directly in TextBlock Text, just like you used string.format in your .cs

+2
source share

All Articles