FrameworkElement.DataContext 属性

定义

获取或设置元素参与数据绑定时的数据上下文。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; }
member this.DataContext : obj with get, set
Public Property DataContext As Object

属性值

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

属性

示例

下面的示例说明了数据上下文如何对绑定进行操作,并提供了定义绑定属性的特定值的信息。The following example illustrates how a data context acts on a binding and provides the information that defines the specific values of bound properties.

<Window.Resources>
  <src:LeagueList x:Key="MyList" />
</Window.Resources>
<DockPanel DataContext="{Binding Source={StaticResource MyList}}">

注解

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

可以将数据上下文直接设置为 .NET 对象,并将绑定计算为该对象的属性。Data context can be set directly to a .NET 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 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.

或者,您可以使用 Binding 类的以下属性之一来显式指定绑定源:Alternatively, you can use one of the following properties of the Binding class to specify the binding source explicitly:

- <span data-ttu-id="2ed87-403"><xref:System.Windows.Data.Binding.ElementName%2A>。</span><span class="sxs-lookup"><span data-stu-id="2ed87-403"><xref:System.Windows.Data.Binding.ElementName%2A>.</span></span>
- <span data-ttu-id="2ed87-404"><xref:System.Windows.Data.Binding.Source%2A>。</span><span class="sxs-lookup"><span data-stu-id="2ed87-404"><xref:System.Windows.Data.Binding.Source%2A>.</span></span>
- <span data-ttu-id="2ed87-405"><xref:System.Windows.Data.Binding.RelativeSource%2A>。</span><span class="sxs-lookup"><span data-stu-id="2ed87-405"><xref:System.Windows.Data.Binding.RelativeSource%2A>.</span></span> 

有关详细信息,请参阅如何:指定绑定源For more information, see How to: Specify the Binding Source.

在 XAML 中,DataContext 通常设置为 Binding 声明。In XAML, DataContext is most typically set as a Binding declaration. 您可以使用属性元素语法或特性语法。You can use either property element syntax or attribute syntax. 此页上的示例显示了特性语法。Attribute syntax is shown in the example on this page. 你还可以使用代码设置 DataContextYou can also use code to set DataContext.

DataContext 是一种可绑定的属性,可用于在一个上下文可能绑定到另一个上下文的情况下实现。DataContext is a bindable property to facilitate scenarios where one context might be bound to another. 但是,如果绑定到 DataContext,请注意不要创建循环绑定引用(不将 DataContext 绑定到其自身,因为 DataContext 属性的属性值继承特性)。However, if you bind to DataContext, be careful to not create circular binding references (do not bind a DataContext to itself, which it is possible to do because of the property value inheritance nature of the DataContext property).

XAML 属性元素用法XAML Property Element Usage

<object>  
  <object.DataContext>  
    <dataContextObject />  
  </object.DataContext>  
</object>  

XAML 属性用法XAML Attribute Usage

<object DataContext="bindingUsage"/>  

-or-

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

XAML 值XAML Values

dataContextObjectdataContextObject
作为父元素中任何绑定的数据上下文的直接嵌入对象。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 derived class. 或者,可将用于绑定的任何对象类型的原始数据放置在此处,并在以后定义实际绑定。Alternatively, raw data of any object type intended for binding may be placed here, with the actual bindings defined later.

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

resourceExtensionresourceExtension
以下项之一: StaticResourceDynamicResourceOne 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.

contextResourceKeycontextResourceKey
要从 ResourceDictionary中请求的对象的密钥标识符。The key identifier for the object being requested from within a ResourceDictionary.

依赖项属性信息Dependency Property Information

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

适用于