Sdílet prostřednictvím


FrameworkElement.DataContext Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

'Declaration
Public Property DataContext As Object
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 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 XAML namespace mapping in 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 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. For an example of the WCF Data Services approach, see WCF Data Services (Silverlight).

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.

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} 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}" >
        <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
       //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 see this same example code with its supporting code for aspects such as the binding source, see How to: Bind to Hierarchical Data and Create a Master/Details View. To learn more about the role of DataContext in data binding scenarios, see Data Binding.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

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