Social Icons

Wednesday, 14 August 2013

DataContext in WPF with Example

Introduction:

     In this article i will explain 
  • What is DataContext in WPF? and How to Bind Objects to DataContext.
  •  How DataContext helpful to WPF binding Techniques.

Description:

DataContext:

          DataContext is the Dependency Property which holds the Data. Here Data can be any type, it can be an CLR Object, a DataSet, XMLDataProvider, Any Collection Object etc. Almost all controls in wpf user interfaces are derived from Dependency Object so either Panel controls or Content Controls can have this Property.

             This Dependency Property has "Value Inheritance " feature means if you set Data Context as Data Object on Parent element then all of its descendent elements or children elements can reference to Data assigned to parent DataContext. i.e  All the child elements are bind against to the Data Object of parent control DataContext unless if we specify to bind against to other Objects.

            If DataContext is set on parent elements then no need to set binding source to children elements , we need to specify the path only for children elements. Let us see with example.

I have class called MobileShop with three properties.



Now i will show these three properties in three textboxes by setting MobileShop Object as DataContext for Window

Here is the XAML Code How the Window Looks like and How Child Elements access the DataContext of the Parent Control.



If you see the above xaml code for accessing the local class file in XAML code first i created a namespace reference to local as below

xmlns:local="clr-namespace:WpfApplication1"
Now we can able to access project resources using local reference in XAML code like below

In the above lines i created an instance of MobileShop Class added data to properties and assigned that object as DataContext for Window Element. So for all the child elements we have to set the path only no need to set source again. 

Source Vs DataContext:

       There is a situation where parent element is set to DataContext object and Child Element bind to Source of another Object then Child Element binds to Source Object Only. Here child element checks first if any Source or RelativeSource bind to any object, if set  it will bind to that object, if Source or RelativeSource is not set then WPF Searches up in the element tree for DataContext of the Parent elements if it finds any non null Object then that objects used for Binding. This type of Binding use full when multiple properties are bind to Single Source.

Here is one example using same MobileShop class , where we will create two objects for this class and one object will bind to parent element as earlier, another bind to child element directly as source.


Conclusion:

        In this way DataContext Property is useful when setting multiple properties to Single Source.

                        If you are binding single property then it is better to set as source for the element because of easy debugging i.e all the binding information exist in one place rather than searching up in the element tree for nearest DataContext. This DataContext defined in FrameworkElement Class.

      Don't forget to Comment and Share to our friends if you like this post. Thank You. 
 

No comments:

Post a comment

Please Give your Valuable Suggestions,Questions and Comments to improve my blog.