Glenn
Glenn

Reputation: 655

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!

Upvotes: 8

Views: 2942

Answers (3)

Glenn
Glenn

Reputation: 655

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>

Upvotes: 2

Immons
Immons

Reputation: 257

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

Upvotes: 2

Scott Chamberlain
Scott Chamberlain

Reputation: 127563

Use the property StringFormat on the binding.

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

It behaves the same as String.Format

Upvotes: 8

Related Questions