Unfortunately the data binding mechanism is hooked quite deeply into controls, so you will have to implement something yourself. Fortunately, this is quite easy to do in the simple case (where you are binding to a simple property and the source supports INotifyPropertyChanged as the notification mechanism):
public class DataBinding : IDisposable {
public INotifyPropertyChanged Source { get; private set; }
public string SourceProperty { get; private set; }
public object Target { get; private set; }
public string TargetProperty { get; private set; }
private PropertyInfo sourcePropertyInfo, targetPropertyInfo;
public DataBinding(INotifyPropertyChanged source, string sourceProperty, object target, string targetProperty){
Source = source; SourceProperty = sourceProperty;
Target = target; TargetProperty = targetProperty;
sourcePropertyInfo = source.GetType().GetProperty(sourceProperty);
targetPropertyInfo = target.GetType().GetProperty(targetProperty);
}
public void Attach(){
Detach();
source.PropertyChanged += SourceEventHandler;
Refresh();
}
public void Detach() {
source.PropertyChanged -= SourceEventHandler;
}
public void Dispose(){ Detach(); }
private void SourceEventHandler(object sender, PropertyChangedEventArgs args){
if(args.PropertyName == SourceProperty || args.PropertyName == null)
Refresh();
}
public void Refresh(){
targetPropertyInfo.SetValue(target, sourcePropertyInfo.GetValue(source));
}
}
You can then use this in a way analogous to
var myBinding = new DataBinding(myClass, "MyValue", myComboBox, "SelectedValue");
myBinding.Attach();
... to bind anything to anything else.
You could also put the attachment code in the constructor, but that seems unnatural to me.
(Disclaimer: this is just typed into the posting box, so there might be minor mistakes.)