Chapter 4. Template

Source: Internet
Author: User
Tags control label

The final design consideration for a custom element is how it connects to its visualization. If an element is directlyFrameworkelementTo generate its own visualization. (7Chapter describes how to create a graphic appearance .) In particular, if you create an element to provide a specific visualization, the element should have full control over how the visualization is managed. Once you write a control, generally, you do not hard encode a graph.

Remember, a widget provides behavior. Visualization is provided by the control template. This visualization is provided by the control template. A control may provide a set of default visualizations, but should allow these visualizations to be replaced to provide the same elasticity as the inner control. (Chapter 5 describes how to use a template to replace the visualization of a control) controls that comply with this method. Here the visualization is separated from the control and usually referenced to a control without appearance. All internal migrationWPFThe widget has no appearance.

Of course, it is impossible for controls to be completely independent from their visualization. Any control will affect the requirements that the template must meet, if the control operation is correct. These requirements vary with controls. For example,ButtonThere is a fairly simple requirement-you only need a placeholder to place the title or content.SliderControls require a wider range of requirements: Visualization must provide two buttons (increase and decrease),"Thumb", And RuntimeThumb. In addition, it also needs to be able to respond to any click and drag on these elements, as well as to be able to locate thisThumb.

There is an implicit convention between any control type and style or template. This control allows its appearance to be customized by replacing the visual tree, but the tree must provide certain features that represent the tree in turn. The nature of this Convention depends on this control. embedded controls use different styles and closely depend on their visual structures. The following section describes how to associate a widget with its template.

 

9.4.1Attribute alias

The loose convention between the control and the template is that the control defines public attributes and allows the template to determine which attributes are visible in the alias. (See Section5Chapter to obtain more information about the attribute alias .) This control is not concerned

What is in the control.

Here is a single-line Convention: the control provides properties and commands, and does not need to return values. Even so, such controls can still respond to user input if necessary. Event Routing allows events to bubble up from visualization to controls. Controls can process these events without having to know any information about the visualization nature.

To support this model, you must use the dependency attribute mechanism described earlier in this chapter to implement these attributes. Example9-11Displays a custom control and defines a separateFooDependency attribute,BrushType.

Users who support this control in the dependency property are mentioned in the template, as shown in the example.9-12.

Example9-12

< Controltemplate Targettype =" {X: type local: mycustomcontrol} " >
< Grid >
< Rectangle Fill =" {Templatebinding Foo} "   />
</ Grid >
</ Controltemplate >

All dependency attributes automatically support attribute aliases. In this case, the "Conventions" are implied by the dependency Attributes provided by a group of your controls.

9.4.2Placeholder

Some controls want to find a specific placeholder element in the template. This will either take the form of the specified type of the element, or it can be an element that marks a specific attribute.

Controls are derived fromContentcontrolSupports content templates and element-type methods. They want to findContentpresenterElement. This is an element of special intent, and its job is to act as a placeholder in other content.

In fact, this is a loose and forced convention. If the template does not containContentpresenter,ContentcontrolIt usually does not appeal. The control is not absolutely dependent on the presentation content, so it can be put there to work. Or you can reach another extreme and put someContentpresenterIn your template, sub-content can appear multiple times.

You don't need to do anything special to support it.ContentpresenterAs long as you are derived fromContentcontrolIt can work well. The user of the control can write a template, as shown in the example9-13.

Example9-13

 

< Controltemplate Targettype =" {X: type local: mycontentcontrol} " >
< Grid >
< Rectangle Fill = "White"   />
< Contentpresenter />
</ Grid >
</ Controltemplate >

9.4.3Specify placeholders through attributes

Some controls look for elements marked with a specific property. For example, derived fromItemscontrolSuchListBoxAndMenuitem, You want the template to includePanel. isitemshostSet propertyTrue. This indicatesPanelHost of the control data item to be assumed.ItemcontrolThe reason for replacing placeholders with affiliated attributes is that you can decide what type to usePanelAs the host of the data item. (ItemcontrolAlso supportedItemspresenterUse of placeholder elements. This will be used when the style does not want to use a specificPanelType and whether the defaultPanelWhen)

To implement controls using this technology, you need to define a custom affiliated dependency attribute and apply it to placeholders. This isBooleanAttribute. Example9-14Register such a ancillary property and define the usual accessors.

Example9-14

Public   Class Controlwithzaceholder: Control {
Public   Static Dependencyproperty ismyplaceholderproperty;

Static Controlwithzaceholder () {

Propertymetadata
Ismyplaceholdermetadata =   New Propertymetadata ( False ,
New Propertyinvalidatedcallback
(Onismyplaceholderchanged ));

Ismyplaceholderproperty = Dependencyproperty. registerattached (
" Ismyplaceholder " , Typeof ( Bool ),
Typeof (Controlwithzaceholder), ismyplaceholdermetadata );
}

Public   Static   Bool Getismyplaceholder (dependencyobject target) {
Return(Bool) Target. getvalue (ismyplaceholderproperty );
}
Public   Static   Void Setismyplaceholder (dependencyobject target, Bool Value) {
Target. setvalue (ismyplaceholderproperty, value );
}

Note Example 9-14IsPropertymetadataProvidesPropertyinvalidatedcallback. This indicates a method that can be called at any time. This affiliated attribute can be set or modified on any element. In this method, our control will find which element is set as a placeholder.9-15This method is displayed.

Example9-15


Private   Static   Void Onismyplaceholderchanged (dependencyobject target) {

Frameworkelement targetelement = Target As Frameworkelement;
If (Targetelement ! =   Null   && Getismyplaceholder (targetelement )) {
Controlwithzaceholder containingcontrol =
Targetelement. templatedparent As Controlwithzaceholder;
If (Containingcontrol ! =   Null ) {
Containingcontrol. placeholder=Targetelement;
}
}
}
Private Frameworkelement placeholder;


}

This example starts when the detection attribute is applied to the frameworkelement. Remember, we hope this property will be applied to a specific control template.UIElement, so if it is applied to another element insteadFrameworkelementSo we won't get anything useful.

Secondly, we useGetismyplaceholderThe accessor method detects the attribute value. The method is shown in the example.9-14Defined for the affiliated attributes. This will be slightly independent. If someone shows that this property is setFalseBut if so, we should not use the element as a placeholder.

If this attribute is setTrueTo continue obtaining the target ElementTemplatedparentAttribute. Because the element is part of the control template, this will return the control that visualization belongs. (If the element is not a member of the controlNull. Since this attribute only makes sense to the elements in the template, if there is no templated parent level, we can't do anything .) We also checked that the parent level is an instance of the control type, and ignored the attributes. If the elements applied to a template are in another type of control template.

Example9-16Shows how to use attributes in a control template to indicate which element is in the placeholder.

Example9-16

< Controltemplate Targettype =" {X: type local: controlwithzaceholder} " >
< Grid Local: controlwithzaceholder. ismyplaceholder = "True"   />
</ Controltemplate >

Some controls want to have a template that provides a set of specific elements to fulfill specific roles in the control label. For example, horizontalsliderYou can drag the widget to the template.ThumbInThumbAnd so on. The template must specify which element is. This can be achieved by defining multiple affiliated attributes using the above-mentioned display technology.

When you write a control that uses placeholders, you may choose not to execute this convention. For example, if any part of the template is missing,SliderControls do not complain. Once you only provide some elements you want to find, you can work without complaining.

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.