Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 794
User1 Avatar asked Mar 15 '23 07:03

User1


1 Answers

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>
like image 173
kmatyaszek Avatar answered Apr 26 '23 01:04

kmatyaszek