Custom Controls for WPF control development (3)

Source: Internet
Author: User

Create usercontrol
As mentioned above, the simplest way to create a control in WPF is to derive from usercontrol. The following example demonstrates the XAML used to define the numericupdownusercontrol User Interface (UI:

 

The following example demonstrates the logic of this usercontrol.

 

As shown in this example, the development model for custom usercontrol is very similar to that for application development.

Create custom controls
Generate controls that support templates
Usercontrol provides a simple method to generate reusable functions in WPF. However, to use templatification and support for different themes, the model to be used is control. This section converts the usercontrol example in the previous section to a custom control.

Change base class
First, replace the usercontrol base class with the control class.

Move to template
Once the base class is updated, you need to move the control content to the template. Templates are defined in styles that can be located in many locations in the application. In this example, the style is located in the Application resource.

In the usercontrol example, the textblock and repeatbutton instances have been specified. The repeatbutton instance also references the event handler defined in the Code. You can remove these two instances from the custom control, because the same behavior will be obtained by means of loose coupling.

 

Process Input
In the usercontrol example, the repeatbutton instance directly references the event handler defined in the Code. For custom control, commands are a more flexible way to implement the same behavior. Controls can define commands, as shown in the following example.

 

The elements in the template can then reference these commands, as shown in the following example.

 

By defining templates and using bindings and commands, you have changed the numericupdown control from a static usercontrol with a fixed visual effect to a custom control with flexibility.

External control library
The last step is to package the numericupdown control into its own Assembly to make reuse easier.

Create a topic File
After the numericupdown class is moved to the library assembly, you need to move the style definition. First, you need to create a "themes" folder to store all themes. Next, create a file named GENERIC. XAML. This file will be used as a rollback for all resources of this Assembly.

Define a resourcedictionary in generic. XAML and place the style of the numericupdown control in resourcedictionary.

Define themeinfo attributes
To find the style in generic. XAML, the host application needs to know that the Assembly contains the control-specific resources. You can add Assembly attributes to the class to implement this task. Because the Assembly only contains general resources, set the genericdictionarylocation attribute to sourceassembly. The following example shows the content of the assemblyinfo. CS file.

 

Provides support for Windows themes
A widget may have different appearances in a large number of different WPF themes. To support multiple themes, you must use the correct style, template, and other resources required by the control to define the theme file. You also need to set themedictionarylocation attribute (property) of the themeinfoattribute attribute to reference the source set, as shown in the following example.

 

This document provides an overview of a set of best practices to consider when designing controls that are easily styled and templated. When processing topic control styles for the built-in WPF control set, we have summarized this set of best practices through a large number of experiments and errors. We have realized that successful styles are not only the functions of well-designed object models, but also the functions of styles. This document is intended for control authors rather than style authors.

Terms
"Style and templatification" is a group of technologies. The control Author can use this group of technologies to delay the visual features of the control to the style and template of the control. These technologies include:

Style (including property setter, trigger, and dashboard ).

Resources.

Control template.

Data Template.

Preparation: Understand your controls
Before reading the principles in this document, you must understand and define the general usage of controls, which is very important. Styles make public a group of possibilities that are generally unrestricted. Controls designed to be widely used by many developers in many applications face the following challenges: styles can be used to make extensive changes to the visual appearance of controls. In fact, controls with styles may not even be intended by the control author. Because styling provides unlimited flexibility in nature, you can use the "General Usage" concept to help you limit your decisions.

To understand the general usage of controls, it is best to consider the value proposition of controls. What content does your control provide that cannot be provided by other controls? The general usage does not represent any specific visual appearance, but represents the basic principle of the control and a set of reasonable expectations about its usage. With this understanding, you can make some assumptions about the model writing and style defining behaviors of the control in general. For example, if you know the common usage of ComboBox, it does not mean that you know whether a specific ComboBox has rounded corners, however, you will understand that ComboBox may need a pop-up window and some method to switch its switch status.

General principles
The template Conventions are not strictly implemented. The template conventions of a control can contain elements, commands, bindings, and triggers, and even include required or attribute settings that should be used to make the control work normally.

Minimize conventions.

Designed around the following expectations: the control template is usually incomplete during design (that is, when a design tool is used. WPF does not provide the infrastructure for the "writing" State. Therefore, controls must be generated based on the expectation that such a State may be valid.

No exception is thrown when no aspects of the template are followed. According to this principle, when the sub-level of the Panel is too large or too small, the panel should not cause exceptions.

Break down peripheral functions into template helper elements. Each control should focus on its core functions and true value proposition, and each control should be defined by the general usage of the control. Therefore, use the writer and helper elements in the template to implement peripheral behavior and visualization, that is, behavior and visualization that do not constitute the core functions of the control. Helper elements can be divided into three types:

The independent helper type is a reusable public control or primitive that is used in the template in an "anonymous" manner. This means that the Helper elements and style controls cannot recognize each other. Technically, any element can be an anonymous type, but here the term "independent" is used to describe the types that encapsulate specialized functions to achieve the target solution.

The Type-based helper element is a new type that encapsulates specialized functions. In general, these elements are designed to be narrower than the functions of common controls or elements. Unlike the independent helper element, the type-based helper element can recognize their use context, and usually must share data with controls that contain the templates to which they belong.

The named Helper element is a public control or primitive that the control should be able to find based on its name in its template. These elements have a known name in the template, which allows the control to locate these elements and interact with them programmatically. In any template, only one element with a given name is allowed.

The following table shows a list of helper elements used by the current widget style:

 

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.