Text Object Model

Text has distinctive layout requirements. With most user interface elements, the goal of layout is typically to define the basic UI structure (e.g., menu and toolbars at the top, status bar at the bottom, tree view on the left, scrollable work area on the right) and have the layout system make simple adjustments to the size and position of each element in order to fit the available space. But with text, we typically have a continuous stream of content where sequencing is more important than exact position. For example, we often don't care whether a particular figure appears at the top, bottom, or middle of a page as long as it is adjacent to the paragraph that refers to the figure.

There is some overlap between the requirements of text layout and UI layout. For example, the WrapPanel arranges elements in a similar manner to word-wrapped text layout. Although it would have been technically possible to use a single layout system for both textual content and user interface structure, this would have involved compromises on both sides. WPF's object model for representing formatted text is therefore separate from the layout panels and controls we've seen in previous chapters.

The element types that make up the text model all derive from a common abstract base class: TextElement. As you saw in Table 14-1, this defines a set of properties for controlling aspects of the text's appearance, such as its font and color. The TextElement type does not derive from FrameworkElement ...

Get Programming WPF, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.