FrameworkContentElement.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.

屬性

範例

下列範例會藉由建立新的自訂資料物件、將該物件建立為 DataContext,以及將系結路徑設定為其中的屬性,來設定 Paragraph 元素的系結。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.

XAMLXAML中,DataContext 最常設定為 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. 您也可以在程式碼中設定 DataContextYou can also set DataContext in code.

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 subclass. 或者,適用于系結之任何 CLRCLR 物件類型的原始資料可能會放在這裡,並在稍後定義實際的系結。Alternatively, raw data of any CLRCLR 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
下列其中一項: 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.

coNtextResourceKeycontextResourceKey
ResourceDictionary中所要求之物件的索引鍵識別碼。The key identifier for the object being requested from within a ResourceDictionary.

相依性屬性資訊Dependency Property Information

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

適用於