"Programming WPF" translation 9th Chapter 1. Customizing Control Basics

Source: Internet
Author: User

Before you write a custom control, the first question you need to ask is, do I really need a custom control? A primary reason for writing a custom control is that the user interface technologist can modify the appearance of the control, but as we've seen in the previous chapters, content models and templates mean that this is usually unnecessary. WPF provides an advanced, scaled customization technology that you should keep in mind-when considering writing a custom control.

To modify the appearance of an existing control by using a property

Combine some existing controls

Embed content into existing controls

Replace an existing control with a template

Create a custom control or other custom element

This order provides an incremental level of competency to exchange for minor additional effects at each level. In these simple situations, you can adjust the behavior and appearance of an inline control to suit your needs by setting the properties. The next step is to compile the controls together to form a more powerful whole. You can take this composition to the next step by embedding the content in another control. You can use a template to completely replace the appearance, as described in chapter 5th. Once technology 1-4 doesn't meet your needs, writing a custom element is like a custom control that looks like an answer.

Whether you need to write a new visual element type, an important indication is whether you plan to add a new API feature. Even in this case, you should carefully consider which custom element type to write, and the control is not the only element kind. You can get more flexibility by writing an underlying component and integrating it into the appearance of a known control. For example, a large number of elements that make WPF flexible, such as layout classes and shapes, are derived from FrameworkElement and are not actually controls-they are not derived from the control base class.

If you decide on a custom element to make the best approach, you need to do a lot of design steps. First, you have to pick out the base class. Does it derive from Frameworkelement,control, or one of the other base types provided by WPF? Then you have to define the API to determine which attributes, events, and commands your component has. Finally, if your new element provides the same elasticity as the inline component, you need to be aware of the interface between the element and its template.

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.