I suppose you could create your own class that maintains a value and raises INotifyPropertyChanged
against the containing class that you could create like:
public Bindable<string> SomeField = new Bindable<string>("test", this);
And then Binding against SomeField
would access the contained value and setting it would lead to INotifyPropertyChanged
being raised against this
You'd need to use some implicit cast operators in order to get the binding system to see your Bindable<T>
as a source of T
and a place to put T
See: http://msdn.microsoft.com/en-us/library/85w54y0a.aspx
Something along the lines of the following may suffice:
public class Bindable<T>
{
private T _value;
private PropertyChangedEventHandler _notifyHandler;
private INotifyPropertyChanged _notifyTarget;
private string _name;
public Bindable(PropertyChangedEventHandler notifyHandler, INotifyPropertyChanged notifyTarget, string name, T value = default(T), bool trigger = false)
{
_value = value;
_name = name;
_notifyHandler = notifyHandler;
_notifyTarget = notifyTarget;
if (trigger)
{
_notifyHandler(_notifyTarget, new PropertyChangedEventArgs(_name));
}
}
public implicit operator T(Bindable<T> bindable)
{
return bindable._value;
}
public implicit operator Bindable<T>(T value)
{
return new Bindable<T>(_notifyHandler, _notifyTarget, _name, value, true);
}
}
The above code is crude and a better version could no doubt be created, but it should point you in the direction you need to go.
On further investigation of my proposed solution I've found that it would be problematic to get to work owing to the implicit cast from T
to Bindable<T>
in order to remember the target and other details, I'm sure this sort of solution contains enough ideas to lead to a working one.