Using Binding in C#

The following translates the use of the Binding markup extension to C#, assuming the relevant Run is given the name run:

Binding binding = new Binding();// Set source objectbinding.Source = this.textBox;// Set source propertybinding.Path = new PropertyPath("Text");// Attach to target propertythis.run.SetBinding(Run.TextProperty, binding);

In C#, you can’t set run’s Text property to a Binding instance, because Text must be a string. The magic that is abstracted away by the markup extension in XAML is simply a call to run’s SetBinding method (inherited from FrameworkElement) that identifies which property to associate with the binding using the relevant static DependencyProperty property. This makes ...

Get XAML Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.