Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
 DataContext Property
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
FrameworkElement..::.DataContext Property

Gets or sets the data context for an element when it participates in data binding. This is a dependency property.

Namespace:  System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
Visual Basic (Declaration)
<LocalizabilityAttribute(LocalizationCategory.NeverLocalize)> _
Public Property DataContext As Object
Visual Basic (Usage)
Dim instance As FrameworkElement
Dim value As Object

value = instance.DataContext

instance.DataContext = value
C#
[LocalizabilityAttribute(LocalizationCategory.NeverLocalize)]
public Object DataContext { get; set; }
Visual C++
[LocalizabilityAttribute(LocalizationCategory::NeverLocalize)]
public:
property Object^ DataContext {
    Object^ get ();
    void set (Object^ value);
}
JScript
public function get DataContext () : Object
public function set DataContext (value : Object)
XAML Property Element Usage
<object>
  <object.DataContext>
    <dataContextObject />
  </object.DataContext>
</object>
XAML Attribute Usage
<object DataContext="bindingUsage"/>
- or -
<object DataContext="{resourceExtension contextResourceKey}"/>

XAML Values

dataContextObject

A directly embedded object that serves as data context for any bindings within the parent element. Typically, this object is a Binding or another BindingBase derived class. Alternatively, raw data of any CLR object type intended for binding may be placed here, with the actual bindings defined later.

bindingUsage

A binding usage that evaluates to an appropriate data context. For details, see Binding Markup Extension.

resourceExtension

One of the following: StaticResource or DynamicResource. This usage is used when referring to raw data defined as an object in resources. See Resources Overview.

contextResourceKey

The key identifier for the object being requested from within a ResourceDictionary.

Property Value

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

Identifier field

DataContextProperty

Metadata properties set to true

Inherits

Data context is a concept that allows elements to inherit information from their parent elements about the data source that is used for binding, as well as other characteristics of the binding, such as the path.

Data context can be set directly to a common language runtime (CLR) object, with the bindings evaluating to properties of that object. Alternatively, you can set the data context to a DataSourceProvider object.

This dependency property inherits property values. If there are child elements without other values for DataContext established through local values or styles, then the property system will set the value to be the DataContext value of the nearest parent element with this value assigned.

Alternatively, you can use one of the following properties of the Binding class to specify the binding source explicitly: ElementName, Source, or RelativeSource. For more information, see How to: Specify the Binding Source.

In XAML, DataContext is most typically set to as a Binding declaration. You can use either property element syntax or attribute syntax. Attribute syntax is shown in the example on this page. You can also use code to set DataContext.

DataContext is a bindable property, to facilitate scenarios where one context might be bound to another. However, if you bind to DataContext, be careful to not create circular binding references (do not bind a DataContext to itself, which is possible to do because of the property value inheritance nature of the DataContext property).

The following example illustrates how a data context acts on a binding and provides the information that defines the specific values of bound properties.

XAML
<Window.Resources>
  <src:LeagueList x:Key="MyList" />


...


</Window.Resources>


...


<DockPanel DataContext="{Binding Source={StaticResource MyList}}">

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0
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 | Site Feedback
Page view tracker