SetBinding Method
Collapse the table of content
Expand the table of content

FrameworkElement.SetBinding Method

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Attaches a binding to a FrameworkElement, using the provided binding object, and returns a BindingExpressionBase for possible later use.

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)

public BindingExpressionBase SetBinding(
	DependencyProperty dp,
	Binding binding


Type: System.Windows.DependencyProperty
The dependency property identifier of the property that is data bound.
Type: System.Windows.Data.Binding
The binding to use for the property.


binding is specified as TwoWay, but has an empty Path.


dp or binding parameters are null.

The returned binding expression is not necessary for many of the common binding scenarios, but is potentially useful for scenarios such as updating the source manually, or getting references to the parent Binding after the expression is created. The BindingExpressionBase class is a base class. In most cases you should attempt to cast the return value to BindingExpression, which is the class that implements the relevant API for the mentioned update or Binding parent scenarios.

For target bindings on dependency properties that are not a property of a FrameworkElement and therefore do not support SetBinding, you can use methods of BindingOperations instead.


Calling this method and passing in a new Binding object will not necessarily remove an existing binding. Instead, you should use the DependencyObject.ClearValue method.

The following 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);

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

© 2018 Microsoft