Practical development tips for Windows Phone (29): dynamic binding of hosts

Source: Internet
Author: User

A friend asked me a few days ago how to dynamically bind the timeline item, that is, the piovtitem item is dynamic, and the data in the timeline titem is also dynamic. The mvvm mode can be easily implemented. In viewmodel, a set is set to indicate the number of items currently in use. The classes in the Set contain the data source in the currently available titem. The following is a simple demo:

First, let's take a look at how to bind in XAML.

 <! -- Layoutroot is the root grid where all page content is placed -->  <  Grid  X  :  Name  = "Layoutroot"  Background  = "Transparent">  <! -- Modify control -->  <  Controls  : Bytes  Title  = "My application"  Itemtemplate  = "{  Staticresource  Dt_timeout  }"  Headertemplate  = "{  Staticresource  Dt_header  }"  Itemssource  = "{  Binding Binddata  } "> </  Controls  :  Bytes  > </  Grid  > 

The metadata data source is bound to binddata in viewmodel. itemtemplate indicates the metadata titem template, and headertemplate indicates the header template in the metadata titem. The two templates are as follows:

 <  Phone  :  Phoneapplicationpage. Resources  > <  Datatemplate  X :  Key  = "Dt_pivot"> <  ListBox  Itemssource  = "{  Binding  Listdata  } "> <  ListBox. itemtemplate  > <  Datatemplate  > <  Textblock  Text  = "{ Binding  } "/> </  Datatemplate  > </  ListBox. itemtemplate  > </  ListBox  > </  Datatemplate  > <  Datatemplate  X  :  Key  = "Dt_header"> <  Textblock Text  = "{  Binding  Name  } "/> </  Datatemplate  > </  Phone  :  Phoneapplicationpage. Resources  > 

Headertemplate is very simple. It uses a textblock to represent the current title. Put a ListBox in the itemtemplate of the notebook. The data source is listdata under binddata.

Data Source in viewmodel:

PrivateObservablecollection<Testbench> _ Binddata;PublicObservablecollection<Testbench> Binddata {Get{Return_ Binddata ;}Set{_ Binddata =Value; Raisepropertychanged ("Binddata");}}

Testbench is a self-defined class that contains the piovtheader and javastitem data sources:

Public classTestbench{/// <Summary> ///Property for Authorization Header/// </Summary>Public StringName {Get;Set;}/// <Summary> ///Data for each item datasource (eg. ListBox)/// </Summary>PublicList<String> Listdata {Get;Set;}}

OK, the binding has been established, and now it is how to initialize the data source. For the sake of simplicity, generate the binding source data in the simplest cycle:

  Public void  adddata ( int  size) {binddata =  New   observablecollection   test.pdf  ();  for  ( int  I = 0; I 
  
    testbench  T = 
    New  
    test.pdf  (); T. name = 
    "piovt item"  + I; T. listdata = 
    New  
    List  
    string  (); 
    for  (
    int  J = 0; j <10; j ++) {T. listdata. add (
    "list item"  + J);} binddata. add (t) ;}}
  

Here, size indicates that there are several latest titem data sources. Here, the future data source can be synchronized or asynchronously. As long as testbench implements policypropertychanged, and the attribute listdata notification changes.

You can findSource code, Hope that helps.

Related Article

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.