c# - How to set focus to a bound ListboxItem by pressing its child element? -


i developping small wpf application consist in displaying observablecollection<> in others observablecollection<>, , on.

here code example of application looks like:

<listbox name="mainlist" itemssource={binding}>     <listbox.itemtemplate>         <datatemplate>             <stackpanel>                        <textblock text={binding mainname} />                 <button>add item</button>                 <button>delete item</button>                  <listbox name="childlist" itemssource="{binding path=childitem}">                     <listbox.itemtemplate>                         <datatemplate>                             <textblock text={binding childname} />                         </datatemplate>                     </listbox.itemtemplate>                 </listbox>              </stackpanel>         </datatemplate>     </listbox.itemtemplate> </listbox> 

and visually pretty looks this:

application example

edit:

i re-explain trying do.

  • whenever click button a or button b want select mainlist listboxitem in contained (i.e: a item)

  • and in second time whenever click button b:

    • i want sure listboxitem selected in childlist(second listbox in picture)
    • and if so, want delete in code-behind.

but main problem since generated bindings cannot get, far, element childlist because childlist duplicated in of mainlist listboxitem.

if understand problem want first click on button of unselected item select mainitem, , on next click, when mainitem selected, preform click action. try when button clicked:

private listboxitem finditemcontainer(dependencyobject obj) {    while (obj != null && !(obj listboxitem))    {        obj = visualtreehelper.getparent(obj);    }     if (obj != null)        return obj listboxitem;    else        return null; }  private void button_click(object sender, routedeventargs e) {    var lbi = finditemcontainer(sender dependencyobject);    if (lbi != null)    {        if (lbi.isselected)        {            //do click event        }        else            lbi.isselected = true;    } } 

of course can more mvvm way binding listboxitem.isselected lets bool mainitem.myitemisselected

<listbox.itemcontainerstyle>     <style targettype="{x:type listboxitem}">         <setter property="isselected" value="{binding path=myitemisselected, mode=twoway}"/>     </style> </listbox.itemcontainerstyle> 

and button.command icommand mainitem.deletecommand , when command executed that:

if (myitemisselected) {     //do command body } else     myitemisselected = true; 

which better long term because replicate selecteditem behaviour in childlist object (add myitemisselected , bind inner 'listboxitem.isselected, discribed above) , add myselecteditem property childlist:

childitem myselecteditem {       {       return items.firstordefault(n=>n.myitemisselected);    } } 

and delete command this:

if (myitemisselected) {     childitem selitem = childitems.myselecteditem;     if (selitem != null) childitems.items.remove(selitem); } else     myitemisselected = true; 

if data bound , lists observablecollections can in object , ui follow. can child selection binding bit , still use first solution , in button_click this:

private void button_click(object sender, routedeventargs e) {    var lbi = finditemcontainer(sender dependencyobject);    if (lbi != null)    {        if (lbi.isselected)        {            mainitem mainitem = lbi.content mainitem;            childitem selchild = mainitem.childitems.myselecteditem;            if (selchild != null) mainitem.childitems.items.remove(selchild);        }        else            lbi.isselected = true;    } } 

here simple, working example on dropbox


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -