Yesterday in this post "Tips: Formatting Silverlight TextBlock Control", I shown you how to use various tags like Run, Span, Bold, Italic and Underline to format the text of TextBlock control, present in your XAML code. I also described the use of Run to bind multiple strings inside a TextBlock control.

 

Today in this small post, I will show you a different method to concatenate multiple text content inside a single control. This tip will help you next time while doing the same. Read to know more.

 

 

Here, we will learn a different way to concatenate multiple strings inside a control. It's not a new feature but many new hands don't know about it and hence thought to share the same here, so that, you will not miss it next time. If you didn't read my previous post on the same topic, please go and read that here: "Tips: Formatting Silverlight TextBlock Control". This will give you more visibility on it.

 

In general this is how we concatenate multiple strings:

 
<StackPanel Orientation="Horizontal" DataContext="{StaticResource User}">
    <TextBlock Text="Welcome "/>
    <TextBlock Text="{Binding Name}"/>
    <TextBlock Text=" to our site."/>
</StackPanel>

 

We used multiple TextBlock wrapped with a StackPanel to implement a single line of string. Yesterday we learnt using <Run /> tag to implement it too. Here is the implementation of the same using the <Run /> tag:

 
<TextBlock DataContext="{StaticResource User}">
    <Run Text="Welcome"/>
    <Run Text="{Binding Name}"/>
    <Run Text="to our site"/>
</TextBlock>

 

In both the cases, we have to use different tags and thus it creates a no. of lines while concatenating. Did you ever think whether there is an option like C# to format a text? Can we use something like "string.Format()" method to do it in XAML? Ok, in this post we will see the same thing.

 

Silverlight 4 has the feature called "StringFormat" which you can use in your XAML while string concatenation inside a single control. Forget about multiple TextBlock controls, forget about multiple Run tags and/or value converters now.

 

StringFormat option allows you putting all the information in a single element. All you have to do is the following:

 
<TextBlock DataContext="{StaticResource User}"
           Text="{Binding Name, StringFormat='Welcome \{0\} to our site'}" />

 

That's only the code and your concatenated text is ready inside a single control. Do use of it whenever you want and make your life simple while displaying multiple text. Make sure that, it will not help you in multiple binding. If you want to use multiple binding, yesterday's post on <Run /> tag will help you there.

 

Please share this to others whom you think that it will be helpful. Your feedback always count. Stay tuned for my tomorrow's post, where I will show you other features of StringFormat inside XAML. Till then, Happy Coding.

Have a question? Or, a comment? Let's Discuss it below...

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.