Differences between WPF Knowledge Points-ControlTemplate and DataTemplate

Source: Internet
Author: User

This article mainly involves reprinting and excerpt (for example, mrfangzheng's blog post in the yard). The existing articles have already been clear and do not need to be repeated.

1.A ControlTemplate is used to define/change the way a class derived from Control looks, such as Buttons, ListBox, ScrollBar. however, some "Controls" you use are not derived from Control: Best example is the TextBlock class. the TextBlock cannot have a ControlTemplate.
2.DataTemplate is closelly linked with the "Content" in WPF. certain controls are made to display "Content ". content can be anything, such as Image, Text, Object .... in fact, it is defined as an "object" type. the DataTemplate simply defines how a participant content (or content type) is meant to be displayed.
3.HierarchicalDataTemplate is an extension of DataTemplate with an additional ItemSource property. It is used to render hierarchical data in controls like treeview or menu.
Example: Usually, a string is displayed using a simple TextBlock... however, if you want a custom text layout, decoration, alignement ,... you wowould need to define an Alternate ememplate.

 

1.ControlTemplate is used to specify the visual structure, behavior, and Trigger that can be shared among multiple Control instances. Unlike creating a custom control, in many cases, you do not need to write your own control. You just want to change the visualization of the control or replace the ControlTemplate of the existing control. It is easy to use ControlTemplate. You only need to set it and assign it to the Template attribute.
2.DataTemplate is used to describe the visual structure of data objects. DataTemplate is usually used to specify the intuitive representation of data. It is easy to use DataTemplate. You only need to set it and assign values to the CellTemplate, ContentTemplate, and ItemTemplate attributes.

 

1.ControlTemplate is used to describe the visual style and behavior of the control. You can use TemplateBinding to bind the attributes of the control, for example, {TemplateBinding Background }.
2.DataTemplate is used to describe the visual style of the control's Content (data object. Use Binding to bind data object attributes, such as {Binding PersonName }. That is, ControlTemplate determines the control itself. DataTemplate determines the Content.

Generally, there is a ContentPresenter in the ControlTemplate. The ContentPresenter's ContentTemplate is of the ememplate type:

Control Type
-Template attribute (controltemplate type)
-Contentpresenter
-Contenttemplate (datatemplate type)

Contentcontrol type
-The template attribute (controltemplate type) is inherited from the control
-Contenttemplate (datatemplate type)

Itemscontrol type
-The template attribute (controltemplate type) is inherited from the control
-Itemspanel attribute (itemspaneltemplate type) specifies the layout container
-Itemtemplate attribute (datetemplate type) template of each item

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.