Aplicativos do Windows
Recolher sumário
Expandir sumário
Este conteúdo não está disponível em seu idioma, mas aqui está a versão em inglês.

FrameworkElement.SetBinding method

Attaches a binding to a FrameworkElement, using the provided binding object.


public void SetBinding(
  DependencyProperty dp, 
  BindingBase binding



Type: DependencyProperty

The dependency property identifier of the property that is data bound.


Type: BindingBase

The binding to use for the property.


This method is a convenience method that calls BindingOperations.SetBinding, passing the current instance as the target parameter.

The type of the binding parameter is BindingBase for compatibility reasons, but in the Windows Runtime you always pass a Binding instance. Binding is derived from BindingBase.

The dp parameter takes a dependency property identifier. For more info on dependency properties and how a DependencyProperty value serves as the identifier, see Dependency properties overview.

You can bind to custom dependency properties or custom attached properties, the identifier you pass as the dp parameter doesn't have to be a Windows Runtime defined property.

Whether a binding created from code will be able to use an acting data context depends on object lifetime considerations. For example, a DataContext value that is set from XAML won't be available until the XAML is parsed. In that case you may want to use a Loaded handler to add bindings from code.

Note  Calling the SetBinding method and passing in a new Binding object won't necessarily remove an existing binding. Instead, you should first call the DependencyObject.ClearValue method, then call SetBinding.

Binding to attached properties

You can put data bindings on any attached properties that a target object supports. Technically an DependencyObject supports all the possible attached properties, but you'd usually only set a binding on an attached property that's relevant to that object or your scenario. For example you would set a binding on Grid.Row only if you anticipate that the target element has a Grid parent that will use that info. Specify the dp parameter as the dependency property identifier that exists on the attached property's owner class (for the Grid.Row example, that identifier is Grid.RowProperty). You won't find that identifier on the target because it's an attached property. For more info on attached properties, see Attached properties overview.


This example establishes a binding to a dependency property on an object by calling SetBinding.

// Create the source string.
string s = "Hello";

// Create the binding description.
Binding b = new Binding();
b.Mode = BindingMode.OneTime;
b.Source = s;

// Attach the binding to the target.
MyText.SetBinding(TextBlock.TextProperty, b);

Requirements (Windows 10 device family)

Device family

Universal, introduced version 10.0.10240.0

API contract

Windows.Foundation.UniversalApiContract, introduced version 1.0


Windows::UI::Xaml [C++]



Requirements (Windows 8.x and Windows Phone 8.x)

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]


Windows::UI::Xaml [C++]



See also

Data binding in depth
Dependency properties overview



© 2017 Microsoft