How To surround a string in Quotation Marks in Xamarin.Forms - c#

So I have a label which is bound to some text in my view model like so:
<Label VerticalOptions="Center" Text="{Binding Note, StringFormat='"{0}"'}" 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 '"{0}"'
But this doesn't seem to work. Does anyone know how to surround a Labels text in quotation marks in Xamarin.Forms?

As you've seen, Xamarin.Forms is different from WPF for this case. For Xamarin, do this:
<Label VerticalOptions="Center" Text="{Binding Note, StringFormat='{}"{0}"'}" .../>
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=' "{0}"'}" .../>
With this latter solution, there will be at least one character rendered before the double quote.

On the code from your ViewModel you can also do this:
string _note;
public string Note => string.Format("\"{0}\"", _note);
I hope this helps.

Related

How to add additional text (or string) to a binding in xaml

I have this:
<Label Text="{Binding Height}" AbsoluteLayout.LayoutBounds=".9,.17,-1,-1" TextColor="White" AbsoluteLayout.LayoutFlags="PositionProportional" HorizontalTextAlignment="Center" HorizontalOptions="Center" FontSize="Medium"/>
Obviously {Binding Height} takes up the entire text.
The binding is currently a double, and needs to remain so, I just need to concat an 'm' at the end to represent meters.
I have tried {Binding Height} m and {Binding Height + m} but obviously xaml doesn't work the same way a regular string concatenation would work.
Try using StringFormat.
Like this:
Text="{Binding Height, StringFormat='{}{0}m'}"
Edited for clarity:
You can write anything you want after the {0} argument.
For example, the above will produce values like 25m, 10m etc.
You can write something like this if you like:
Text="{Binding Height, StringFormat='{}{0} is a good number.'}"
The above will produce, for example:
10 is a good number.
Have you tried StringFormat?
<Label Text="{Binding Height, StringFormat={0}m}"

WPF Label Flowdirection text does not reverse with special characters

I want to display the text "1/min" in a label with reversed FlowDirection, i.e. RightToLeft. However, the text remains the same with either "1/min" or "min/1".
Test code:
<Grid>
<StackPanel>
<Label FlowDirection="RightToLeft">1/min</Label>
<Label FlowDirection="RightToLeft">min/1</Label>
</StackPanel>
</Grid>
Both labels show the same text, namely "min/1". I'm fairly certain that non-ascii chars cause this, but how do I get around that behaviour?

WPF TextBox disable break on special characters

I have a TextBox defined like this:
<TextBox Text="{Binding License.LicenseKey}"
HorizontalContentAlignment="Left"
TextAlignment="Justify"
Width="350"
Height="100"
Margin="10,0,0,0"
TextWrapping="Wrap" />
Currently a long string will break on special characters:
I would prefer it to simply break on any character once it reaches the end of the TextBox like this:
Is there a way to disable the stock breaking that a TextBox uses? I have tried various options for TextAlignment and HorizontalContentAlignment to no avail.
You could add a zero-width space (U+200B) after each character which would allow a break at any position. You would need to define a property in your view model and bind to it, and have the getter do this transformation so that it is displayed with line breaks, e.g.:
string SomeProperty
{
get { return String.Join(string.Empty, License.LicenseKey.Zip(new string('\u200B', License.LicenseKey.Length), (x, y) => x.ToString() + y)); }
set { Model.LicenseKey = value?.Replace("\u200B", string.Empty); }
}
However I don't know what would happen to the cursor position.
This is exceptionally messy due to limited options on the TextBox's TextWrapping property.
See this forum post to explain the U+200B comment below your question. However, that doesn't work for you because you DON'T want it to break. And if there's a library of standard non-breaking versions of characters, I've been unable to dig it up.
The only way I see this working is to use a fixed-width font, keeping track of how many characters are entered alongside the capacity of the box, and adding your own newline when that capacity is reached.

HeaderStringFormat issue when using Curly Brace next to the beginning quote

I have the following XAML. The idea is to append the text " For Report" after a string that is coming from a bound XML file.
<GroupBox Header="{Binding XMLData.Element[HeadingText].Value}"
HeaderStringFormat="For Report">
When I add {0} to the beginning of the HeadingStringFormat String like so, the application ignores the HeaderStringFormat and simply puts up the databound value.
<GroupBox Header="{Binding XMLData.Element[HeadingText].Value}"
HeaderStringFormat="{0} For Report">
However, when I add a single space (or any other character for that matter) after the quote, but prior to the left {, the application works fine.
<GroupBox Header="{Binding XMLData.Element[HeadingText].Value}"
HeaderStringFormat=" {0} For Report" >
In the first example, the editor seems to think that the string (between the quotes) isn't a string anymore, but if I put in that space, it's happy.
Try prefixing your format string with {}. This should work:
<GroupBox Header="{Binding XMLData.Element[HeadingText].Value}"
HeaderStringFormat="{}{0} For Report">
For further information, see MSDN: {} Escape Sequence / Markup Extension

How to Line Break or new line in XAML

I am having hard time to match Special characters set in XAML. I only on the following:
To represent a LineBreak in XAML hyperlink button:
use : > lineBreak <
But What do I use to represent a New Line or LineBreak In XAML hyperlink button??
Example : I want this one line mag : This is line one. This is line two
into this :
This is line one. This is line two.
it seems this \r\n is not working. This is line one \r\n
You've got options. For example;
<HyperlinkButton Content="Line One
Line Two"/>
or
<HyperlinkButton>
<HyperlinkButton.Content>
<TextBlock>
<Run Text="Line 1"/><LineBreak/><Run Text="Line 2"/>
</TextBlock>
</HyperlinkButton.Content>
</HyperlinkButton>
Hope this helps.
Addendum: You can do this stuff in basically anything. WPF, Silverlight, UWP, whatever. It's not WP specific.
You can use preserve. It includes all whitespace, so inputting the exact string you want would involve messing up your indentation, but this will work:
<HyperlinkButton xml:space="preserve">This is line one.
This is line two.</HyperlinkButton>

Categories