WPF supports several text controls and a number of document specific elements. Broadly speaking, you can categorized the elements as follows. There are the text display elements, which excel at showing read only content on the screen. There are editors, which provide editable boxes on the screen. In the editor, you can write text, edit and rearrange the text and in some cases, change the format of characters and words. They are document specific elements too. These elements work in conjunction with the display and editor controls. These elements allow fine-tuned control over the content of at text document.
- [Instructor] WPF sports several text controls and a number of document specific elements. Broadly speaking, you can categorize the elements as follows. There are text display elements, which excel at showing read only content on the screen. There are editors, which provide editable boxes on the screen. In the editor, you can write text, edit, and rearrange the text, and in some cases, change the format of characters and words. There are document specific elements, too. These document elements work in conjunction with the display and editor controls.
These elements allow fine tune control over the content of a text document. I'll be demonstrating some of these controls in this XAML file, AllTheText.xaml. We'll start by looking at the Read-Only text display. I have three, Label, TextBlock, and AccessText. Label has a history in Windows. If you go back prior to WPF in the Windows Forms days, or in the old VB days, there was a concept of a label. In the HTML world, in markup, there's a concept of the label.
Microsoft provides the label as a way of doing read only text. TextBlock is the newer version. This is a memory efficient lightweight element. To set the text for the label, I can use the content property like this. This is setting a string data to the Content. For the TextBlock, I have the Text property, that's also setting string data to the TextBlock. This third element is called AccessText. It is an element that acts like a special type of TextBlock.
Its specialty is to enable access keys tied to the Alt key on the computer keyboard. Any keyboard character preceded by an underscore _ is designated as the access key. You can see here in this string, I have an underscore in front of the letter _K. That means on the computer, when the user presses the Alt + K key, it'll activate the AccessText. Typically, this is not used stand alone like this unless you're working in a control template. It's usually used within the Label class.
It already has built in support for AccessText and I'll show you that later in this chapter. The TextBox and Label also support other kind of content besides plain text strings. Here's an example. There's the TextBlock, so rather than setting the text like I did up here in line 21, here I'm using TextBlock.Inlines. Inlines are those document specific elements I was mentioning. What this allows me to do is define chunks of text that are marked up with these other elements, these Inline elements.
I'm making this bold, I'm putting a LineBreak in, and then I'm putting Italics in. They're similar to the way HTML tags work. The Label also allows content besides text. A Label is a content control, and if you remember what a content control does, is it's a control that supports XAML content, so any valid XAML elements can be nested inside a content control. That's what I'm doing here.
Inside the Label, I'm specifying a StackPanel that contains a rectangle and a TextBlock, and a rectangle. Now we'll go look at this second tab. We'll talk about Read-Only FlowDocuments. I talked about Inlines. You can put those Inlines in a variety of different elements. What we're looking at, and this one is three of them, FlowDocumentPageViewer, FlowDocumentScrollViewer, and FlowDocumentReader.
The idea behind these three elements is when you have large amounts of text, formatted text, and you want to show it to the user. It's still Read-Only, just like the TextBlock, but the difference, the TextBlock is for simple text, a few paragraphs, perhaps. If you want to have more text, then you'll want to have a control that understands scrolling, so the user can look through long documents, or maybe they want to be able to page through a long document, so you could use the FlowDocumentPageViewer. These are Read-Only, and there is the concept of a FlowDocument, but the FlowDocument's not hard to understand when you pop this open.
There's the Document property, there's an instance of the FlowDocument, and right here are the Inlines that we saw in the TextBlocks earlier. This is another way of defining it, and you're putting in an element that's optimized for the user reading bigger documents. Finally, we have the Editors, there are three. There's the TextBox, the PasswordBox, and the RichTextBox. Now, the TextBox is a place for the user to edit a text, and add text, and you set the text with the text property.
The PasswordBox is very similar to the TextBox, except it hides what you're typing, so you can't see the text. Then the RichTextBox is exactly the same as the FlowDocumentViewer as I mentioned up here, but it's an editor, so you can define a document and a FlowDocument and use those same Inlines, you can load a FlowDocument into the RichTextBox. You can also load a rich text format, or an RTF file, into the RichTextBox.
In this chapter, I'll look at the text displays. In the next chapter, I'll look at the text editors in more detail.
- Interactions and controls
- UIElement family
- FrameworkElement family
- Shape family
- Decorator Family
- Control Fundamentals
- Button family
- Text Editor family