The magic of "Windows Phone" CollectionViewSource

Source: Internet
Author: User

When binding collection data in Windows Phone, it is sometimes necessary to have hierarchical data, usually in the form of master-slave attempts to display. The usual approach is to bind the data source of the second listbox (main view) to the first listbox

The SelectedItem (from the view), or the SelectionChanged event of the first ListBox, sets the binding. But the use of CollectionViewSource class can be more convenient to achieve;

CollectionView is a collection view class that supports sorting, grouping, and filtering of data. The image of the data is arranged in combination;

CollectionViewSource is a XAML proxy for CollectionView, which can be used in XAML;

Case Description: Use the master-slave attempt to display a list of two employees, as follows:

Pre-work, create three classes to initial data source;

(1) Employee.cs

    public class Employee {A. n        int number {get; set;}//Work No. n        string  name {get; set;}//Name        Pub Lic string  sex {get; set;}//Gender public        int Birthyear {get; set;}//Year of birth    }

(2) Department.cs

    public class department:observablecollection<employee>    {public            string Depname {get; set;}            Public observablecollection<employee> Employees {get; set;}    }

(3) DepartmentList.cs

    public class Departmentlist:observablecollection<department> {public departmentlist () {                observablecollection<employee> employee1 = new Observablecollection<employee> { New Employee{number=2012,name= "Netboy", sex= "Boy", birthyear=1992}, new Employee{number=2013,name= "Dandan", Sex= "Girl", birthyear=2000}, New Employee{number=2014,name= "Xiaobai", sex= "Girl", birthyear=2012}}            ;  observablecollection<employee> employee2 = new Observablecollection<employee> {NEW Employee{number=2020,name= "Kaizi", sex= "Girl", birthyear=2011}, New Employee{number=2021,name= "Yangzai", Sex=            "Gril", birthyear=2010}}; This.            ADD (new Department {depname = "technical Department", Employees = employee1}); This.            ADD (new Department {depname = "mofcom", Employees = Employee2}); Observablecollection<department> Deparment = new observablecollection<department>//{//New Department{depname= "Tengfei", Employe        ES=EMPLOYEE1},//New department{depname= "Google", employees=employee2}//}; }    }

Note: You need to refer to the namespace--using System.Collections.ObjectModel when using observablecollection<t>;

Add a namespace mapping in the phone:phoneapplicationpage tag of the new page. The code is as follows:

Xmlns:local= "Clr-namespace: Data Binding"//My project is "data binding"

To add a resource dictionary:

   <phone:PhoneApplicationPage.Resources> <local:departmentlist x:key= "deplist"/> <collection Viewsource x:key= "Departmentview" source= "{StaticResource deplist}"/> <datatempl                        Ate x:key= "Dtemployees" > <stackpanel height= "" "Horizontalalignment=" Center " Width= "480" verticalalignment= "Top" orientation= "Horizont                           Al "> <textblock height=" horizontalalignment= "left"                           Width= "text=" {Binding number} "/> <textblock height=" 50 "                            Width= "text=" {Binding Name} "/> <textblock height=" 50 " Width= "text=" {Binding birthyear} "/> <textbl         Ock height= "50"                  Width= "text=" {Binding Sex} "/> </StackPanel> &l T;/datatemplate> </phone:PhoneApplicationPage.Resources>

Add the following code to the layout page:

            <textblock width= "height=" fontsize= "36"                       text= "Please select Department:" Horizontalalignment= "left" verticalalignment= "Top"                     margin= "10,30,0,0"/> <listbox name= "LB1" height= "100" Width= "156" displaymemberpath= "Depname" itemssource= "{Binding source={static                     Resource Departmentview}} "margin=" 40,86,260,0 "horizontalalignment=" Center " Verticalalignment= "Top" fontsize= "/>" <textblock height= "Widt"                       H= "111" horizontalalignment= "left" verticalalignment= "Top" text= "{Binding path=depname,source={staticresource Departmentview}}" foreground= "Red" margin= "12, 210,0,0 "fontsize=" 32 "/> <textblock height= "horizontalalignment=" right "text=" employees List "verticalalignment=" Top "margin=" 0,210,169,0 "width=" 158 "fontsize=" "/> <text"                       Block height= "horizontalalignment=" left "width=" 120 " text= "Gender" margin= "344,278,0,279" fontsize= "/> <textblock height=" "text=" date of birth margin= "204,278,112 , 279 "fontsize=", "/> <textblock height=" "Width=" "Tex"                       t= "Work number" margin= "6,278,330,279" fontsize= "/> <textblock height=" "Width=" 98 " text= "name" margin= "0,278,260,279" horizontalalignment= "right" fontsize= "+"/> <listbo X name= "lb2" height= "verticalalignment=" "Top" itemssource= "{Binding path=employees, Source={staticresource Departmentview}} "itemtemplate=" {StaticResource dtemployees} "margin=" 12,334,- 46,0 "Fontsize="/>

The magic of "Windows Phone" CollectionViewSource

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.