arrow-left arrow-right brightness-2 chevron-left chevron-right circle-half-full dots-horizontal facebook-box facebook loader magnify menu-down RSS star Twitter twitter GitHub white-balance-sunny window-close
String.Format MultiBinding for WPF
1 min read

String.Format MultiBinding for WPF

This is an old post and doesn't necessarily reflect my current thinking on a topic, and some links or images may not work. The text is preserved here for posterity.

One of the upcoming additions in .NET 3.5 SP1 is a StringFormat parameter you can pass along with your bindings. Sacha Barber has an example of how to use it:

    Text="{Binding Path=AccountBalance, StringFormat='You have {0:c} in your bank account.'}" 

However, if you can't install the service pack or you haven't been able to upgrade to .NET 3.5, don't fret. Here's a snippet, albeit with a few more lines of code, to perform the same thing:

            ConverterParameter="Hello {0} {1}, you have {2:c} in your account"
            Converter="{StaticResource StringFormatConverter}"
            <Binding Path="FirstName" />
            <Binding Path="LastName" />
            <Binding Path="AccountBalance" />

The converter would be implemented as so:

class StringFormatConverter : IMultiValueConverter
   public object Convert(object[] values, ..., object parameter) 
       return string.Format(parameter.ToString(), values);

You can find this and more binding tricks in my WPF Platform Examples solution.

Enjoying these posts? Subscribe for more

Subscribe now
Already have an account? Sign in
Paul Stovell's Blog

Hello, I'm Paul Stovell

I'm a Brisbane-based software developer, and founder of Octopus Deploy, a DevOps automation software company. This is my personal blog where I write about my journey with Octopus and software development.

I write new blog posts about once a month. Subscribe and I'll send you an email when I publish something new.



You've successfully subscribed to Paul Stovell's Blog.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.