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; }
[<System.Windows.Localizability(System.Windows.LocalizationCategory.NeverLocalize)>]
member this.DataContext : obj with get, set
Public Property DataContext As Object

屬性值

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="79cb2-403"><xref:System.Windows.Data.Binding.ElementName%2A>.</span><span class="sxs-lookup"><span data-stu-id="79cb2-403"><xref:System.Windows.Data.Binding.ElementName%2A>.</span></span>
- <span data-ttu-id="79cb2-404"><xref:System.Windows.Data.Binding.Source%2A>.</span><span class="sxs-lookup"><span data-stu-id="79cb2-404"><xref:System.Windows.Data.Binding.Source%2A>.</span></span>
- <span data-ttu-id="79cb2-405"><xref:System.Windows.Data.Binding.RelativeSource%2A>.</span><span class="sxs-lookup"><span data-stu-id="79cb2-405"><xref:System.Windows.Data.Binding.RelativeSource%2A>.</span></span> 

如需詳細資訊,請參閱 如何:指定系結來源。For more information, see How to: Specify the Binding Source.

在 XAML 中, DataContext 最常設定為宣告 BindingIn 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 Attribute UsageXAML 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
從內要求之物件的索引鍵識別碼 ResourceDictionaryThe key identifier for the object being requested from within a ResourceDictionary.

相依性屬性資訊Dependency Property Information

識別碼欄位Identifier field DataContextProperty
中繼資料屬性設定為 trueMetadata properties set to true Inherits

適用於