Tips: Concatenating strings in Silverlight XAML using StringFormat

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."/>


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"/>


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.

If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter, Facebook, Google+ and LinkedIn. I would love to hear your thoughts and opinions on my articles directly.

Don't forget to share your feedback/queries in the comment section below.


  1. Thank you. In the next post, I will show you different formatting option in XAML using StringFormat. Stay tuned.

  2. Hello Kunal, Nice
    But If my both strings have different style then ?

    Any other solution rather than above.
    Any Idea ?

  3. Hi Jugal,

    For this I have my next article here: Formatting text in Silverlight XAML using StringFormat. Hope, that will help you to understand it properly.

  4. Nope it doesn't help me.
    because I have a TextBlock which has text with two different style.
    then I have to have separate TextBlock

    Note :
    I am unable to post code here. my previous comment It should have been code.


  5. Hello Jugal,

    Please explore the properties of Run. I already shown in that article, how you can split strings of a single TextBlock using the Run tag. Just add proper style to those as below:

    <Run Text="Username: " FontStyle="{StaticResource Label_Style}"/>
    <Run Text="Kunal" FontStyle="{StaticResource Value_Style}"/>

    Hope this will help you. Let me know, if you have further queries on this.
    Regarding the XAML code, you need to encode them properly. For example, replace < with &lt; and > with &gt; :)

  6. i really like it ..

    thank you sir...........
    soundarya ..coimbatore

  7. How to do same in Windows store apps ? StringFormat property is not available.

  8. Hi Farhan,

    StringFormat is not supported in XAML for Windows Store apps. Just use a Converter to do the same.



© 2008-2018 Kunal-Chowdhury.com | Designed by Kunal Chowdhury
Back to top