ajsmith595
Topic Author
Posts: 8
Joined: 18 Nov 2017, 16:23

Unity XAML Editing at Runtime

08 Dec 2017, 16:15

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
 
ajsmith595
Topic Author
Posts: 8
Joined: 18 Nov 2017, 16:23

Re: Unity XAML Editing at Runtime

09 Dec 2017, 03:04

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.
 
User avatar
sfernandez
Site Admin
Posts: 2983
Joined: 22 Dec 2011, 19:20

Re: Unity XAML Editing at Runtime

11 Dec 2017, 10:43

Hi,

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.

Who is online

Users browsing this forum: Google [Bot] and 72 guests