BindingOperations.SetBinding method

Applies to Windows and Windows Phone

Associates a Binding with a target property on a target object. This method is the code equivalent to using a Binding markup extension in XAML markup.

Syntax


public static void SetBinding(
  DependencyObject target, 
  DependencyProperty dp, 
  BindingBase binding
)

Parameters

target

Type: DependencyObject

The object that should be the target of the evaluated binding.

dp

Type: DependencyProperty

The property on the target to bind, specified by its identifier. These identifiers are usually available as static read-only properties on the type that defines the target object, or one of its base types. You can also bind to attached properties, but see Remarks.

binding

Type: BindingBase

The binding to assign to the target property. This Binding should be initialized: important Binding properties such as Path should already be set before passing it as the parameter.

Remarks

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.

BindingOperations.SetBinding is a static utility method, and does basically the same thing as FrameworkElement.SetBinding. It's more common to use FrameworkElement.SetBinding because it's an instance method. One important difference though is that BindingOperations.SetBinding can use a target value of any DependencyObject, whereas FrameworkElement.SetBinding can by definition only be used for a FrameworkElement target. This doesn't usually matter for most Windows Runtime classes used for XAML UI, because these are mostly FrameworkElement subclasses anyways. But the distinction might matter if you are targeting bindings on your own custom classes that derive from DependencyObject or UIElement.

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.

Requirements

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]

Namespace

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

Metadata

Windows.winmd

See also

BindingOperations
Binding
XAML data binding sample
Data binding overview

 

 

Show:
© 2014 Microsoft