JKennedy
JKennedy

Reputation: 18799

Is it possible to add "" (Quotes) to a binding using StringFormat

So I have a TextBlock bound to a property on my object like so:

<TextBlock Grid.Column="1" Text="{Binding FriendlyName}" Margin="0,5,0,5"/>

I would now like to surround this text in quotes and add a hyphen to it so I tried:

<TextBlock Grid.Column="1" Text="{Binding FriendlyName, StringFormat= - \"{0}\"}" Margin="0,5,0,5"/>

But got a number of errors.

I also tried (from here):

<TextBlock Grid.Column="1" Text="{Binding FriendlyName, StringFormat= -  &quot;{0} &quot;}" Margin="0,5,0,5"/>

but got the error:

Error 4 Names and Values in a MarkupExtension cannot contain quotes. The MarkupExtension arguments ' FriendlyName, StringFormat= - "{0} "}' are not valid

So I was wondering is it possible to add quotes to a binding using StringFormat?

Upvotes: 5

Views: 1948

Answers (2)

nicholas
nicholas

Reputation: 3047

Define the binding element explicitly (no multi-binding necessary):

<TextBlock Grid.Column="1" Margin="0,5,0,5">
    <TextBlock.Text>
        <Binding StringFormat="{} -  &quot;{0}&quot;" Path="FriendlyName"/>
    </TextBlock.Text>
</TextBlock>

Upvotes: 1

kmatyaszek
kmatyaszek

Reputation: 19296

You should add single quotes:

 <TextBlock Grid.Column="1" Text="{Binding FriendlyName, StringFormat='-  &quot;{0}&quot;'}" Margin="0,5,0,5"/>

Or you can use MultiBinding:

<TextBlock Grid.Column="1" Margin="0,5,0,5">
    <TextBlock.Text>
        <MultiBinding StringFormat=" -  &quot;{0}&quot;">
            <Binding Path="FriendlyName" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

Upvotes: 7

Related Questions