.NET Framework Class Library for Silverlight
FrameworkElement..::.DataContext Property

Gets or sets the data context for a FrameworkElement when it participates in data binding.

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)
Syntax

Visual Basic (Declaration)
Public Property DataContext As Object
    Get
    Set
Visual Basic (Usage)
Dim instance As FrameworkElement
Dim value As Object

value = instance.DataContext

instance.DataContext = value
C#
public Object DataContext { get; set; }
XAML Attribute Usage
<frameworkElement DataContext="binding"/>
- or -
<frameworkElement DataContext="{StaticResource keyedObject}"/>

XAML Values

binding

A binding expression that can reference an existing data context, or a property within the data context. See Data Binding or Binding Markup Extension.

keyedObject

The x:Key value of an object that exists in an in-scope Resources collection. Typically, this is an object element instantiation of a custom type defined elsewhere in your code, and requires a custom XML namespace mapping within the ResourceDictionary.

Property Value

Type: System..::.Object
The object to use as data context.
Remarks

Dependency property identifier field: DataContextProperty

Data context is a concept that allows objects to inherit binding-specifying information from their parents in the object tree. The most important aspect of data context is the data source that is used for binding. The data context can also hold other characteristics of the binding, such as the path. For instance, you could establish the following object tree in XAML.

XAML
<StackPanel>
  <StackPanel.Resources>
    <SolidColorBrush Color="Orange" x:Key="MyBrush"/>
  </StackPanel.Resources>
  <StackPanel DataContext="{StaticResource MyBrush}">
    <Rectangle Height="50" Width="50" Fill="{Binding}" />
  </StackPanel>
</StackPanel>

In this case, the DataContext defined by the inner StackPanel inherits to the Rectangle child object, and becomes the data context for the otherwise unqualified binding statement made by the binding in the Fill property.

In code, data context can be set directly to a CLR object, with the bindings evaluating to properties of that object.

You can also set the DataContext to a custom object that is instantiated as a XAML object element in a ResourceDictionary, referencing it by using StaticResource to retrieve the resource by its x:Key value.

DataContext is a bindable property, to facilitate scenarios where one context might be bound to another.

Examples

The following example shows the UI context for a ListBox named MyBooks and its items. Code-behind that executes on load then sets DataContext on the MyBooks ListBox. The {Binding ISBN} and {Binding Title} pathing expressions in the ListBox items can inherit and use the DataContext for the binding Source, and use the ListBox item population behavior to bind to the data items from that collection source.

XAML
<StackPanel>

    <ListBox x:Name="MyBooks" Margin="5" ItemsSource="{Binding Mode=OneWay}" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" >
                    <TextBlock Text="{Binding ISBN}" Margin="0,0,50,0" />
            <TextBlock Text="{Binding Title}" />
            </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
Visual Basic
' You can add items to your collection.
AllBooks.Add(New Book("4458907683", "Training Your Dog", _
    New DateTime(2000, 2, 8), 44.25))
AllBooks.Add(New Book("0446675385", "Good Owners, Great Dogs", _
    New DateTime(1999, 9, 1), 15.99))

'Set the data context for the list of books
MyBooks.DataContext = AllBooks
C#
       //You can add items to your collection
       AllBooks.Add(new Book("4458907683", "Training Your Dog",
           new DateTime(2000, 2, 8), 44.25));
       AllBooks.Add(new Book("0446675385", "Good Owners, Great Dogs",
           new DateTime(1999, 9, 1), 15.99));
       //Set the data context for the list of books
       MyBooks.DataContext = AllBooks;

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference

Other Resources

Tags : databinding


Page view tracker