Featured post
c# - passing Event Arguments of original handler to Routed Event in wpf -
the following code shows normal event , routed event. here have used same event name explaining purposes in reality using routed event.
//normal event public event selectedhandler selected; public delegate void selectedhandler(object sender, routedeventargs e); //routed event public static readonly routedevent selectedevent = eventmanager.registerroutedevent( "selected", routingstrategy.bubble, typeof(routedeventhandler), typeof(myusercontrol)); //add remove handlers public event routedeventhandler selected { add { addhandler(selectedevent, value); } remove { removehandler(selectedevent, value); } }
i raising these events couple of event handlers follows
private void lstvmyview_selectionchanged(object sender, selectionchangedeventargs e) { //normal event raise if (selected != null) selected(this, e); //routed event raise routedeventargs args = new routedeventargs(selectedevent); raiseevent(args); } private void lstvmyview_mouseleftbuttonup(object sender, mousebuttoneventargs e) { //normal event raise if (selected != null) selected(this, e); //routed event raise routedeventargs args = new routedeventargs(selectedevent); raiseevent(args); }
when handling normal event able send args of both handlers event in routed event args new instance. want pass args of both handlers routed event. possible achieve this? if yes how?
first of not need (and should remove it):
//normal event public event selectedhandler selected; public delegate void selectedhandler(object sender, routedeventargs e);
i.e. not need define separate "normal" event, because have done declaration:
public event routedeventhandler selected { add { addhandler(selectedevent, value); } remove { removehandler(selectedevent, value); } }
with above code block "wrapping" routed event "normal" (clr) 1 users of class can use "normal" syntax (i.e. instanceofmyusercontrol.selected += ....
)
second, if want event arguments of routed event same ones of selectionchanged
event of the listview
listening to, should declare routed event way:
public static readonly routedevent selectedevent = eventmanager.registerroutedevent( "selected", routingstrategy.bubble, typeof(selectionchangedeventhandler), typeof(myusercontrol)); //add remove handlers public event selectionchangedeventhandler selected { add { addhandler(selectedevent, value); } remove { removehandler(selectedevent, value); } }
notice have substituted routedeventhandler
selectionchangedeventhandler
, predefined 1 can "carry" selectionchangedeventargs
.
now rising of event. not need rise both "normal" , routed 1 (as "normal" wrapper routed), should delete this:
//normal event raise if (selected != null) selected(this, e);
and rise routed version, can done way:
selectionchangedeventargs args = new selectionchangedeventargs(selectedevent, e.removeditems, e.addeditems); raiseevent(args);
notice using event arguments original event set addeditems
, removeditems
of 1 custom one.
- Get link
- X
- Other Apps
Comments
Post a Comment