FrameworkElement.SetBinding Method

May 11, 2014

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
)

Parameters

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

ExceptionCondition
ArgumentException

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

-or-

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.

NoteNote:

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

Show:
© 2014 Microsoft