Visual Layer Programming

The shape elements can provide a convenient way to work with graphics. However, in some situations, creating all the shape elements required to represent a drawing, and adding them to the UI tree, may be more trouble than it's worth. Data binding can often provide a solution—the shape classes all derive from FrameworkElement, so they can participate in data binding like any other user interface element. However, sometimes your data may be structured in such a way that it's easier or more efficient to write code that performs a series of drawing operations based on the data. For this reason, WPF provides a "visual layer" API as a lower-level alternative to shape elements. (In fact, the shape elements are all implemented on top of this visual layer.) This API lets us write code that renders content on demand.

Tip

A visual is a visible object. A WPF application's appearance is formed by composing all of its visuals onto the screen. Because WPF builds on top of the visual layer, every element is a visual—the FrameworkElement base class derives indirectly from Visual. Programming at the visual layer simply involves creating a visual and writing code that tells WPF what we'd like to appear in that visual.

Even at this low level, WPF behaves very differently from Win32. The way in which graphics acceleration is managed means that your on-demand rendering code is called much less often than it would be in a classic Windows application.

Rendering On Demand

The key to ...

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.