What is the correct syntax for using a single-binding StringFormat?

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 does not work (although it seems to be the same syntax as this example ):

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

Answer:

Thanks to Matt that I needed a combination of your two answers, this works great:

<TextBlock Text="{Binding Path=HiredDate, 
    StringFormat='Hired on {0:MMM dd, yyyy}'}"/>
+5
source share
1 answer

You want to leave the curly braces from the format string in your example because you are not using them as a placeholder (for example, you should use "{0}" in String.Format ()).

So:

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

placeholder , , :

<TextBlock Text="{Binding Path=HiredDate, StringFormat='Hired on \{0\}'}"/>
+10

All Articles