There is a static class BindingOperations which has a method called SetBinding().
BindingOperations.SetBinding(targetObject, TargetObjectClass.AttachableProperty, new Binding());
Where:
targetObject – is the instance of the object we bind to
TargetObjectClass.AttachableProperty – is the static reference to the propety we want to bind from
new Binding() – is the Binding with the Path and Source properties appropriately set.
Sample usage is:
BindingOperations.SetBinding(observableResources,ObservableResources.CurrentCultureProperty, new Binding() { Path = new PropertyPath("CurrentCulture"), Source = LanguageManager, Mode = BindingMode.TwoWay});
You can also use:
targetObject.SetBinding(TargetObjectClass.AttachableProperty, new Binding());
To use these methods "TargetObjectClass" has to inherit from DependencyObject,
ReplyDeletepublic class TargetObjectClass : DependencyObject
{
}