Edit this page

DataBinding - Overview

Data binding is a process of connecting the application User Interface and business logic.

DataContext

One very important aspect of the data binding is the DataContext. It is the data object, which is assigned to the specific framework element. You can assign DataContext both through code-behind and XAML:

XAML

<telerik:RadTreeView DataContext="{StaticResource Categories}" />

C#

radTreeView.DataContext = new CategoryList();

VB.NET

radTreeView.DataContext = New CategoryList()

The DataContext property is inherited, e.g. if you assign a DataContext to an element, all of its children will automatically have the same data context, unless you assign them a different data context. For example, if you have the following control structure:

XAML

<Grid x:Name="LayoutRoot">
    <telerik:RadTreeView x:Name="radTreeView1"/>
    <telerik:RadTreeView x:Name="radTreeView2"/>      
</Grid>

You can simply assign the DataContext to the Grid control and both tree views will inherit it.

XAML

<Grid x:Name="LayoutRoot"DataContext="{StaticResource categories}">

Where the static resource categories is defined in XAML as follows:

XAML

<UserControl.Resources>
  <local:CategoryList x:Key="categories" />
</UserControl.Resources>

Or in code-behind:

C#

LayoutRoot.DataContext = new CategoryList();

VB.NET

LayoutRoot.DataContext = New CategoryList()

See Also