c# - WPF How to listen to a BindingBase object? -
i want create special datatrigger
inheriting triggerbase<frameworkelement>
. similar datatrigger
, property of type bindingbase
has been defined in mydatatrigger
class.
how can listen in order trace changes?
public class mydatatrigger : triggerbase<frameworkelement> { ... /// <summary> /// [wrapper property bindingproperty] /// <para> /// gets or sets binding produces property value of data object. /// </para> /// </summary> public bindingbase binding { { return (bindingbase)getvalue(bindingproperty); } set { setvalue(bindingproperty, value); } } public static readonly dependencyproperty bindingproperty = dependencyproperty.register("binding", typeof(bindingbase), typeof(mydatatrigger), new frameworkpropertymetadata(null)); }
update:
the main problem don't know how find bindingbase
associated dependencyproperty
. know how listen dp;
void listentodp(object component, dependencyproperty dp) { dependencypropertydescriptor dpdescriptor = dependencypropertydescriptor.fromproperty(dp, component.gettype()); dpdescriptor.addvaluechanged(component, dplistener_valuechanged); }
where dplistener_valuechanged
eventhandler
delegate. here, component parameter value this.associatedobject
.
okay, found!
considering answer, binding not dp. tried find binding associated dp:
type type = binding.gettype(); propertypath propertypath = (propertypath)(type.getproperty("path").getvalue(binding)); string propertyname = propertypath.path;
complete code:
public class mydatatrigger : triggerbase<frameworkelement> { ... public bindingbase binding { get; set; } protected override void onattached() { base.onattached(); if (binding != null && this.associatedobject.datacontext != null) // // adding property changed listener.. // type type = binding.gettype(); propertypath propertypath = (propertypath)(type.getproperty("path").getvalue(binding)); string propertyname = propertypath.path; typedescriptor.getproperties(this.associatedobject.datacontext).find(propertyname, false).addvaluechanged(this.associatedobject.datacontext, propertylistener_valuechanged); } private void propertylistener_valuechanged(object sender, eventargs e) { // stuff here.. } }
Comments
Post a Comment