Introduction To Data binding UWP

Want to control your GUI dynamically with the present data? If yes, then Data Binding is the answer to your question.

Data binding is the process that establishes a connection between the UI layer with our Data Layer. So when you change your Data your GUI is updated and vice versa.

Let’s start with an example.

Firstly, create a new project,

new project

Design a UI template in MainPage.Xaml to make our layout like this,

mainpage

  1. <ListView Name="list_students">  
  2.     <!--Name the listView to refer-->  
  3.     <ListView.ItemTemplate>  
  4.         <DataTemplate>  
  5.             <StackPanel Orientation="Horizontal" Margin="10,20,0,0">  
  6.                 <Image Stretch="Fill" Width="100" Height="100"></Image>  
  7.                 <StackPanel Orientation="Vertical">  
  8.                     <TextBlock></TextBlock>  
  9.                     <TextBlock></TextBlock>  
  10.                     <TextBlock TextBlock>  
  11.                         <TextBlock TextBlock>  
  12.                 </StackPanel>  
  13.             </StackPanel>  
  14.         </DataTemplate>  
  15.     </ListView.ItemTemplate>  
  16. </ListView>  
Name your ListView so you can refer it later.

Now add a new class in the project called student.cs. Make the class Public, so that it is accessible. This will be your model class containing the properties of the data being used.
  1. namespace Databinding   
  2. {  
  3.     public class Student   
  4.     {  
  5.         public int id   
  6.         {  
  7.             get;  
  8.             set;  
  9.         }  
  10.         public string Name   
  11.         {  
  12.             get;  
  13.             set;  
  14.         }  
  15.         public string Class   
  16.         {  
  17.             get;  
  18.             set;  
  19.         }  
  20.         public string RollNo   
  21.         {  
  22.             get;  
  23.             set;  
  24.         }  
  25.         public string ImagePath   
  26.         {  
  27.             get;  
  28.             set;  
  29.         }  
  30.     }  
  31. }  
Now the data with the respective properties in our MainPage.xaml. Be careful as it is case sensitive.
  1. <StackPanel Orientation="Horizontal" Margin="10,20,0,0">  
  2.     <Image Source="{Binding ImagePath}" Stretch="Fill" Width="100" Height="100"></Image>  
  3.     <StackPanel Orientation="Vertical">  
  4.         <TextBlock Text="{Binding id}"></TextBlock>  
  5.         <TextBlock Text="{Binding Name}"></TextBlock>  
  6.         <TextBlock Text="{Binding Class}"></TextBlock>  
  7.         <TextBlock Text="{Binding RollNo}"></TextBlock>  
  8.     </StackPanel>  
  9. </StackPanel>  
We have bind the source of image with our ImagePath property, text properties with their respective values. These all will be filled dynamically.

Now we need to populate our data, for that open MainPage.xaml.cs and write a function to fill the list.
Include the header,

using System.Collections.ObjectModel;

And then make an observable collection of the type of the student class.
  1. public ObservableCollection < Student > stdlist = new ObservableCollection < Student > ();  
  2. public void PopulateData()   
  3. {  
  4.     stdlist.Add(new Student   
  5.     {  
  6.         id = 1, Name = "Ahmed", RollNo = "100-BSCS-15", Class = "FreshMan", ImagePath = "Assets\\Image DB.png"  
  7.     });  
  8.     stdlist.Add(new Student   
  9.     {  
  10.         id = 2, Name = "Anthony", RollNo = "21-BSCS-14", Class = "Sophomore", ImagePath = "Assets\\Image DB.png"  
  11.     });  
  12.     stdlist.Add(new Student   
  13.     {  
  14.         id = 3, Name = "David", RollNo = "72-PHY-13", Class = "Junior", ImagePath = "Assets\\Image DB.png"  
  15.     });  
  16.     stdlist.Add(new Student   
  17.     {  
  18.         id = 4, Name = "Umer", RollNo = "125-BSEE-12", Class = "Senior", ImagePath = "Assets\\Image DB.png"  
  19.     });  
  20.     list_students.ItemsSource = stdlist;  
  21. }  
I am using the same image for every entry, you can use different. Right path should be given and Image should be added into your project first.
Then populate the data in that list and call the function in the constructor so it should run as the program loads.
  1. public MainPage()   
  2. {  
  3.     this.InitializeComponent();  
  4.     PopulateData();  
  5. }  
Now run you project and it will be like this. Getting all the data dynamically and using the layout multiple times by itself. Increase or decrease the record and will also add or remove them from the following GUI,

gui

 


Similar Articles