Custom Controls in WPF (1)

Source: Internet
Author: User
Custom Controls in WPF (1): overview
Zhou yinhui

I. do not necessarily need custom controls
Before using WPF, the use of custom controls almost became inertial thinking. For example, a button with an image is needed, but in WPF, such tasks do not need such a huge weekly chapter, this is because the control can be nested and can create a new style for the control appearance. whether or not we need to customize controls, which requires you to consider the real logic functions of existing controls, not limited to the appearance. If the current controls do not intuitively express your thoughts, you can create a widget on your own. Otherwise, you can simply change the template of the current widget to complete the task. many people often make mistakes in custom controls: Repeat the existing Logic

2. usercontrol or customcontrol?
to customize a control in WPF, using usercontrol and customcontrol is a good choice (In addition, there are more options, such as creating a custom panel, but this is beyond the scope of this article). The difference is:
usercontrol, which is more like the development style of custom controls in winform, is simpler and faster in development, it can be easily understood as: using the designer to assemble multiple existing controls as sub-elements into a usercontrol and modify its appearance, then, the background logic Code directly accesses these child elements. its biggest drawback is that it has poor support for template styles, and its reuse scope is limited.
customcontrol: the developed controls have the WPF style, which is well supported by the template style. This is because the logic code and appearance are separated when creating customcontrol, even if you change to a set of completely different visual trees, it can work very well, just like the built-in controls of WPF.
When Visual Studio is used to create controls, the difference between usercontrol and customcontrol is more obvious. When a usercontrol is added to a project, we will find that the designer adds a XAML file and a corresponding one for us. CS file (or. VB), and then you can design the usercontrol like designing a common form. If we add a customcontrol to the project, but this is not the case, the designer will generate. CS file (or. VB). This file is used to compile the background logic of the control, while the control's appearance is defined in the theme of the Software. (If you do not define a general topic for the software, it automatically generates a general themes \ Generic. XAML, and a style is automatically generated for your control in the topic), and the general topic is associated with the control. this is why customcontrol has better support for styles than usercontrol.

3. inherit from usercontorl, control, or others?
If you want to create a control and use a tool such as Visual Studio for development, the usercontrol will be automatically created for you from the system. windows. controls. usercontrol is inherited. windows. controls. control inheritance. however, in actual situations, it may be better for us to inherit from their derivative classes (better reuse of existing logic ), for example, if your control has more features similar to the button, the inheritance from the button is much less than the inheritance from the control.

In the following sections, we will gradually discuss how to create usercontrol and customcontrol and make them better support the new features of WPF.

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.