FrameworkContentElement.DataContext 属性


获取或设置元素参与数据绑定时的数据上下文。Gets or sets the data context for an element when it participates in data binding.

 property System::Object ^ DataContext { System::Object ^ get(); void set(System::Object ^ value); };
public object DataContext { get; set; }
member this.DataContext : obj with get, set
Public Property DataContext As Object



要用作数据上下文的对象。The object to use as data context.



下面的示例 Paragraph 通过创建一个新的自定义数据对象,将该对象建立为 DataContext ,并将绑定路径设置为其中的属性,来设置对元素的绑定。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")


数据上下文 是一种概念,允许元素从父元素继承有关用于绑定的绑定源的信息,以及绑定的其他特征(如路径)。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.

可以将数据上下文直接设置为 公共语言运行时 (CLR)common language runtime (CLR) 对象,并将绑定计算为该对象的属性。Data context can be set directly to a 公共语言运行时 (CLR)common language runtime (CLR) object, with the bindings evaluating to properties of that object. 或者,您可以将数据上下文设置为 DataSourceProvider 对象。Alternatively, you can set the data context to a DataSourceProvider object.

此依赖属性继承属性值。This dependency property inherits property values. 如果有子元素没有 DataContext 通过本地值或样式建立的其他值,则属性系统会将该值设置为 DataContext 已分配此值的最近父元素的值。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.

或者,您可以使用类的下列属性之一 Binding 显式指定绑定源: ElementNameSourceRelativeSourceAlternatively, 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.

在中 XAMLXAMLDataContext 最常将设置为作为 Binding 声明。In XAMLXAML, 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. 你还可以 DataContext 在代码中设置。You can also set DataContext in code.

XAML 属性元素用法XAML Property Element Usage

    <dataContextObject />  

XAML 属性用法XAML Attribute Usage

<object DataContext="bindingUsage"/>  


<object DataContext="{resourceExtension contextResourceKey}"/>  


作为父元素中任何绑定的数据上下文的直接嵌入对象。A directly embedded object that serves as data context for any bindings within the parent element. 通常,此对象是 Binding 或其他 BindingBase 子类。Typically, this object is a Binding or another BindingBase subclass. 或者, CLRCLR 可将用于绑定的任何对象类型的原始数据放置在此处,并在以后定义实际绑定。Alternatively, raw data of any CLRCLR object type intended for binding may be placed here, with the actual bindings defined later.

计算结果为适当数据上下文的绑定使用情况。A binding usage that evaluates to an appropriate data context. 有关详细信息,请参阅 Binding 标记扩展For details, see Binding Markup Extension.

以下项之一: StaticResource 、或 DynamicResourceOne of the following: StaticResource, or DynamicResource. 当引用定义为资源中的对象的原始数据时,将使用此用法。This usage is used when referring to raw data defined as an object in resources. 请参阅 XAML 资源See XAML Resources.

要从中请求的对象的键标识符 ResourceDictionaryThe key identifier for the object being requested from within a ResourceDictionary.

依赖项属性信息Dependency Property Information

标识符字段Identifier field DataContextProperty
元数据属性设置为 trueMetadata properties set to true Inherits