Midimatt
Midimatt

Reputation: 1114

Correctly escaping '{' character in Silverlight bindings with string format

I'm trying to use string format with a data binding in silverlight, however if the '{' character is the first character in the string format then the project fails to build.

Adding a space before allows the code to build and displays what I want, however i'd like to get rid of the first space.

<TextBlock Grid.Column="0"
           Grid.ColumnSpan="2"
           Grid.Row="11"
           Margin="2">
    <TextBlock.Text>
        <Binding ElementName="_DateTypes" 
                 Path="SelectedItem"
                 StringFormat=" {0}'s Ago "/>
        </TextBlock.Text>
    </TextBlock>

I've tried the following

StringFormat=" {0}s Ago "
StringFormat="\{0}s Ago "
StringFormat="/{0}s Ago "
StringFormat="%s's Ago "

and get the following results if the string is Day

 Days Ago
\Days Ago
/Days Ago
Day

Upvotes: 0

Views: 286

Answers (2)

codingbiz
codingbiz

Reputation: 26386

I think it should be

StringFormat="\{0\}s Ago "

Escaping the two symbols

Upvotes: 0

McGarnagle
McGarnagle

Reputation: 102743

I have no idea how or why this works, but you just have to prepend {}:

StringFormat="{} {0}'s Ago "

(I suppose it's just a syntactic workaround, since normally starting an attribute with { indicates a binding expression.)

Upvotes: 2

Related Questions