"Programming WPF" translation 9th Chapter 2. Select a base class

Source: Internet
Author: User
Tags inheritance

WPF provides a number of classes that you can derive from when you create a custom element. Figure 9-1 shows a set of possible base classes that might be the appropriate class, and illustrates the inheritance relationship between them. Note that this is by no means a complete inheritance diagram, but simply shows some possible base classes that you should consider.

Regardless of which base class you choose, your elements derive directly or indirectly from the FrameworkElement. This will provide routing events, advanced property handling, animations, data binding, appearance support, styling, and logical tree integration.

Deriving from FrameworkElement is not an absolute necessity. The 7th chapter discusses the underlying visual graphics API, although the chapter's example derives from FrameworkElement, and you can also derive directly from visual when using the underlying drawing API. However, if you do so, you will lose all the services provided by FrameworkElement. For the elements that derive from the underlying, you can only use them in special professional environments.

Figure 9-1

Deriving directly from FrameworkElement is appropriate for an element that is designed to be grouped into other elements. For example, consider an element that is bound to a data source and that generates a data graph. You may derive it from the control. However, the elements that are drawn by an unprocessed graphic often work together on other elements such as TextBlock, thereby providing a label for the graphic and its axes. As a result, it may make sense to divide graph into graphical drawing, which is merged into a single skin, containing any number of different controls.

It is possible to place a control inside the template of another control. But once you find out that you're simply writing a custom control and putting it in another control's template, you might want to review the base class you chose.

If you write an element that behaves as a custom appearance logic, you should derive from the panel so that it is consistent with the inline appearance elements.

If you write an element that is wrapped around another element--enhancements in some ways, consider deriving from decorator. Many of the inline elements are derived from decorator. For example, Border adds a border outside the element, and Viewbox, which automatically scales the elements wrapped by it, and fills in valid space. If you want to provide a wrapper to add functionality outside the content, consider deriving from decorator.

If your element provides behavior, or supports user interaction--you cannot use inline components, then it is appropriate to derive control, either directly or indirectly. For example, if you want to make an interactive Chart component, the user can click on the data items in the chart to check them, or zoom in, which can be typically written as a control (and possibly using the elements of the underlying image you wrote earlier).

Control provides a number of derived classes that enhance the functionality of the underlying controls. If you write a control that provides a blank area where the user can prevent some content (such as a caption), you should derive from ContentControl, which provides a control that supports the content model. Consider deriving from HeaderedContentControl if your control supports content in header headers and main areas such as paging TabPage.

If you need to represent multiple child elements, first consider whether the listbox and data binding and data-style syndication will meet your needs. Data binding and styling support WPF's ListBox handling wide-range scenarios that are not appropriate for WIN32 and Windows Forms's listbox. Once you need additional functionality--inline list controls are not supported, you should consider deriving your custom element type, such as selector or its base class, such as Itemcontrol. Itemcontrol provides basic support for controls that contain list items, including optional data-binding capabilities. Selector enhances the ability to track the current one or a set of options.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.