WindowsPhone custom controls (2)-Analysis of Template Class Libraries

Source: Internet
Author: User
++ ++

This article is original on this site. You are welcome to repost it! Reprinted please indicate the source:

Http://blog.csdn.net/mr_raptor/article/details/7251948

++ ++

 

WindowsPhone custom controls (1)-control class library Analysis

The previous section mainly analyzes the control class libraries and the inheritance relationships between control classes. Through the inheritance relationships, you can know some attributes, the source and Mechanism of events.

 

This section introduces the template class library and adds instances.

Templates are used for basic class customization. All templates in the framework are subclasses of FrameworkTemplate, including:

  1. ControlTemplate
  2. ItemsPanelTemplate
  3. DataTemplate

Through the above text analysis, you can understand why there are three templates above.

 

The three templates are described below.

 

I. template details

 

Inheritance relationship:

 

 

It can be seen that the Control Object Templates, item set templates, and Data Object Templates are inherited from the FrameworkTemplate class,

1. ControlTemplate is mainly used for custom controlOperation BehaviorAndView Structure. For example, how to display the image and text when the button is pressed.

  • Apply the custom ControlTemplate by setting the Template attribute of the Control (inherited from the Control ).

2. ItemsPanelTemplate is mainly used to customizeLayoutAppearanceDisplay effect, for example, how to layout the list items in ListBox.

  • Apply the custom ItemsPanelTemplate by setting the ItemsPanel attribute of the control.

3. DataTemplate is mainly used in custom content controls.Data ViewEffect, for example, what data is displayed for each item in ListBox.

  • You can set the ItemTemplate/ContentTemplate attribute of the control to apply the custom DataTemplate. Note: Multiple custom templates may be applied to a control, for example: listBox sets the ListBox list item Items as a horizontal arrangement, and sets the layout and data in each list item. In this way, you need to set the ItemsPanelTemplate and DataTemplate of ListBox.

 Address: http://blog.csdn.net/mr_raptor/article/details/7251948

ControlTemplate class

Define the view display template of the control to customize the control. You can build your own control object tree in the template.

Note:

  • If you are defining a control template to replace an existing control class template, The XAML that you use to define the control template content should match the existing control. Otherwise, the control may not work properly on the user interface.
  • You cannot apply ControlTemplate to UserControl (the previous section describes why ).

For example, create a simple ControlTemplate for the Button. The control template contains a Grid and specifies the following behavior:

· When you hover the mouse over the Button, the Grid changes from green to red after half a second.

· When you move the mouse away from the button, the Grid immediately changes back to green.

 

<ControlTemplate TargetType = "Button"> <br/> <Grid> <br/> <VisualStateManager. visualStateGroups> <br/> <VisualStateGroup x: Name = "CommonStates"> <br/> <VisualStateGroup. transitions> <br/> <! -- Take one half second to trasition to the MouseOver state. --> <br/> <VisualTransition To = "MouseOver" GeneratedDuration = "0: 0. 5 "/> <br/> </VisualStateGroup. transitions> <br/> <VisualState x: Name = "Normal"/> <br/> <! -- Change the SolidColorBrush, ButtonBrush, to red when the <br/> mouse is over the button. --> <br/> <VisualState x: Name = "MouseOver"> <br/> <Storyboard> <br/> <ColorAnimation Storyboard. targetName = "ButtonBrush" <br/> Storyboard. targetProperty = "Color" To = "Red"/> <br/> </Storyboard> <br/> </VisualState> <br/> </VisualStateGroup> <br/> </VisualStateManager. visualStateGroups> <br/> <Grid. background> <br/> <SolidColorBrush x: Name = "ButtonBrush" Color = "Green"/> <br/> </Grid. background> <br/> </Grid> <br/> </ControlTemplate> <br/>

 

ItemsPanelTemplate class

ItemsPanelTemplate defines the layout template of the Item in ItemsControl. The default value of ItemsControl is an ItemsPanelTemplate that specifies StackPanel. For example, ListBox is an ItemsControl subcontrol. Its Item layout template ItemsPanelTemplate is the default StackPanel, and StackPanel is the vertical layout by default, the items in the default ListBox are vertically arranged. When we add items to ListBox, they are all vertically listed. If you want to customize your ListBox style to horizontal display, the StackPanel in ItemsPanelTemplate will be customized to the horizontal direction.

In the following example, The ListBox style is changed to the horizontal subitem display mode.

 

Template XAML:

<Grid> <br/> <Grid. resources> <br/> <Style x: Key = "horizontalListBoxStyle" TargetType = "ListBox"> <br/> <Setter Property = "ItemsPanel"> <br/> <Setter. value> <br/> <ItemsPanelTemplate> <br/> <StackPanel Orientation = "Horizontal" <br/> VerticalAlignment = "Center" <br/> HorizontalAlignment = "Center"/> <br/> </ItemsPanelTemplate> <br/> </Setter. value> <br/> </Setter> <br/> </Style> <br/> <src: Items x: key = "items"/> <br/> </Grid. resources> <br/> <ListBox ItemsSource = "{StaticResource items}" <br/> Style = "{StaticResource horizontalListBoxStyle}"/> <br/> </Grid> <br />

 

