Data Binding

DataContext

The purpose of the DataContext is to hold the data that will be bound (like a data source). If the DataContext is empty no binding will be applied. The DataContext can be set from both XAML and managed code:

<telerik:RadTabControl x:Name="radTabControl" DataContext="{Binding MyTabItems}" /> 

RadTabControl radTabControl= new RadTabControl(); 
radTabControl.DataContext = new Collection<WordDocument>(); 
Dim radTabControl As New RadTabControl() 
radTabControl.DataContext = New Collection(Of WordDocument)() 

The DataContext is inherited in the visual tree, which means that the child controls of the RadTabControl will have the same DataContext, if it is not explicitly changed.

The PropagateItemDataContextToContent property of the RadTabControl specifies whether the DataContext of the RadTabItem should be assigned as the DataContext of its content when the selection changes. The default value of this property is True.

Item Source

The ItemsSource property allows the RadTabControl to be bound to any collection that implements the IEnumerable interface. For each item in the collection, a container of type RadTabItem is created. By using the ItemTemplate, ItemContainerStyle and TemplateSelectors you can control the appearance of the dynamically created tabs. Read more about that here.

When binding the RadTabControl to a collection, the dynamically created RadTabItems have the item they are bound to as DataContext.

Types of Sources

The ItemsSource property can be bound to any collection that implements the IEnumerable interface. If you want insertions or deletions in the collection to be automatically applied to the UI, the collection we bound to must also implement the ICollectionChanged interface. This interface exposes an event that should be raised whenever the underlying collection changes. In Silverlight there is a built-in implementation of a data collection that exposes the INotifyCollectionChanged interface – the ObservableCollection class.

Consider using ObservableCollection or one of the other existing collection classes like List, Collection, instead of implementing your own collection. If the scenario requires a custom collection to be implemented, use the IList interface, which provides individual access by index to its items and the best performance.

DisplayMemberPath

When no ItemTemplate is defined the DisplayMemberPath is used to determine which property of the DataContext object to be visualized in the tab content.

<telerik:RadTabControl x:Name="radTabControl" ItemsSource="{Binding Persons}" DisplayMemberPath="Name" /> 

RadTabControl radTabControl= new RadTabControl(); 
radTabControl.ItemsSource = new Collection<Person>(); 
radTabControl.DisplayMemberPath = "Name"; 
Dim radTabControl As New RadTabControl() 
radTabControl.ItemsSource = New Collection(Of Person)() 
radTabControl.DisplayMemberPath = "Name" 

If neither the DisplayMemberPath nor the ItemTemplate are set, then the content of the tab would be set to the value returned by the ToString() method.

See Also

In this article