[Silverlight getting started series] difference between controltemplate and datatemplate

Source: Internet
Author: User

The previous example solved a small problem, but we need to understand the difference between controltemplate and datatemplate to understand why it can solve the problem.

Controltemplate

Specifies the visual structure, behavior, and trigger that can be shared among multiple instances of a control. 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.A buttonstyle is shown below, which is used to set the controltemplate of the button :( msdn: Use controltemplate to customize the appearance of the existing control)

Datatemplate

describes the visual structure of a data object. Datatemplate is usually used to specify the intuitive representation of data. The datatemplate object is particularly useful when you bind itemscontrol (such as ListBox) to the entire set. Unless otherwise specified, ListBox displays the string representation of the object in the collection. In this case, you can use datatemplate to define the appearance of the data object. The content of the datatemplate changes to the visible structure of the data object. You can use data binding in datatemplate. For example, assume that ListBox is bound to the set of customer objects and the itemtemplate attribute is set to datatemplate. When creating a ListBox, A listboxitem is created for each customer in the set, and the datacontext of listboxitem is set as the corresponding customer. That is to say, the datacontext of the first listboxitem is set to the first customer, and the datacontext of the second listboxitem is set to the second customer, and so on. You can bind the elements in the datatemplate to the attributes of the customer object. You can also use datatemplate to share the uielement object among multiple contentcontrol objects. For example, to apply the Program , multiple buttons have the same image. You can create a datatemplate that contains the image and use it as the contenttemplate for these buttons. For more information, see contentcontrol. contenttemplate. You can use datatemplate as the direct sub-level of the object. itemtemplate attribute element. You can also define a datatemplate as a resource and reference it as the value of the itemtemplate attribute. Defines the XAML usage of the content used to create a data template and does not make it public as configurable attributes. This is a special behavior built into the XAML processing of datatemplate object elements.

It is easy to use datatemplate. You only need to set it and assign values to the attributes celltemplate, contenttemplate, itemtemplate.The following example uses datatemplate to display items in ListBox. In this example, ListBox is bound to the set of customer objects. Datatemplate contains textblock controls that are bound to the firstname, lastname, and address attributes.

For example, we can define the style of the value displayed in a DataGrid column and implement it with a converter. This requires datatemplate, because it specifiesData.

Datatemplate layers of several control types:

Summary

In a word, controltemplate is used to describe controls.ItselfDatatemplate is used to describe the visual style and behavior of the control.Content (Data Object). Do you understand?

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.