C # code:

Public class Items: <br/> System. collections. objectModel. observableCollection <string> <br/>{< br/> public Items () <br/>{< br/> Add ("Item 1 "); <br/> Add ("Item 2"); <br/> Add ("Item 3"); <br/> Add ("Item 4 "); <br/> Add ("Item 5"); <br/>}< br/>

The display effect is as follows:

 

Summary:

ItemsPanelTemplate is mainly used to set the layout template of controls with Item items. A common control is ListBox,

 Address: http://blog.csdn.net/mr_raptor/article/details/7251948

DataTemplate class

A template that defines the visual structure of data objects in a content control. Although the content control can only contain one UIElement, it can contain one container control and indirectly include multiple child controls, dataContent is a template class for the layout of child controls in these container controls.

The following example defines how the UI of each item in ListBox is displayed. Each Item contains four TextBlock controls in a horizontal layout. Each TextBlock control is bound with the MERs attribute.

XAML:

<Grid> <br/> <Grid. resources> <br/> <src: MERs x: Key = "Customers"/> <br/> </Grid. resources> </p> <ListBox ItemsSource = "{StaticResource MERs}" Width = "350" Margin = ","> <br/> <ListBox. itemTemplate> <br/> <DataTemplate> <br/> <StackPanel Orientation = "Horizontal"> <br/> <TextBlock Padding = "5, 0, 5, 0 "<br/> Text =" {Binding FirstName} "/> <br/> <TextBlock Text =" {Binding LastName} "/> <br/> <TextBlock Text = ", "/> <br/> <TextBlock Text =" {Binding Address} "/> <br/> </StackPanel> <br/> </DataTemplate> <br/> </ListBox. itemTemplate> <br/> </ListBox> <br/> </Grid> </p> <p>

C #:

Public class Customer <br/>{< br/> public String FirstName {get; set ;}< br/> public String LastName {get; set ;} <br/> public String Address {get; set ;}</p> <p> public Customer (String firstName, String lastName, String address) <br/>{< br/> this. firstName = firstName; <br/> this. lastName = lastName; <br/> this. address = address; <br/>}</p> <p> public class MERs: observableCollection <Customer> <br/> {<br/> public MERs () <br/> {<br/> Add (new Customer ("Michael", "andreberg ", <br/> "12 North Third Street, Apartment 45"); <br/> Add (new Customer ("Chris", "Ashton ", <br/> "34 West Fifth Street, Apartment 67"); <br/> Add (new Customer ("Cassie", "Hicks ", <br/> "56 East Seventh Street, Apartment 89"); <br/> Add (new Customer ("Guido", "Pica ", <br/> "78 South Ninth Street, Apartment 10"); <br/>}< br/>

 

Ii. Others

Address: http://blog.csdn.net/mr_raptor/article/details/7251948

DataContext class

 

DataContext is an attribute of FrameworkElement and an Object type. It is used to obtain or set the data context when FrameworkElement participates in data binding. That is to say, it is a data-bound object.

DataContext is the attribute of the fourth-generation control ancestor (to be honest, the control has a look and a look from the third-generation ancestor UIElement ),

If you bind a data source to the data source, CLR will take out the corresponding data from the data source for display. DataContext has transmission. If the externally contained control sets DataContext, the contained control does not set this attribute, the contained control can also use the DataContext of the externally contained control.

For example:

XAML:

<Phone: PhoneApplicationPage. resources> <br/> <local: WeiBoData x: Key = "MyWeiBoData"/> <br/> </phone: PhoneApplicationPage. resources> </p> <Grid x: Name = "LayoutRoot" Background = "Transparent" DataContext = "{StaticResource MyWeiBoData}"> <br/> <StackPanel Grid. row = "0" Margin = ","> <br/> <TextBlock x: name = "DateTextBlock" Text = "{Binding WeiBoDate}"> <br/> <TextBlock x: name = "TitleTextBlock" Text = "{Binding WeiBoTitle}"/> <br/> </StackPanel> <br/> </Grid> <br/>

The WeiBoData class contains the WeiBoDate and WeiBoTitle attributes. Although no binding object is specified for two textblocks, The WeiBoData class has the DataContext of the Grid control.

In the next two sections, we will share these two sections with two good examples:

Custom watermark password input controlAndPull-down refresh control.

Note: These two controls are frequently used and convenient. They are often used in Weibo and other frequently refreshed areas.

 

++ ++

This article is original on this site. You are welcome to repost it! Reprinted please indicate the source:

Http://blog.csdn.net/mr_raptor/article/details/7251948

++ ++

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.