Binding Collection To ListView Control In UWP

Introduction

List is a common and basic construct of many apps. For example, Contacts app in your smartphone. In this article I will show how you can make list of your own template in UWP and bind it to a collection of items you want to display. Just follow the simple steps below.

Content

  1. Creating A project
  2. Creating Model class
  3. Adding List control to Page
  4. Adding DataTemplate to List
  5. Map Properties of Model class to DataTemplate
  6. Creating ObserveableCollection (List) of item to bind to UI
  7. Bind ObserveableCollection to List control
Step 1: Open Visual studio and create a new Windows Universal project. Name it 'ListBinding' or whatever you want. VS automatically added MainPage.xaml to your project double click to view it.

Step 2: Add Model Class

As Class is like a blue print. It has some properties and we can make as many objects from it which have different values for those properties. In case of List same phenomena exist. List is made of items and each item have some template (e.g an image and text next to it). So we make a class (say it as model) whose object will bind to one item of list.

Add new Class 'Car' in the project and add the following properties to it,
  1. public string Brand { getset; }  
  2. public string Model { getset; }  
  3. public string Color { getset; }  
code

Step 3: 
Double click MainPage.xaml and add the following code to it.
  1. <ListView x:Name="MyList">    
  2. </ListView>   
Step 4: 
Now add code between ListView tag.
  1. <ListView.ItemTemplate>  
  2.     <DataTemplate> </DataTemplate>  
  3. </ListView.ItemTemplate>  
<ListView.ItemTemplate> is telling we are adding template to ListView and </DataTemplate> telling here's the template.

Step 5: 
Now we will tell where to display properties of Car. Add code to <DataTemplate>.
  1. <Grid>  
  2.     <TextBlock Text="{Binding Brand}"></TextBlock>  
  3.     <TextBlock Text="{Binding Model}"></TextBlock>  
  4.     <TextBlock Text="{Binding Color}"></TextBlock>  
  5. </Grid>  
You could have any number of properties of model and controls and bind them the way you want.
 
Till now what we have done? We added a ListView to display list of items and then added DataTemplate to it. <DataTemplate> can contain only one item so we added a Grid container and added code to it. Inside Grid we defined three text blocks for our three properties of Car class. Bind it to properties of our Model (Car). Make sure name after Binding is the same as property of class to which you want to bind that TextBlock.
 
MainPage.xaml will look like the following,

xml Code
Step 6: Now we should have some data list to show. Expand MainPage.xaml and double click MainPage.xaml.cs file. Add the following code. (Add using System.Collections.ObjectModel; namespace as well to use ObersvableCollection Class. You could use List<T> as well but ObservableCollection notify the ListView when values of its objects are changed).
  1. ObservableCollection<Car> dataList = new ObservableCollection<Car>();  
  2. Car c1 = new Car() { Brand = "Ferrari", Model = " Lamborghinis", Color = "Red" };  
  3. Car c2 = new Car() { Brand = "Honda", Model = "GLI", Color = "Black" };  
  4. Car c3 = new Car() { Brand = "Porsche", Model = "968 snowplow", Color = "White" };  
  5. dataList.Add(c1);  
  6. dataList.Add(c2);  
  7. dataList.Add(c3);  
  8. MyList.ItemsSource = dataList;  
Note: I am using temporary data objects for 3 cars here but these could be any objects got from database or other data sources. It will look like:

main page
Run the project by clicking local machine (windows 10) and you will see overlapped text, why? Because inside Grid we just declared three TextBlocks. We didn't set their layout, now just enclose them with <StackPanel> like the following,
  1. <StackPanel>  
  2.     <TextBlock Text="{Binding Brand}"></TextBlock>  
  3.     <TextBlock Text="{Binding Model}"></TextBlock>  
  4.     <TextBlock Text="{Binding Color}"></TextBlock>  
  5. </StackPanel>  
Now run again to see the following,

run

Up Next
    Ebook Download
    View all
    Learn
    View all