c# - Bindable field in ViewModel -
this question has answer here:
in viewmodels there typically lots of these
private string somefield; public string somefield { { return somefield; } set { somefield = value; notifyofpropertychanged(() => somefield); } }
is there way short version of such construct, bindable
?
so have write like:
public bindable<string> somefield;
perhaps action shall fired notifypropertychanged
...
i suppose create own class maintains value , raises inotifypropertychanged
against containing class create like:
public bindable<string> somefield = new bindable<string>("test", this);
and binding against somefield
access contained value , setting lead inotifypropertychanged
being raised against this
you'd need use implicit cast operators in order binding system see bindable<t>
source of t
, place put t
see: http://msdn.microsoft.com/en-us/library/85w54y0a.aspx
something along lines of 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 crude , better version no doubt created, should point in direction need go.
on further investigation of proposed solution i've found problematic work owing implicit cast t
bindable<t>
in order remember target , other details, i'm sure sort of solution contains enough ideas lead working one.
Comments
Post a Comment