c# - Keep Mouse Events bubbling from WindowsFormsHost on -
i have winforms control within wpf windowsformshost. winforms control passive , must not handle mouse event. mouse events should raised usual inner wpf control in wpf visual tree, windowsformshost (or next one). no event triggered @ all.
how should configure windowsformshost, winforms control, in order achieve this?
remark: keydown , keyup behave expected. mouse events don't, illustrated following snoop screenshot:
indeed winforms control keeps mouse event himself , doesn't forward event host. solution subscribe winforms mousedown event , generate programmatically routed event.
i overrided windowsformshost following , rocks:
(remark: behavior may more flexible)
public class extendedwindowsformshost : windowsformshost { public extendedwindowsformshost() { childchanged += onchildchanged; } private void onchildchanged(object sender, childchangedeventargs childchangedeventargs) { var previouschild = childchangedeventargs.previouschild control; if (previouschild != null) { previouschild.mousedown -= onmousedown; } if (child != null) { child.mousedown += onmousedown; } } private void onmousedown(object sender, mouseeventargs mouseeventargs) { mousebutton? wpfbutton = converttowpf(mouseeventargs.button); if (!wpfbutton.hasvalue) return; raiseevent(new mousebuttoneventargs(mouse.primarydevice, 0, wpfbutton.value) { routedevent = mouse.mousedownevent, source = this, }); } private mousebutton? converttowpf(mousebuttons winformbutton) { switch (winformbutton) { case mousebuttons.left: return mousebutton.left; case mousebuttons.none: return null; case mousebuttons.right: return mousebutton.right; case mousebuttons.middle: return mousebutton.middle; case mousebuttons.xbutton1: return mousebutton.xbutton1; case mousebuttons.xbutton2: return mousebutton.xbutton2; default: throw new argumentoutofrangeexception("winformbutton"); } } }
Comments
Post a Comment