Click here to Skip to main content
15,891,845 members
Articles / Desktop Programming / XAML
Tip/Trick

Data Binding To Grid View Controls

Rate me:
Please Sign up or sign in to vote.
3.95/5 (6 votes)
25 Nov 2015CPOL3 min read 12.9K   8   2
How to create Data Binding?

Introduction

Data Binding

Data Binding is the process of taking some data and associating it to visual elements on user interface(UI).

Overview of Data Binding

We will take any object and with their properties and present each instance of object in collection on screen to user in “Grid “like fashion. We get to create a little template of how each individual instance of object is represented in the user interface. The “ListView” is also the same but it works in single column fashion.

Create the Project in Visual Studio 2015

In order to perform data binding, you need to create a project in your Visual Studio 2015, the steps are simple.

  1. Select “Universal” as shown by the arrow.
  2. Select “Blank App” as shown in the circle.
  3. Name your solution , for instance “BindDataExample“.

1

Designer Window

A designer window will be open once you create the project with name ”MainPage.xaml“. In this window, there is a designer surface and you can also write your XAML code … now for instance, we select our object a “Book” and in order to perform data binding, simply create a user interface in XAML, the code is shown in the image in the rectangle.

2

Create Data Model

  1. Go to “Solution Explorer” shown by the circle.
  2. Right click the “BindDataExample” shown by the arrow.
  3. Add new folder, name it “Models”, shown by the circle.

3

Create C# class in Models Folder

  1. Go to Models folder.
  2. Right click and select add new item.
  3. Select Class, name it “Book.cs“.

4

Book Class with Properties

Once you perform all the above mentioned tasks, there will be a C# class open for us and our object which in this case is a book, we can create its class and define its properties in that very C# class we have.

  1. Create a class “Book“.
  2. Define its properties.
  3. Copy the C# code in a red rectangular box.

11

Creating New Instances & Add in Collection

After you created your Book class and set its properties, now it's time to create instances of your class and populate it into a collection, list of book, we will generate another class, name as “BookManager” in the same file, for this purpose. And then return the list to caller. The code will help you out.

12

Add Grid View, Bind to Data from BookManager Class

Our next step after we create the instances and store them in collection is to bind the data in Grid view from that very class, for this, we will return to our “MainPage.xaml” and create there a template how each individual item will appear on the screen. The template code is in the red box.

11

Add XAML namespace

In order to reference the class in XAML, we need to reference its namespace in XAML… here you go, shown by red arrow.

7

Create Public Property in MainPage.xaml.cs

After we create the template, our next step will be to create a public property in “mainpage.xaml.cs“ and populate it with Books.

  1. Add public property, as shown in circular region.
  2. Make sure you add a namespace if it is not there, as shown by the arrow.
  3. Initialize it with “Books=BookManager.GetBooks();”, this will return list of books.

9

Run your application.

After completion of all steps, run your application and the final result will be shown something like this:13

Working on Alignment

You can change the alignment and margin again, here is the simple step to do so.

14

Overview

In a nutshell, the alignment will make display much better.

15

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Junior)
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralNice One Pin
aarif moh shaikh25-Nov-15 19:28
professionalaarif moh shaikh25-Nov-15 19:28 
GeneralRe: Nice One Pin
Zain Nisar26-Nov-15 0:09
Zain Nisar26-Nov-15 0:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.