Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concat data binding and static text in C#

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!

like image 737
Glenn Avatar asked Jul 26 '15 21:07

Glenn


3 Answers

Use the property StringFormat on the binding.

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

It behaves the same as String.Format

like image 138
Scott Chamberlain Avatar answered Oct 19 '22 15:10

Scott Chamberlain


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

like image 37
Immons Avatar answered Oct 19 '22 16:10

Immons


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>
like image 2
Glenn Avatar answered Oct 19 '22 15:10

Glenn