JKennedy
JKennedy

Reputation: 18799

How To surround a string in Quotation Marks in Xamarin.Forms

So I have a label which is bound to some text in my view model like so:

<Label VerticalOptions="Center" Text="{Binding Note, StringFormat='&quot;{0}&quot;'}" Style="{StaticResource ListItemSubTitleStyleDefault}" LineBreakMode="WordWrap" FontAttributes="Italic"/>

And I am trying to get the note to be surrounded in quotation marks like so

"I am a Note"

looking at some WPF answers it suggested using the following in the StringFormat property '&quot;{0}&quot;'

But this doesn't seem to work. Does anyone know how to surround a Labels text in quotation marks in Xamarin.Forms?

Upvotes: 2

Views: 2649

Answers (2)

Mario Galv&#225;n
Mario Galv&#225;n

Reputation: 4032

On the code from your ViewModel you can also do this:

string _note;
public string Note => string.Format("\"{0}\"", _note);

I hope this helps.

Upvotes: 0

DavidS
DavidS

Reputation: 2934

As you've seen, Xamarin.Forms is different from WPF for this case. For Xamarin, do this:

<Label VerticalOptions="Center" Text="{Binding Note, StringFormat='{}&quot;{0}&quot;'}" .../>

In order to prevent the runtime from ignoring the double quotes, the first double quote either has to be escaped (as above), or it can't immediately follow the single quote (see below).

So for example, throwing in a space in between works as well:

<Label VerticalOptions="Center" Text="{Binding Note, StringFormat=' &quot;{0}&quot;'}" .../>

With this latter solution, there will be at least one character rendered before the double quote.

Upvotes: 4

Related Questions