Cet article a fait l’objet d’une traduction automatique. Pour afficher l’article en anglais, activez la case d’option Anglais. Vous pouvez également afficher le texte anglais dans une fenêtre contextuelle en faisant glisser le pointeur de la souris sur le texte traduit.
Traduction
Anglais

FrameworkElement.DataContext propriété

 

Date de publication : novembre 2016

Obtient ou définit le contexte de données d’un élément quand il participe à la liaison de données.

Espace de noms:   System.Windows
Assembly:  PresentationFramework (dans PresentationFramework.dll)

[LocalizabilityAttribute(LocalizationCategory.NeverLocalize)]
public object DataContext { get; set; }

Valeur de propriété

Type: System.Object

Objet à utiliser comme contexte de données.

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.

Data context can be set directly to a TLA#tla_clr object, with the bindings evaluating to properties of that object. Alternatively, you can set the data context to a T:System.Windows.Data.DataSourceProvider object.

This dependency property inherits property values. If there are child elements without other values for P:System.Windows.FrameworkElement.DataContext established through local values or styles, then the property system will set the value to be the P:System.Windows.FrameworkElement.DataContext value of the nearest parent element with this value assigned.

Alternatively, you can use one of the following properties of the T:System.Windows.Data.Binding class to specify the binding source explicitly: P:System.Windows.Data.Binding.ElementName, P:System.Windows.Data.Binding.Source, or P:System.Windows.Data.Binding.RelativeSource. For more information, see How to: Specify the Binding Source.

In TLA2#tla_xaml, P:System.Windows.FrameworkElement.DataContext is most typically set to as a T:System.Windows.Data.Binding declaration. You can use either property element syntax or attribute syntax. Attribute syntax is shown in the example on this page. You can also use code to set P:System.Windows.FrameworkElement.DataContext.

DataContext is a bindable property, to facilitate scenarios where one context might be bound to another. However, if you bind to DataContext, be careful to not create circular binding references (do not bind a DataContext to itself, which is possible to do because of the property value inheritance nature of the DataContext property).

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

<object DataContext="bindingUsage"/>
- or -
<object DataContext="{resourceExtension contextResourceKey}"/>

dataContextObject

A directly embedded object that serves as data context for any bindings within the parent element. Typically, this object is a T:System.Windows.Data.Binding or another T:System.Windows.Data.BindingBase derived class. Alternatively, raw data of any TLA2#tla_clr object type intended for binding may be placed here, with the actual bindings defined later.

bindingUsage

A binding usage that evaluates to an appropriate data context. For details, see Binding Markup Extension.

resourceExtension

One of the following: or . This usage is used when referring to raw data defined as an object in resources. See XAML Resources.

contextResourceKey

The key identifier for the object being requested from within a T:System.Windows.ResourceDictionary.

Identifier field

DataContextProperty

Metadata properties set to true

Inherits

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}}">

.NET Framework
Disponible depuis 3.0
Silverlight
Disponible depuis 2.0
Silverlight pour Windows Phone
Disponible depuis 7.0
Retour au début
Afficher: