Choosing a Base Class

WPF provides many classes from which you can derive when creating custom elements. Figure 18-1 shows a set of classes that are most likely to be suitable base classes, and illustrates the inheritance relationship between them. Note that this is by no means a complete inheritance diagram—it simply shows the classes you should consider as possible base classes.

Partial class hierarchy, showing candidate base classes for custom elements

Figure 18-1. Partial class hierarchy, showing candidate base classes for custom elements

Whichever base class you choose, your element will derive directly or indirectly from FrameworkElement. This offers event routing, advanced property handling, animation, data binding, layout support, styling, and logical tree integration.

Tip

It is not an absolute requirement to derive from FrameworkElement. Chapter 13 discussed the low-level visual layer graphics API, and although the example in that chapter derived from FrameworkElement, you can derive directly from UIElement when using the low-level drawing API. However, you would lose all of the services FrameworkElement offers. The main reason UIElement exists is that Microsoft wanted to make it possible to use WPF's low-level rendering services without being obliged to use the whole WPF framework. In practice, you would not normally do this.

Deriving directly from FrameworkElement might be appropriate for an element designed to be composed into other elements. For example, ...

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.