Binding properties in code behind

Here’s how you define and apply a binding in code:

Binding binding = new Binding {
  Source = TitleLabel,
  Path = new PropertyPath("Content"),
};
BottomLabel.SetBinding(ContentControl.ContentProperty, binding);

Note that on objects that don’t derive from FrameworkElement, you have to explicitly use BindingOperations.SetBinding() instead of element.SetBinding():

BindingOperations.SetBinding(BottomLabel, ContentControl.ContentProperty, binding);

Leave a Comment