HeaderStringFormat issue when using Curly Brace next to the beginning quote - c#

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

Related

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='"{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.

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.

Span-Elements in FormattedString appear to be trimmed

I am trying to put some formatted text in a view (an imprint, for what it's worth) with a Label with an formatted text. Text formatting works as intended, but whenever I try to add whitespace characters to the end of a span element within the formatted text these appear to be trimmed. This holds true for normal spaces, non-breaking spaces and CR/NL so far. Anyway, when in the middle of a string, nothing is removed.
This renders the label unusable for me (at least for this use-case), since I won't be able to format my text properly. Is there anything I have missed? I did not find anything about this matter in the web and in the documentation. Is the approach taken completely wrong, or is this a bug in Xamarin? (For that matter, the version used is 2.3.2.127)
This renders the label unusable for me (at least for this use-case), since I won't be able to format my text properly
It's not entirely clear why you would try to achieve this formatting with trailing whitespace.
Have you tried using margin or padding around the label?
https://developer.xamarin.com/guides/xamarin-forms/user-interface/layouts/margin-and-padding/
Using whitespace characters for element spacing is generally not a good practice.
The solution to this is to use inline Text="..." rather than enclosing the text between an opening and closing tag. For example, this preserves the spaces:
<FormattedString.Spans>
<Span Text="You have " />
<Span Text="{Binding Points}" />
<Span Text=" points." />
</FormattedString.Spans>
But this trims whitespace:
<FormattedString.Spans>
<Span>You have </Span>
<Span Text="{Binding Points}" />
<Span> points.</Span>
</FormattedString.Spans>

How do I pass string with spaces to converterParameter?

My sample code is below.
I want to pass 'Go to linked item' to ConverterParameter but I can't because the string has spaces.
Text="{Binding Value,
Source={x:Static local:Dictionary.Instance},
Converter={StaticResource StringConverter},
ConverterParameter=Go to linked item, Mode=OneWay}"
How can I do this?
Option 1
Text="{Binding Value,
Source={x:Static local:Dictionary.Instance},
Converter={StaticResource StringConverter},
ConverterParameter='Go to linked item', Mode=OneWay}"
Option 2
If you want to use this in multiple places add a string resource.
<sys:String x:Key="GoToLink">Go to linked item</sys:String>
And pass the resource key.
ConverterParameter={StaticResource ResourceKey=GoToLink}}
If your string has spaces then wrap it in single quotes, double quotes won't work; this is probably due to the fact that the entire text field is wrapped in double quotes and so using them again within the binding would incorrectly indicate closure.
Text="{Binding Value,
Source={x:Static local:Dictionary.Instance},
Converter={StaticResource StringConverter},
ConverterParameter='Go to linked item', Mode=OneWay}"

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