.net - count controls of type A under the mouse in WPF -


i have custom panels on canvas, there b panels, how can count panels located mouse cursor actually?

i know achieved visualtreehelper.hittest, didn't have chance, returns elements on custom panels or nothing @ all...

this code

<usercontrol x:class="wpfapplication7.usercontrol1">     <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>  <window x:class="wpfapplication7.mainwindow" previewmouseleftbuttondown="window_previewmouseleftbuttondown" xmlns:my="clr-namespace:wpfapplication7">     <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> 

.cs

public partial class mainwindow : window {     public mainwindow()     {         initializecomponent();     }      list<usercontrol1> ucs = new list<usercontrol1>();      private void window_previewmouseleftbuttondown(object sender, mousebuttoneventargs e)     {         getucscount(e);         console.writeline("ucs.count = {0}", ucs.count);     }      private void getucscount(mousebuttoneventargs e)     {         ucs.clear();          point p = e.getposition(this);          visualtreehelper.hittest(this, null,              new hittestresultcallback(myhittestcallback),              new pointhittestparameters(p));     }      hittestresultbehavior myhittestcallback(hittestresult result)     {         if (result.visualhit.gettype() == typeof(usercontrol1))         {             ucs.add(result.visualhit usercontrol1);         }          return hittestresultbehavior.continue;     } } 

the result == 0 anywhere click...

alt text

for each hit in hittestresultcallback can try find parent usercontrol1 , add list if hasn't been added yet

hittestresultbehavior myhittestcallback(hittestresult result) {     dependencyobject visualhit = result.visualhit;     usercontrol1 parentusercontrol = getvisualparent<usercontrol1>(visualhit);     if (parentusercontrol != null && ucs.indexof(parentusercontrol) < 0)     {         ucs.add(parentusercontrol usercontrol1);     }     return hittestresultbehavior.continue; } public static t getvisualparent<t>(object childobject) t : visual {     dependencyobject child = childobject dependencyobject;     while ((child != null) && !(child t))     {         child = visualtreehelper.getparent(child);     }     return child t; } 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -