Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Silverlight
Silverlight 2
 DataContext Property

  Switch on low bandwidth view
This page is specific to
Microsoft Silverlight 2

Other versions are also available for the following:
.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)
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
<object DataContext="binding"/>
- or -
<object 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.

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 also holds other characteristics of the binding, such as the path. For instance, 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, 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.

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

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker