Windows 10: New Data Binding Technique

Here are the steps,

  1. Open a blank app and add a GridView.
    1. <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
    2.     <GridView Name="gridPlayers"> </GridView>  
    3. </Grid>  
  2. Create a folder inside assets folder, name it as "Images". 
  3. Add some images inside it.
  4. Create Models folder.
  5. Create Model class as "Player" in Models folder.
    1. public class Player  
    2. {  
    3.     public string Name {  
    4.         get;  
    5.         set;  
    6.     }  
    7.     public int Age {  
    8.         get;  
    9.         set;  
    10.     }  
    11.     public string ImageUrl {  
    12.         get;  
    13.         set;  
    14.     }  
    15. }  
  6. Create ObservableCollection of Player class in the code, at the backend of MainPage.xaml.cs
    1. using UWPBindingApp.Models;    
    2. private ObservableCollection<Player> Players = new ObservableCollection<Player>();   
  7. At Page_Loaded event, write the code to fill the data in your observable collection.
    1. Players.Add(new Models.Player() { Name = "Ajinkya Rahane", Age = 32, ImageUrl = "/Assets/Images/ajinkya-rahane.png" });   
    2. Players.Add(new Models.Player() { Name = "Virat Kohli", Age = 31, ImageUrl = "/Assets/Images/virat-kohli.png" });   
    3. Players.Add(new Models.Player() { Name = "Rohit Sharma", Age = 30, ImageUrl = "/Assets/Images/rohit-sharma.png" });   
    4. Players.Add(new Models.Player() { Name = "Suresh Raina", Age = 29, ImageUrl = "/Assets/Images/suresh-raina.png" });   
    5. Players.Add(new Models.Player() { Name = "Yuvraj Singh", Age = 34, ImageUrl = "/Assets/Images/yuvraj-singh.png" });   
    6. Players.Add(new Models.Player() { Name = "MS Dhoni", Age = 34, ImageUrl = "/Assets/Images/ms-dhoni.png" });   
    7. Players.Add(new Models.Player() { Name = "Ravindra Jadeja", Age = 29, ImageUrl = "/Assets/Images/ravindra-jadeja.png" });   
    8. Players.Add(new Models.Player() { Name = "Ashish Nehra", Age = 32, ImageUrl = "/Assets/Images/ashish-nehra.png" });   
    9. Players.Add(new Models.Player() { Name = "B Kumar", Age = 32, ImageUrl = "/Assets/Images/bhuvneshwar-kumar.png" });   
    10. Players.Add(new Models.Player() { Name = "Amit Mishra", Age = 30, ImageUrl = "/Assets/Images/amit-mishra.png" });   
    11. Players.Add(new Models.Player() { Name = "R Ashwin", Age = 28, ImageUrl = "/Assets/Images/ravichandran-ashwin.png" });   
  8. At MainPage.xaml, define ItemsSource for your GridView, as shown below:
    1. <GridView Name="gridPlayers" ItemsSource="{x:Bind Players}" Margin="20,20,0,0" >   
  9. Define DataTemplate for your GridView.
    1. xmlns:data="using:UWPBindingApp.Models"   
  10. Inside DataTemplate, we need to define x:DataType to tell the compiler about which type of dataModel we are going to use to bind our DataTemplate.
    1. <GridView.ItemTemplate>  
    2.     <DataTemplate>  
    3.         <Grid Width="350" Margin="10">  
    4.             <Grid.ColumnDefinitions>  
    5.                 <ColumnDefinition Width="Auto" />  
    6.                 <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions>  
    7.             <Image Height="100" Width="100" Source="{x:Bind ImageUrl}" />  
    8.             <StackPanel Grid.Column="1">  
    9.                 <TextBlock Text="{x:Bind Name}" FontSize="16" />  
    10.                 <TextBlock FontSize="12">  
    11.                     <Run Text="Age: " />  
    12.                     <Run Text="{x:Bind Age}" /> </TextBlock>  
    13.             </StackPanel>  
    14.         </Grid>  
    15.     </DataTemplate>  
    16. </GridView.ItemTemplate>  
  11. We have to bind Text and Image source property to the respective property of Model class, using x:Bind as below.
    1. <DataTemplate x:DataType="data:Player">   
  12. Now, run the Application.

    application
Download Source Code.
Ebook Download
View all
Learn
View all