FrameworkElement.DataContext FrameworkElement.DataContext FrameworkElement.DataContext FrameworkElement.DataContext Property


取得或設定項目在參與資料繫結時的資料內容。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.


下列範例說明如何將資料內容處理程式碼繫結,並提供定義的繫結屬性的特定值的資訊。The following example illustrates how a data context acts on a binding and provides the information that defines the specific values of bound properties.

  <src:LeagueList x:Key="MyList" />
<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="2751f-403"><xref:System.Windows.Data.Binding.ElementName%2A>.</span><span class="sxs-lookup"><span data-stu-id="2751f-403"><xref:System.Windows.Data.Binding.ElementName%2A>.</span></span>
- <span data-ttu-id="2751f-404"><xref:System.Windows.Data.Binding.Source%2A>.</span><span class="sxs-lookup"><span data-stu-id="2751f-404"><xref:System.Windows.Data.Binding.Source%2A>.</span></span>
- <span data-ttu-id="2751f-405"><xref:System.Windows.Data.Binding.RelativeSource%2A>.</span><span class="sxs-lookup"><span data-stu-id="2751f-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 是可繫結為 dbtype_iunknown 其中一個內容可能會繫結到另一個屬性。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

    <dataContextObject />  

XAML Attribute UsageXAML Attribute Usage

<object DataContext="bindingUsage"/>  
- or -  
<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 derived class. 或者,可能會在這裡,放適用於繫結的任何物件類型的未經處理資料實際稍後定義的繫結。Alternatively, raw data of any 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 或是 DynamicResource One 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