Friday, July 15, 2011

Data Context Property in WPF

Data context property holds data. Data context property allows inheriting data to its child elements. This is because dependency property allows property value inheritance.  So if parent element defines data context property then its child element will get access to the data assigned in data context property. This property generally used in WPF data binding. While binding child element we don’t need to specify source property if we already defined data context property to its parent element we just need to specify path property with binding. Data context property is available at FrameworkElement class. You can read more about WPF class hierarchy here. 


<Window.Resources>
    <local:StudentInfo FirstName="Mitesh"
                       LastName="Sureja"
                       Age="29"
                       x:Key="MyStudentInfo"/>
</Window.Resources>
<Grid>
      ---
      ---
    <TextBlock Text="Student Name: " />
    <TextBox Name="StudentName"
            Text="{Binding Source={StaticResource MyStudentInfo},                           Path=FullName}" />
    <TextBlock Text="Student Age: " />
    <TextBox Name="StudentAge"
             Text="{Binding Source={StaticResource MyStudentInfo},                            Path=Age}" />
</Grid>


In this example Textbox's Text property is bind with StudentInfo class's FullName and Age property respectively. In each binding, binding source and path are mentioned. But if data context property of parent object is set with source then don't need to specify source in each binding. Lets have a look on below XAML snippet.

<Window.Resources>
    <local:StudentInfo FirstName="Mitesh"
                       LastName="Sureja"
                       Age="29"
                       x:Key="MyStudentInfo"/>
</Window.Resources>
<Grid DataContext="{StaticResource MyStudentInfo}">

---

    <TextBlock Text="Student Name: " />
    <TextBox Name="StudentName"
            Text="{Binding Path=FullName}" />
    <TextBlock Text="Student Age: " />
    <TextBox Name="StudentAge"
             Text="{Binding Path=Age}" />
</Grid>


In above XAML, data context property is set to grid so all element defined inside grid will have access to data assigned in data context property of grid. So while binding textbox's text property, we don't need to specify binding source just need to specify path property. If source property of binding is not specified then binding will look for data context property of parent element and consider it as source.


Related posts- 

2 comments:

  1. Where did the "FullName" property come from? MyStudentInfo does not contain this property.

    ReplyDelete
  2. Sorry for delayed answer,

    I have used StudentInfo Class which exposes FirstName, LastName, FullName and Age Property.

    You can refer to below article which demonstrates StudentInfo Class information.
    http://miteshsureja.blogspot.in/2011/07/inotifypropertychanged-interface-in-wpf.html

    ReplyDelete