Page 1 of 1

Unity XAML Editing at Runtime

Posted: 08 Dec 2017, 16:15
by ajsmith595
I am working with NoesisGUI with Unity, and I am trying to add Run tags into an existing TextBlock at runtime. Is this possible? I am trying to get it so that specific parts of the text are certain colours and bold/italic when needed.
If it was a Grid then you could just add a Run tag to its children, but I want to do this with a TextBlock, but this does not have this option. I have tried adding the tags as you would in XAML literally as in setting its text value to:
<Run Text="whatever" Foreground="SomeColour"></Run>
But all this does is display the literal text. Any ideas?
And by the way, I did put actual colours into the Foreground property, this was just a demo of what I did

Re: Unity XAML Editing at Runtime

Posted: 09 Dec 2017, 03:04
by ajsmith595
I have solved this by instead of using one TextBlock, using a StackPanel with WrapPanels for each line, with TextBlocks in those WrapPanels for each of the different text styles.

Re: Unity XAML Editing at Runtime

Posted: 11 Dec 2017, 10:43
by sfernandez

TextBlock exposes the Inlines property. It is a collection where you can add inline elements like Run, Span, LineBreak...
TextBlock txt = (TextBlock)FindName("txt");
txt.Inlines.Add(new Run { Text = "Hello", Foreground = Brushes.Red });
txt.Inlines.Add(new Run { Text = " World", Foreground = Brushes.Blue });
This is better than using a WrapPanel with several TextBlocks inside.