Windows Phone : Data Binding

By | July 8, 2013

Data Binding is the most important part of developing a good quality dynamic application. You can set data of TextBox or someother control by creating different pages and you need to write a lot of code for that and hence data binding helps you to easily bind some data to your controls in creating complex applications.

Text="{Binding PostName}" />

Here we have a TextBlock for a post name, we can have the value of TextBlock each time but it will make the code more unhealthy and the practice is not recommended and hence you can notice a Binding statement which is pointing at something called PostName, PostName here is the property of some data source and next we have to define the data source by setting the DataContext either on the page or on a specific element (preferably on page). These act as objects which says that against any binding statements I will resolve the data associated with it. So the PostName here is the instance of the data source and once we have defined the data source it automatically pulls the post name and assigns it to the PostNameTextBox.

Data Binding Modes

There are different binding modes which can be used according to the requirement of the application :

OneTime :  It means when the page is rendered any binding statements are resolved against the DataContext, this is done one a single time i.e. if after resolving the data there is a change in data source it is not reflected back on the application page.

OneWay : It gets the data from the data source and there may be a web service or background code running which updates the data source so the UI control gets updates, so it is the synchronized method of data binding.

TwoWay : It is the next step from OneWay, it does everything the OneWay does and can also update the data source for example if a user has TextBox where a user can enter a data or radio button where a user can select a data, then the change goes the other way as well i.e. the data in the data source also gets updated. And the best part is that user don’t have to write any code to update the data source it does it automatically.

Text="{Binding PostName, Mode=TwoWay}"  />

So now with data binding we have much more cleaner code much more maintained and a healthy programming practice.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.