Writing XML Exactly as XML Literals are Written - xaml

I'm transforming some XML from DrawingML to XAML. Unfortunately, the XAML one is not working as expected with white spaces, but I have found a work around. Here's the problem:
Problem Statment
I want to write the following in a TextBlock:
Hi John, what did Sushi A say to
Sushi B?
So I would write:
<TextBlock>
<Run>Hey</Run>
<Run FontWeight="Bold">John</Run>
<Run>,</Run>
<Run FontStyle="Italic">what did Sushi A say to Sushi B?</Run>
</TextBlock>
This doesn't produce the desired results. Instead, it produces:
Hi John , what did Sushi A say to
Sushi B?
Notice the space now between "John" and ","? Weird, eh? This is because XAML appends a space between runs. I don't know why it does this. I really do need the formatting exactly as above, so the option of changing formatting, like making the comma bold too is not an option.
Partial Solution
The weirder thing is that there is a way around this - i.e. to lose the extra space that XAML adds - you have to put your runs on the same line. I have no idea why, but that's the case. So the following actually works just fine:
<TextBlock>
<Run>Hey</Run>
<Run FontWeight="Bold">John</Run><Run>,</Run>
<Run FontStyle="Italic">what did Sushi A say to Sushi B?</Run>
</TextBlock>
Notice runs #2 and #3 (of 4 runs) are now on the same line.
Question
The issue I'm having is that I haven't found a way to write the above using XML Literals. If I try this:
Dim tb = <TextBlock>
<Run>Hey</Run>
<Run FontWeight="Bold">John</Run><Run>,</Run>
<Run FontStyle="Italic">what did Sushi A say to Sushi B?</Run>
</TextBlock>
it is always created as the below, with the 4 runs on seperate lines:
<TextBlock>
<Run>Hey</Run>
<Run FontWeight="Bold">John</Run>
<Run>,</Run>
<Run FontStyle="Italic">what did Sushi A say to Sushi B?</Run>
</TextBlock>
Does anyone know how XML can be written exactly as written in XML Literals?
Bonus
If you answer the question correctly, I'll tell you the punchline of the joke :)

I don't suppose using a span will help you (as it will keep non-formatted text out of XML elements so it might not get auto-formatted).
i.e.
<TextBlock>
<Span>
Hey
<Bold>John</Bold>,
<Italic>what did Sushi A say to Sushi B?</Italic>
</Span>
</TextBlock>
Obviously this only fixes the specific case not the general, I would probably suggest not using XML literals :)

Any chance the unicode backspace character would solve your problem?
http://www.fileformat.info/info/unicode/char/0008/index.htm
Update
One other idea. Have you looked into the XDocument.Save(TextWriter textWriter, SaveOptions saveOptions) method? The documentation says that if you use SaveOptions.DisableFormatting, it will preserve spacing.

Related

Xamarin Binding Special Characters

I am pulling some data from a remote DB and inserting them into a ResourceDictionary. My app will take the a key and get a value based on the chosen language.
My XAML looks like this:
<Label Text="{Binding Dictionary[, Something:]}"/>
<Label Text="{Binding Dictionary[" Why" - where what!]}"/>
So, if a user chooses Danish, the Label will use the key ", Something" and translate it to the Danish word.
The problem I'm having seems to be that I can't use special characters like "," and ":".
I have tried replacing those with Unicode and ASCII characters but it didn't work.

How to use the Run within the TextBlock without space in XAML Winrt?

<TextBlock>
<Run Text="{Binding Percentage}"/>
<Run Text="%"/>
</TextBlock>
I know this seems like a trivial question but I was wondering in the above code if Percentage value was 95 then it displays 95 % and not 95%. That is there is a space between the Run which I don't want. How to remove this.
Thanks in advance.
Try writing them in the same line. e.g.
<TextBlock>
<Run Text="{Binding Percentage}"/><Run Text="%"/>
</TextBlock>

Show '...' when text is longer that the area in textblock

