The TextBlock can show multi-formatted text, the key to this feature is via the Inline classes. These elements an HTML like experience, providing Bold, Italic, Underline and other standard format concept to the TextBlock text.
- [Instructor] The text block excels at showing text on the screen. Assign a value to the text property and as it's rendered to the screen, it picks up the formatting assigned in these properties. There might be occasions where you would like to mix it up and have some of the words in the text box be in one format and other words in a separate format. That's the reason why WPF has a concept of inline. And inline means, that it is a piece of text that can be rendered as part of a larger text.
Where that's implemented in the text block, is there's an inlines property which is a collection and then you can put instances of the inline classes in here. So, here's an example, the bold class is in inline. It inherits from span. Span inherits from inline and then inline inherits from text element. So, text element is all the elements that are used to build something called a flow document. Let's go take a look at all the text elements over here in this class diagram.
Here's the text element. Inline is one branch. You see, on this side of the page is some of the other text elements. Like, list or paragraph. So, when you're building a document that the user would read and you need to split it into paragraphs or bulleted lists, that would be the purpose of a text element. The text block is simpler than that. It only supports the elements that are in this area. The ones that derive from inlines. That would be things like a run and a span. The spans are items like, underline, bold, italics and hyperlink.
Let's see how to use these in the example. So, line 18 shows a text block that is using the text property. To work with the inlines, one syntax you can use is, start the text block then go to this property, text block.inlines and then once you're inside that area, you can start using these inline elements. The way you put text inside an inline is by placing it in between the beginning and ending tags.
So, this is the bold text. This indicates a line break. And this indicates the italics text. If you've worked in other market blind which is like HTML, this'll be very familiar to you here's another way of specifying inlines. Create the open and closing text block tags and then just start using the inlines in between. You don't need to use this more verbose syntax. The run is a way of formatting a group of text.
So, what I've got here in a run, it says here, "Contains text only. No child inlines." So, I can put text inside there and then I can do a line break. And here's one of the advantages of using a run, I've got this section of text, I put it inside a run and then I can format these properties. Font size, font weight, foreground, I can format just that run. You see what I'm doing here is I'm making a capital letter and then the rest of this sentence is in smaller text size.
A related element is the span. This can contain text and inlines. So, here is why this is useful, I create a span and then inside the span, I create a line break and a bold section and another line break. Now, the benefit of using the span is, one, is I can have these child inlines but then I can go to the span, itself, and set the font size and the font weight and the foreground, here. Otherwise, if I couldn't do it at the span level and I needed to change the font size, I'd have to do it in every one of the inlines and that would get tedious.
So, the span is just a wrapper that lets me group together a bunch of inlines and then apply a formatting to all of the inlines, at one time. As you can see, there's a lot of things to learn, here. And it shows you that the text block can have mixed formatted text.
- Interactions and controls
- UIElement family
- FrameworkElement family
- Shape family
- Decorator Family
- Control Fundamentals
- Button family
- Text Editor family