.net - Hit Test behavior -
i have following code
public partial class mainwindow : window { public mainwindow() { initializecomponent(); } list<uielement> ucs = new list<uielement>(); private void window_previewmouseleftbuttondown(object sender, mousebuttoneventargs e) { ucs.clear(); point p = e.getposition((uielement)sender); visualtreehelper.hittest(this, null, new hittestresultcallback(myhittestcallback), new pointhittestparameters(p)); console.writeline("ucs.count = {0}", ucs.count); foreach (var item in ucs) { console.writeline("item: {0}", item.tostring()); } } hittestresultbehavior myhittestcallback(hittestresult result) { ucs.add(result.visualhit uielement); return hittestresultbehavior.continue; } }
this window
<window> <grid> <my:usercontrol1 horizontalalignment="left" margin="82,88,0,0" x:name="usercontrol11" verticalalignment="top" /> <my:usercontrol1 horizontalalignment="left" margin="168,166,0,0" x:name="usercontrol12" verticalalignment="top" /> <my:usercontrol1 horizontalalignment="left" margin="231,130,0,0" x:name="usercontrol13" verticalalignment="top" /> </grid> </window>
this uc
<usercontrol> <grid> <label content="label" height="44" horizontalalignment="left" name="label1" verticalalignment="top" fontsize="20" fontweight="bold" width="78" background="#ff4b9fc4" borderbrush="#ff020a0d" borderthickness="1" /> </grid> </usercontrol>
this output when click on user control, on intersection of 2 usercontrols:
ucs.count = 2 item: system.windows.controls.border item: system.windows.controls.border ucs.count = 3 item: system.windows.controls.border item: system.windows.controls.border item: system.windows.controls.border
why this? usercontrol under mouse instance?
ps:
now, when have on label borderthickness = 0
ucs.count = 3 item: system.windows.controls.textblock item: system.windows.controls.border item: system.windows.controls.border ucs.count = 3 item: system.windows.controls.textblock item: system.windows.controls.border item: system.windows.controls.border
the usercontrol1
invisible. contents visible, usercontrol1
instance not have visuals of own. (and never will. user control's job contain other things.)
hit testing reports elements make direct contribution visual tree. , since hit testing considers each element in isolation, means elements acting purely containers don't show up. (and related fact hit testing considers pixels got painted. if have border
you've set borderbrush
, non-zero borderthickness
have no background
, hit test consider border outline hit - points inside of border not considered hit border because it's not painting in interior.
if need hit testing of "this thing, or inside thing" style, either either
- use mouse enter/leave events - bubble, , raised on invisible container elements
- use
ismouseover
or - use hit test function you're using, passing user control first argument, , treat hit indication hit test point inside user control
the third 1 more complex, if need hit test points other 1 under mouse, you'd need use it.
Comments
Post a Comment