Some days back i saw an example of what i need now but couldn't remind what was the way.
I want to show the text description in TextBlock and if the text is more that the size of text block, show the ...
Use below code to achieve this,
<TextBlock Text="{StaticResource someText}"
TextWrapping="Wrap" TextTrimming="CharacterEllipsis"
Margin="10"/>
And possible values of TextTrimming are as below,
None – no ellipsis, text is clipped (the default)
CharacterEllipsis – display as many characters as possible, followed
by an ellipsis
WordEllipsis – display as many words as possible, followed by an
ellipsis
I know link only answer are frowned upon but
TextBlock.TextTrimming Property
<TextBlock
Name="myTextBlock"
Margin="20" Background="LightGoldenrodYellow"
TextTrimming="WordEllipsis" TextWrapping="NoWrap"
FontSize="14">
One<LineBreak/>
two two<LineBreak/>
Three Three Three<LineBreak/>
four four four four<LineBreak/>
Five Five Five Five Five<LineBreak/>
six six six six six six<LineBreak/>
Seven Seven Seven Seven Seven Seven Seven
</TextBlock>

WinRT-xaml-Toolkit: Line Series with median Line

Is there a way to implement a median Line using this lib?
The only way I think could work is adding a new LineSeries and add a median for each Value in my Data-LineSeries
Something like this:
<charting:Chart>
<charting:Chart.Series >
<charting:LineSeries ItemsSource="{Binding Articles}" Title="Test Title" Height="400" Width="400" IndependentValuePath="dateTime" DependentValuePath="price" >
</charting:LineSeries>
---Pseudo----
<charting:LineSeries x:Name="Median" ItemsSource="{Binding Articles}" Title="Median" IndependentValuePath="dateTime" DependentValuePath="median">
</charting:LineSeries>
</charting:Chart.Series>
</charting:Chart>
This would cause me to add the median Value of my Product to each Article which is..kinda unnecessary.
Is there an easier way to do this? Or even a way which fills the space between Data and Median with a certain color (e.g. http://www.jidesoft.com/images/line-chart-gradient-fill.png)?
Thanks in Advance
I don't think there are any built in statistical functions in the DataVisualization library that I ported from Silverlight Toolkit, but you can provide your own data source separate from your Articles one that has any range of values you want.
I also can't recall if the filled style of the data series visualization is implemented, but you could try to create a modified version of the LineSeries one that does support Fill or check the source if any such property is already available. It might be doable by simply changing the Style of the series.

Multiple Colors In TextBlock

Is it possible to add dynamic colors to a TextBlock ..i.e. have one character in one color and the next in another color.
<TextBlock Text="{Binding no}" TextWrapping="Wrap" Margin="10,0,0,0" Style="{StaticResource PhoneTextSubtleStyle}" FontSize="40" Foreground="#A400C4FF" >
// Can we add something here to specify what colours for what chars
</TextBlock>
Basically I input a dynamic 4 character sequence from no. I've bound it to this TextBlock inside a ListBox. Is it possible to have the characters in different colors.
If so is it possible to add these colors dynamically for eg. If I click a button certain characters change color?
Thank You. Any Help is appreciated.
Actually, you can, which can come in handy when you're doing a StringFormat on a data bound Textblock or a number of other places.
If you did want to try it though, like here's an SL example for a form label that puts a red asterisk next to the text Required Fields, but then can also add more stuff to it as shown in the example. Should work for Silverlight, WPF, UWP, etc...
<TextBlock>
<Run Text="*" Foreground="#FFE10101"/><Run Text="Required Line" />
<Run Text="Red" Foreground="Red"/>
<Run Text="Blue" Foreground="Blue"/>
<Run Text="{Binding SomeString, StringFormat='Hell ya you can make \{0\} a different color!'}" Foreground="Orange"/>
</TextBlock>
I'm developing for Mango with the WP7 SDK. You can use a <Run>. It seems a little buggy on WP7 , you have to add a spaces on the Run.Text property to get the spacing correct:
<TextBlock>Hello<Run Foreground="Bisque" Text=" Holla "></Run>and hello again!</TextBlock>;
to set foreground color dynamically to a textblock
use: txtblockname.Foreground= new SolidColorBrush(Colors.Yellow);
The TextBlock does not support multiple Foreground colors.
You could recreate this behaviour by using multiple textblocks (one for each letter) and placing them within a wrappanel. You could then change the color of individual characters/letters as you wish.
Beware of the probable performance impact this may have. The margins around individual letters will need to be adjusted to recreate standard behaviour though. Be especially careful around punctuation.