The translation of programming WPF 9th 2. Select a base class

Source: Internet
Author: User

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

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

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

derived from FrameworkElement is not an absolute need. Chapter 7 discusses the underlying visual graphics API, although the example of this chapter derives from FrameworkElement, you can also derive directly from visualwhen using the underlying drawing API at the time. However, if you do this, you will lose all the services provided by FrameworkElement. For elements that derive from the underlying, you can only use them in a special professional environment.

Figure 9-1


Derived directly from FrameworkElement, is appropriate for an element that is designed to be combined into other elements. For example, consider an element that is bound to a data source and generates a data chart. You may make it derived from Control. Nevertheless, the elements that are drawn by the unprocessed graphic usually work in conjunction with other elements such as TextBlock, thus providing a label for the graph and its axes. Therefore, it may make sense to divide the graph into graphs, which will be merged into one appearance, containing any number of different controls.

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

If you write an element that behaves as a custom appearance logic, you should derive from Panelto align with the inline skin elements.

If you write an element that is wrapped in another element--in some ways enhanced, consider deriving from Decorator. Many of the inline elements are derived from Decorator. For example,Borderadds a border to the outside of the element , and Viewbox, which automatically scales the elements it is wrapped in to fill in the effective space. If you want to provide a wrapper to add functionality outside of the content, consider deriving from Decorator.

If your element provides behavior, or supports user interaction--you cannot use inline components, this is derived from Control is appropriate, in a direct or indirect way. For example, if you want to create an interactive Chart component, users 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).

The

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 something (such as a title), you should derive from the contentcontrol This base class provides controls that support the content model. If your control supports content in header headings and in the main area (such as pagination tabpage headeredcontentcontrol< Span style= "font-family: Song Body" >.

If you need to represent multiple child elements, first consider The combination of ListBox and data binding and data styling meets your needs. Data binding and styling support for WPF 's listbox handles a wide range of scenarios that are not appropriate for the Win32 and Windows Forms 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.

The translation of programming WPF 9th 2. Select a base class

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.