Comparison between user-defined controls inherited from UserControl and Control

Source: Internet
Author: User
Derived from usercontrol

The simplest way to create a control in WPF is from {
Track ('ctl00 _ mtcs_main_ctl262_ctl00_contenthere | ctl00_mtcs_main_ctl262_ctl00_ctl03 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.usercontrol.aspx "> usercontrol derived. If the generation inherits from {
Track ('ctl00 _ mtcs_main_ctl262_ctl00_contenthere | ctl00_mtcs_main_ctl262_ctl00_ctl04 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.usercontrol.aspx "> controls for usercontrol, you need to add existing components {
Track ('ctl00 _ mtcs_main_ctl262_ctl00_contenthere | ctl00_mtcs_main_ctl262_ctl00_ctl05 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.usercontrol.aspx "> UserControl, naming these components and then referencing event handlers in the Extensible Application Markup Language (XAML. After performing these operations, you can reference these naming elements and define event handlers in the code. This development model is very similar to the model used for WPF application development.

If the generation is correct ,{
Track ('ctl00 _ MTCS_main_ctl262_ctl00_contenthere | ctl00_MTCS_main_ctl262_ctl00_ctl06 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.usercontrol.aspx "> UserControl can take advantage of rich content, styles, and triggers. However, if the control inherits from {
Track ('ctl00 _ MTCS_main_ctl262_ctl00_contenthere | ctl00_MTCS_main_ctl262_ctl00_ctl07 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.usercontrol.aspx "> UserControl, users using this control will not be able to use {
Track ('ctl00 _ MTCS_main_ctl262_ctl00_contenthere | ctl00_MTCS_main_ctl262_ctl00_ctl08 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.datatemplate.aspx "> DataTemplate or {
Track ('ctl00 _ MTCS_main_ctl262_ctl00_contenthere | ctl00_MTCS_main_ctl262_ctl00_ctl09 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.controltemplate.aspx "> ControlTemplate customize its appearance. Therefore, it is necessary {
Track ('ctl00 _ MTCS_main_ctl262_ctl00_contenthere | ctl00_MTCS_main_ctl262_ctl00_ctl10 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.control.aspx "> Control class or its derived class ({
Track ('ctl00 _ MTCS_main_ctl262_ctl00_contenthere | ctl00_MTCS_main_ctl262_ctl00_ctl11 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.usercontrol.aspx "> Except UserControl) to create custom controls that support templates.

Advantages derived from usercontrol

If all of the following conditions are met, consider {
Track ('ctl00 _ MTCS_main_ctl262_ctl00_contenthere | ctl00_MTCS_main_ctl262_ctl00_ctl12 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.usercontrol.aspx "> UserControl derived:

  • You want to generate a control in a way similar to generating an application.

  • Controls only consist of existing components.

  • You do not need to support complex custom items.

Derived from control

From {
Track ('ctl00 _ mtcs_main_ctl262_ctl00_contenthere | ctl00_mtcs_main_ctl262_ctl00_ctl13 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.control.aspx "> the control class derives the model used by most existing WPF controls. In the create inherited from {
Track ('ctl00 _ mtcs_main_ctl262_ctl00_contenthere | ctl00_mtcs_main_ctl262_ctl00_ctl14 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.control.aspx "> controls of the control class can be defined using a template. In this way, the computing logic can be separated from the visual representation. By using commands and binding (rather than events) and avoiding references as much as possible {
Track ('ctl00 _ mtcs_main_ctl262_ctl00_contenthere | ctl00_mtcs_main_ctl262_ctl00_ctl15 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.controltemplate.aspx "> elements in controltemplate also ensure that the UI and logic are separated. If the UI and logic of the control are correctly separated, the user of the control can redefine its {
Track ('ctl00 _ mtcs_main_ctl262_ctl00_contenthere | ctl00_mtcs_main_ctl262_ctl00_ctl16 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.controltemplate.aspx "> ControlTemplate to customize its appearance. Even though custom {
Track ('ctl00 _ MTCS_main_ctl262_ctl00_contenthere | ctl00_MTCS_main_ctl262_ctl00_ctl17 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.control.aspx "> Control does not generate {
Track ('ctl00 _ MTCS_main_ctl262_ctl00_contenthere | ctl00_MTCS_main_ctl262_ctl00_ctl18 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.usercontrol.aspx "> UserControl that simple, custom {
Track ('ctl00 _ MTCS_main_ctl262_ctl00_contenthere | ctl00_MTCS_main_ctl262_ctl00_ctl19 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.control.aspx "> Control still provides maximum flexibility.

Benefits derived from control

If any of the following conditions is met, consider {
Track ('ctl00 _ MTCS_main_ctl262_ctl00_contenthere | ctl00_MTCS_main_ctl262_ctl00_ctl20 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.control.aspx "> Control derivation, instead of using {
Track ('ctl00 _ MTCS_main_ctl262_ctl00_contenthere | ctl00_MTCS_main_ctl262_ctl00_ctl21 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.usercontrol.aspx "> UserControl class:

  • You want to pass {
    Track ('ctl00 _ mtcs_main_ctl262_ctl00_contenthere | ctl00_mtcs_main_ctl262_ctl00_ctl22 ', this );
    } "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.controltemplate.aspx "> controltemplate for custom.

  • You want the controls to support different themes.

Derived from frameworkelement

From {
Track ('ctl00 _ MTCS_main_ctl262_ctl00_contenthere | ctl00_MTCS_main_ctl262_ctl00_ctl23 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.usercontrol.aspx "> UserControl or {
Track ('ctl00 _ MTCS_main_ctl262_ctl00_contenthere | ctl00_MTCS_main_ctl262_ctl00_ctl24 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.control.aspx "> controls derived from controls depend on combining existing elements. In many cases, this is an acceptable solution because {
Track ('ctl00 _ MTCS_main_ctl262_ctl00_contenthere | ctl00_MTCS_main_ctl262_ctl00_ctl25 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.frameworkelement.aspx "> Any object inherited by FrameworkElement can be located in {
Track ('ctl00 _ MTCS_main_ctl262_ctl00_contenthere | ctl00_MTCS_main_ctl262_ctl00_ctl26 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.controltemplate.aspx "> ControlTemplate. However, in some cases, simple element combinations cannot meet the display requirements of controls. In these cases, make the component based on {
Track ('ctl00 _ MTCS_main_ctl262_ctl00_contenthere | ctl00_MTCS_main_ctl262_ctl00_ctl27 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.frameworkelement.aspx "> FrameworkElement is the right option.

Generate Based on {
Track ('ctl00 _ MTCS_main_ctl262_ctl00_contenthere | ctl00_MTCS_main_ctl262_ctl00_ctl28 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.frameworkelement.aspx "> FrameworkElement components have two standard methods: Direct rendering and custom element combination. Operations involved in direct rendering include rewriting {
Track ('ctl00 _ mtcs_main_ctl262_ctl00_contenthere | ctl00_mtcs_main_ctl262_ctl00_ctl29 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.frameworkelement.aspx "> {
Track ('ctl00 _ mtcs_main_ctl262_ctl00_contenthere | ctl00_mtcs_main_ctl262_ctl00_ctl30 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.uielement.onrender.aspx "> onrender method and provides {
Track ('ctl00 _ mtcs_main_ctl262_ctl00_contenthere | ctl00_mtcs_main_ctl262_ctl00_ctl31 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.media.drawingcontext.aspx "> drawingcontext operation. This method consists {
Track ('ctl00 _ mtcs_main_ctl262_ctl00_contenthere | ctl00_mtcs_main_ctl262_ctl00_ctl32 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.image.aspx "> image and {
Track ('ctl00 _ mtcs_main_ctl262_ctl00_contenthere | ctl00_mtcs_main_ctl262_ctl00_ctl33 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.border.aspx "> border use. Operations involved in custom element combinations include the use {
Track ('ctl00 _ mtcs_main_ctl262_ctl00_contenthere | ctl00_mtcs_main_ctl262_ctl00_ctl34 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.media.visual.aspx "> the appearance of the Visual Object composite component. For examples, see {
Track ('ctl00 _ mtcs_main_ctl262_ctl00_contenthere | ctl00_mtcs_main_ctl262_ctl00_ctl35 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/ms742254.aspx "> use a drawingvisual object. {
Track ('ctl00 _ mtcs_main_ctl262_ctl00_contenthere | ctl00_mtcs_main_ctl262_ctl00_ctl36 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.primitives.track.aspx "> track is an example of a control that uses a custom element combination in WPF. In the same control, you can also use both direct rendering and custom element combinations.

 

Advantages derived from FrameworkElement)

If any of the following conditions is met, consider {
Track ('ctl00 _ MTCS_main_ctl262_ctl00_contenthere | ctl00_MTCS_main_ctl262_ctl00_ctl37 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.frameworkelement.aspx "> FrameworkElement derivation:

  • We hope to precisely control the appearance of the control, not just the effect provided by simple combinations of elements.

  • You want to define the display logic of the control.

  • To {
    Track ('ctl00 _ mtcs_main_ctl262_ctl00_contenthere | ctl00_mtcs_main_ctl262_ctl00_ctl38 ', this );
    } "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.usercontrol.aspx "> usercontrol and {
    Track ('ctl00 _ mtcs_main_ctl262_ctl00_contenthere | ctl00_mtcs_main_ctl262_ctl00_ctl39 ', this );
    } "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.control.aspx "> A novel combination of existing elements outside of Control.

Comparison:

The main difference between the two is that they inherit from {
Track ('ctl00 _ mtcs_main_ctl1__ctl00_contenthere | ctl00_mtcs_main_ctl0000_ctl00_ctl06 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.usercontrol.aspx "> NumericUpDown of UserControl is not used {
Track ('ctl00 _ mtcs_main_ctl1__ctl00_contenthere | ctl00_mtcs_main_ctl0000_ctl00_ctl07 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.controltemplate.aspx "> ControlTemplate directly inherited from {
Track ('ctl00 _ mtcs_main_ctl1__ctl00_contenthere | ctl00_mtcs_main_ctl0000_ctl00_ctl08 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.control.aspx "> Control controls are the opposite. In other words, use {
Track ('ctl00 _ mtcs_main_ctl0000_ctl00_contenthere | ctl00_mtcs_main_ctl0000_ctl00_ctl18 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.controltemplate.aspx "> ControlTemplate controls have a customizable appearance that inherits from {
Track ('ctl00 _ mtcs_main_ctl1__ctl00_contenthere | ctl00_mtcs_main_ctl0000_ctl00_ctl19 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.usercontrol.aspx "> the control of UserControl does not exist. Of course, we may also allow our controls to inherit from {
Track ('ctl00 _ mtcs_main_ctl1__ctl00_contenthere | ctl00_mtcs_main_ctl0000_ctl00_ctl06 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.windows.controls.usercontrol.aspx "> UserControl, and then use other alternative means to customize the appearance, unfortunately, almost certainly that must be more complex.

 

Some content from: http://msdn.microsoft.com/zh-cn/library/ms745025.aspx

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.