FrameworkElement.DataContext Property

July 28, 2014

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)
XMLNS for XAML: Not mapped to an xmlns.

public Object DataContext { get; set; }
<frameworkElement DataContext="binding"/>
- or -
<frameworkElement DataContext="{StaticResource keyedObject}"/>

XAML Values

binding

A binding expression that can reference an existing data context, or a property in the data context. See Data binding for Windows Phone 8.

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 XAML namespace mapping in the ResourceDictionary.

Property Value

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

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 most frequent use of DataContext is to set it directly to a CLR data source object. The CLR data source might be a class if it is a static starting data set. Or you can create a local data source as a new ObservableCollection<T>, and then update that collection through WCF Data Services or other techniques..

The data context can also hold other characteristics of the binding other than the data source, such as a path into the data source. For example, you could establish the following object tree in 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, a DataContext defined by StackPanel inherits to the Rectangle child object, and becomes the data context for the otherwise unqualified {Binding} statement in the Fill property.

DataContext is a convenience for aligning bindings to shared data contexts, which is often quite useful for avoiding verbose binding statements. However, it is valid for a DataContext to be undefined, and for all the necessary binding qualifications to exist in separate binding statements.

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 in order to facilitate scenarios where one context might be bound to another, although that scenario is infrequent.

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} path expressions in the ListBox items inherit and can use the DataContext for the binding Source, and can use the ListBox item population behavior to bind to the data items from that collection source.


<StackPanel>
    <ListBox x:Name="MyBooks" Margin="5" 
             ItemsSource="{Binding Mode=OneWay}" 
             FontSize="{StaticResource PhoneFontSizeMedium}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" >
                    <TextBlock Text="{Binding ISBN}" Margin="0,0,50,0" />
                    <TextBlock Text="{Binding Title}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>



//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;


This is only a brief example. To learn more about the role of DataContext in data binding scenarios, see Data binding for Windows Phone 8.

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft