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 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 same but it works in single column fashion.
Create a Project in Visual Studio 2015
In order to perform data binding , you need to create a project in Visual Studio 2015, steps are simple:
- Select “Universal” as shown by arrow.
- Select “Blank App” as shown in circle.
- Name your solution, for instance “BindDataExample“.
A designer window will 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 following image in rectangle sharpe.
Create Data Model
- Go to “Solution Explorer” shown by circle.
- Right click the “BindDataExample” shown by arrow.
- Add new folder, name it “Models” shown by circle.
Create C# class in Models Folder
- Go to Models folder.
- Right click and select add new item.
- Select Class and name it “Book.cs“.
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.
- Create a class “Book“.
- Define its properties.
- Copy the C# code in red rectangular box.
Creating New Instances & Add in Collection
After you created your Book class and set its properties, now its time to create instances of your class and populate it into a collection, list of book. We will generate another class , name it “BookManager” in the same file, for this purpose. And then return the list to caller. The following code will help you out.
Add GridView, Bind to data from BookManager Class
Our next step is after we created the instances, storing them in collection is to bind the data in GridView from that very class. For this we will return to our “MainPage.xaml” and create a template that how each individual item will appear on the screen. The template code is in red box .
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.
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.
- Add public property as shown in circular region.
- Make sure you add a namespace if it is not there as shown by arrow.
- Initialize it with “Books=BookManager.GetBooks();.”, this will return list of books.
Run your Application.
After completion of all the steps, run you application and the final result will be shown something like the following,
Working on Alignment
You can change the alignment and margin again. Here's a simple step to do,
In nutshell, the alignment will make display much better.