c# - Two overlapping ListBoxes. Selection problem -


i'm trying display shapes in 2 separate listboxes occupying same space. i've set background both transparent , {x:null} mouseclick still getting captured topmost listbox can't select shapes underlying listbox.

here sample code reproducing problem.

<grid>     <!-- listbox 1 -->     <listbox background="{x:null}">         <listbox.itemspanel>             <itemspaneltemplate>                 <canvas background="{x:null}"/>             </itemspaneltemplate>         </listbox.itemspanel>         <listbox.itemtemplate>             <datatemplate>                 <grid background="transparent">                     <ellipse width="100" height="100" stroke="blue" strokethickness="10"/>                 </grid>             </datatemplate>         </listbox.itemtemplate>         1     </listbox>      <!-- listbox 2 -->     <listbox background="{x:null}">         <listbox.itemspanel>             <itemspaneltemplate>                 <canvas background="{x:null}"/>             </itemspaneltemplate>         </listbox.itemspanel>         <listbox.itemcontainerstyle>             <style targettype="listboxitem">                 <setter property="canvas.left" value="100"/>                 <setter property="canvas.top" value="100"/>             </style>         </listbox.itemcontainerstyle>         <listbox.itemtemplate>             <datatemplate>                 <ellipse width="100" height="100" stroke="blue" strokethickness="10"/>             </datatemplate>         </listbox.itemtemplate>         1     </listbox> </grid> 

this how solved problem i'm more open other suggestions :) enabled hittesting on grid , disabled both listboxes. did hittesting in event handler instead

<grid mousedown="grid_mousedown"       ishittestvisible="true"       background="transparent">      <!-- listbox 1 -->     <listbox background="transparent"              ishittestvisible="true"              ..>     </listbox>      <!-- listbox 2 -->     <listbox background="transparent"              ishittestvisible="true"              ..>     </listbox> </grid> 

event handler , hittesting

private void grid_mousedown(object sender, mousebuttoneventargs e) {     grid grid = sender grid;     point ptcurrent = e.getposition(grid);     visualtreehelper.hittest(grid, null, new hittestresultcallback(hittestcallback), new pointhittestparameters(ptcurrent)); } public hittestresultbehavior hittestcallback(hittestresult htrresult) {     listboxitem listboxitem = getvisualparent<listboxitem>(htrresult.visualhit);     if (listboxitem != null)     {         listboxitem.isselected = true;         return hittestresultbehavior.stop;     }     return hittestresultbehavior.continue; } public t getvisualparent<t>(object child) t : visual {     dependencyobject c = child dependencyobject;     while ((c != null) && !(c t))     {         c = visualtreehelper.getparent(c);     }     return c t; } 

you can bind multiple sets of data single listbox using compositecollection items source:

<listbox ...>   <listbox.itemssource>     <compositecollection>       <collectioncontainer collection={binding ...}" />       <collectioncontainer collection={binding ...}" />     </compositecollection>   </listbox.itemssource> </listbox> 

you can use data templates control appearance of different data types. can either use implicit data templates or can use itemtemplateselector.

for more information using , binding compositecollection, see resource: how bind collectioncontainer collection in view model?


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -