Content + contentcontrol + controltemplate (learning)

Source: Internet
Author: User

System. Windows.Controls --That is, various controls such as button and checkbox

System. Windows. controls.

The first is control, inherited from frameworkelement. Most of silverlight2.0'sControls (buttons, etc)They all come from two classes. One isControlAnd the other isContentcontrol. Contentcontrol is an extension of the control class and inherits the iaddchild interface. Two functions addchild and addtext are added to add content attributes.(Content).

For content, I will introduce it later. In addition, the member contenttemplate of the datatemplate type is used to include the template of the content attribute.

 

Code

 

Here are several forms of control attributes.

1. Attribute/Value Type

This is a common method and is also the most common method in Silverlight. It's easy.

 

 

We can do this when our attribute is not just a simple value.

 

2. Content Attributes

Some special properties of controls inherited from contentcontrol are content attributes.

 

The content attribute can be defined only once. This seems to have some limitations. Actually not. The consideration for the content attribute may be due to the mandatory addition of the layout style of the Panel type. So if you put more than one control in the content attribute at the same time, the property 'content' is set more than once. error will occur, but you can do this.

 

3. Additional attributes

This is an important assignment method. This allows you to define attributes for the parent node by attaching attributes. The most typical example is to define the elements in the canvas during the canvas layout.

 

Contentcontrol (content attribute) enriches the original controls through this method. You can use this method to build custom controls to change the expression of the original default controls. The following is an important property contenttemplate (control template ).

Controlttemplate

Added support for control templates in silverlight2.0 beta1. Controls in WPF include controltemplate and itemspaneltemplate.(That is, one is the control template and the other is the items template). Here we will discuss controltemplate.

Controltemplate describes the appearance of the control and the trigger method of the control. (unlike style, style can only change the existing attributes (size, position, color, etc.) of the original control ...) Controls can be customized, while the control template can change the internal structure of the control, that is, the visualtree. For example, if the style is to make the existing things more beautiful, contenttemplate is used to create some new custom controls.

To change the template of a control, we need to declare a controltemplate object and configure the controltemplate object accordingly.The controltemplate object is assigned to the template attribute of the control.-That is, the template attribute of the control accepts the new controltemplate.

 

This writing method may be too limited. For example, I need to be able to dynamically set attributes such as width and height to achieve different effects, we can use templatebinding to associate the attributes of the control with the parent attributes that call this template. The aboveCodeThis is the case.

 

we just mentioned that contenttemplate can change the internal structure of the control's visualtree. In fact, another important attribute of controltemplate is the triggers trigger. Events that can change controls, such as hover, mousemove, and so on. Although triggers prompts appear in smart prompts, triggers is not supported in silverlight2.0 beta1, scott wrote about the style, datatemplate, and controltemplate supported by silverlight2.0 beta1 in his blog. So triggers is not involved here. Let's study it after beta2 comes out. :)
Next time let's talk about the style and datatemplate, scott will introduce Silverlight to the WPF desktop Program in Chapter 8 of silverlight2.0, in fact, the data binding method of ListBox is a little different, so it also needs to be changed. Otherwise, there will be a little problem. If you are free to write a question about your step by step process and bring your own code.

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.