xaml - Different item templates in ComboBox -


in winrt xaml have combobox displays complex item.

is possible declare , bind 2 different item templates 1) "normal" selected item (combobox closed) , 2) list in popup when user wants choose item (combobox open)?

i found itemtemplate work both, want display items different template if user wants choose item.

use item template selector. define 2 properties selectedtemplate , dropdowntemplate in selector class. check whether container wrapped in comboboxitem. if yes choose dropdowntemplate. if not choose selectedtemplate.

public class comboboxitemtemplateselector : datatemplateselector {     public datatemplate selectedtemplate { get; set; }     public datatemplate dropdowntemplate { get; set; }      protected override datatemplate selecttemplatecore(object item, dependencyobject container)     {         var comboboxitem = container.getvisualparent<comboboxitem>();         if (comboboxitem == null)         {             return selectedtemplate;         }         return dropdowntemplate;     }  }  public static class dependencyobjectextensions {     public static t getvisualparent<t>(this dependencyobject child) t : frameworkelement     {         while ((child != null) && !(child t))         {             child = visualtreehelper.getparent(child);         }         return child t;     } } 

combobox in xaml,

<page.resources>     <local:comboboxitemtemplateselector x:key="selector">         <local:comboboxitemtemplateselector.dropdowntemplate>             <datatemplate>                 <stackpanel orientation="horizontal">                     <textblock text="{binding id}"/>                     <textblock text="{binding name}" margin="5 0 0 0"/>                 </stackpanel>             </datatemplate>         </local:comboboxitemtemplateselector.dropdowntemplate>         <local:comboboxitemtemplateselector.selectedtemplate>             <datatemplate>                 <stackpanel orientation="horizontal">                     <textblock text="{binding name}"/>                 </stackpanel>             </datatemplate>         </local:comboboxitemtemplateselector.selectedtemplate>     </local:comboboxitemtemplateselector> </page.resources> <grid background="{staticresource applicationpagebackgroundthemebrush}">     <combobox x:name="combo"               itemtemplateselector="{staticresource selector}">      </combobox> </grid> 

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 -