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

定義

取得或設定項目在參與資料繫結時的資料內容。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="eb0de-403"><xref:System.Windows.Data.Binding.ElementName%2A>.</span><span class="sxs-lookup"><span data-stu-id="eb0de-403"><xref:System.Windows.Data.Binding.ElementName%2A>.</span></span>
- <span data-ttu-id="eb0de-404"><xref:System.Windows.Data.Binding.Source%2A>.</span><span class="sxs-lookup"><span data-stu-id="eb0de-404"><xref:System.Windows.Data.Binding.Source%2A>.</span></span>
- <span data-ttu-id="eb0de-405"><xref:System.Windows.Data.Binding.RelativeSource%2A>.</span><span class="sxs-lookup"><span data-stu-id="eb0de-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. 您也可以使用程式碼來DataContext設定。You 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
從內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

適用於