tags:

views:

220

answers:

1

I can get MultiBinding to work with StringFormat:

<TextBlock.Text>
    <MultiBinding StringFormat="{}{0} {1} (hired on {2:MMM dd, yyyy})">
        <Binding Path="FirstName"/>
        <Binding Path="LastName"/>
        <Binding Path="HireDate"/>
    </MultiBinding>
</TextBlock.Text>

But what is the correct syntax for single binding? The following doesn't work (although it seems to be the same syntax as this example):

<TextBlock Text="{Binding Path=HiredDate, StringFormat='{MMM dd, yyyy}'}"/>

ANSWER:

Thanks Matt, what I needed was a combination of your two answers, this works great:

<TextBlock Text="{Binding Path=HiredDate, 
    StringFormat='Hired on {0:MMM dd, yyyy}'}"/>
+4  A: 

You want to leave the curly braces out of the format string in your example, because you're not using them as a placeholder (like you'd use "{0}" in String.Format()).

So:

<TextBlock Text="{Binding Path=HiredDate, StringFormat='MMM dd, yyyy'}"/>

If you want to reference the placeholder value somewhere inside the string, you can do so by escaping the curly braces with a backslash:

<TextBlock Text="{Binding Path=HiredDate, StringFormat='Hired on \{0\}'}"/>
Matt Hamilton
You don't think you need the backslashes on your second example, it works without them, or are they there for some other reason.
Edward Tanguay
Strange. I've had compilation errors in the past when I've omitted the backslashes. Glad it works for you though!
Matt Hamilton