c# - Change backcolor of textboxes when their bound properties change? -
what best way of accomplishing this?
some sample code:
adding binding text boxes, , adding event listener:
this.myobject = new myobject(); this.mytextbox.databindings.add( new binding("text", this.myobject, "myproperty", true, datasourceupdatemode.onpropertychanged)); this.myothertextbox.databindings.add( new binding("text", this.myobject, "myotherproperty", true, datasourceupdatemode.onpropertychanged)); this.myobject.propertychanged += handler;
the propertychangedeventhandler handler
passed in parent user control, new propertychangedeventhandler(handlepropertychange)
. because parent contains save button enabled/disabled, , tab control has multiple pages containing multiple user controls. want handle property changes children, seemed best way of structuring things (??)
this object/property bind (based on https://stackoverflow.com/a/1316566/1061602)
public class myobject : inotifypropertychanged { private int myproperty; public int myproperty { { return myproperty; } set { setfield(ref myproperty, value, () => myproperty); } } private int myotherproperty; public int myotherproperty { { return myotherproperty; } set { setfield(ref myotherproperty, value, () => myotherproperty); } } public event propertychangedeventhandler propertychanged; protected virtual void onpropertychanged<t>(expression<func<t>> selectorexpression) { if (selectorexpression == null) throw new argumentnullexception("selectorexpression"); var body = selectorexpression.body memberexpression; if (body == null) throw new argumentexception("the body must member expression"); onpropertychanged(body.member.name); } protected virtual void onpropertychanged(string propertyname) { propertychangedeventhandler handler = propertychanged; if (handler != null) handler(this, new propertychangedeventargs(propertyname)); } protected bool setfield<t>(ref t field, t value, expression<func<t>> selectorexpression) { if (equalitycomparer<t>.default.equals(field, value)) return false; field = value; onpropertychanged(selectorexpression); return true; } }
i know there parameter called formattingenabled
on binding
object, looks date/time/currency formatting.
i can't see way of getting textbox propertychangedevent, referring property itself.
i saw this: binding bool property backcolor property of winform - i'm not sure if adding properties best approach?
update
based on bond's answer, this:
this.myobject.propertychanged += (s, e) => handler(this.controls, e);
so passing in entire controlcollection on user control event hander.
and have event handler this:
public void configchanged(object sender, eventargs e) { if (sender controlcollection) { var allcontrols = (controlcollection)sender; foreach (var control in allcontrols) { if (control textbox) { var textbox = (textbox)control; if (textbox.databindings[0].bindingmemberinfo.bindingfield == (e propertychangedeventargs).propertyname) { textbox.backcolor = color.lightgoldenrodyellow; } } } } this.savebutton.enabled = true; }
but seems unwieldy, , if form changed (e.g. putting textboxes groupbox, or wanting same other control types) no longer work.
i guess on user control side, know controls want bind to, create collection of , pass in sender
object, still not particularly nice i'd have maintain list separately...
any thoughts?
you can this
this.myobject.propertychanged += (s, e) => handler(mytextbox, e);
Comments
Post a Comment