c# - Double-click event fires at window level and control level, even when it is handled within the control -


i working on wpf application have window level double-click event maximizes app window when user double-clicks anywhere within window. however, have custom control within app window, , separate double-click event within custom control. when user double-clicks in custom control, control's double-click event should fire, , window should not resize. code looks this:

public class window  {     private void window_doubleclick(object sender, eventargs e)      {         /// maximize/minimize window     }      private void mycustomcontrol_doubleclick(object sender, eventargs e)      {         /// other things, please don't resize!         e.handled = true;     }  } 

and xaml looks this:

<window x:class="myproject.mywindow"      mousedoubleclick="window_doubleclick">     <grid grid.row="0" margin="0">         <local:mycustomcontrol             mousedoubleclick="mycustomcontrol_doubleclick"/>     </grid> </window> 

unfortunately, code in place, when user double-clicks in control, control event fires, window event fires well. have tried setting e.handled=true in control event, window event fires anyway. thing has stopped window level double-click event firing handling custom control's preview double-click event using this:

private void mycustomcontrol_previewmousedoubleclick(object sender, mousebuttoneventargs e)  {     e.handled = true; } 

though preview event stops window double-click before can fire, can't use without having refactor bit of code. custom control has few child controls handle double-click event. if preview event gets called @ parent control level, double-click event never tunnels down through child controls, , double-clicking doesn't want do. if possible, avoid having refactor of code in child controls. being said, question this: there way stop window-level double-click event firing once control's double-click event has been handled? i've been driving myself crazy trying figure out.. appreciated!

the problem is, mousedoubleclick appears bubble, doesn't. msdn:

although routed event seems follow bubbling route through element tree, direct routed event raised along element tree each uielement. if set handled property true in mousedoubleclick event handler, subsequent mousedoubleclick events along route occur handled set false.

instead use mouseleftbuttondown event , check clickcount:

private void mycustomcontrol_mouseleftbuttondown(object sender, mousebuttoneventargs e) {     if (e.clickcount == 2)     {         //do         e.handled = true;     } } 

note: not work on mouseleftbuttonup reason.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -