Επεξεργασία

Κοινή χρήση μέσω


FrameworkContentElement.DataContext Property

Definition

Gets or sets the data context for an element when it participates in data binding.

public:
 property System::Object ^ DataContext { System::Object ^ get(); void set(System::Object ^ value); };
[System.Windows.Localizability(System.Windows.LocalizationCategory.NeverLocalize)]
public object DataContext { get; set; }
[<System.Windows.Localizability(System.Windows.LocalizationCategory.NeverLocalize)>]
member this.DataContext : obj with get, set
Public Property DataContext As Object

Property Value

The object to use as data context.

Attributes

Examples

The following example sets a binding on a Paragraph element, by creating a new custom data object, establishing that object as DataContext, and setting the binding path to a property within it.

MyData myDataObject = new MyData();
myflowdocument.DataContext = myDataObject;
introParagraph.SetBinding(Paragraph.TagProperty, "CustomData");
Dim myDataObject As New MyData()
myflowdocument.DataContext = myDataObject
introParagraph.SetBinding(Paragraph.TagProperty, "CustomData")

Remarks

Data context is a concept that allows elements to inherit information from their parent elements about the binding 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 with no other value for DataContext established through local values or styles, 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 commonly 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 set DataContext in code.

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 subclass. 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 XAML Resources.

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

Dependency Property Information

Identifier field DataContextProperty
Metadata properties set to true Inherits

Applies to