Skip to main content

Using StringFormat in XAML binding

February 05, 2014 mgroves 0 Comments
Tags: xaml windows phone

Today I discovered a little trick with XAML binding while working on a Windows Phone 8 app.

Let's say you have a TextBlock, and its value is bound like so:

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

Which works fine, but suppose I want to add some additional text, formatting, etc, kinda like a string.Format? I guess you could just do that in a property on whatever model object you're binding to, but you can also do this:

<TextBlock Text="{Binding MyInnuendo, StringFormat='\{0\}, if you know what I mean'}" />

Now the extra text and formatting will show up. I just thought it was a nice little trick; I'll leave it up to you to decide if you should do it this way.


Matthew D. Groves

About the Author

Matthew D. Groves lives in Central Ohio. He works remotely, loves to code, and is a Microsoft MVP.

Latest Comments