c# - Listbox ItemTemplate Selector does not pick a template -


i trying use itemtemplateselector on listbox within grid creating on different file later called mainwindow.

here datatemplateselector code

 public class templateselector : datatemplateselector {     public override datatemplate selecttemplate(object item, dependencyobject container)     {         frameworkelement element = container frameworkelement;         if (element != null && item != null && item myclass)         {             myclass agg = item myclass;             if(agg.mytype == a)             {                 return element.findresource("greenitemtemplate") datatemplate;             }             else if (agg.mytype == b)             {                 return element.findresource("yellowitemtemplate") datatemplate;             }             else if (agg.mytype == c)             {                 return element.findresource("reditemtemplate") datatemplate;             }         }          return null;     } } 

here xaml

 <grid x:class="ns.views.listview"   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   xmlns:c="clr-namespace:ns.commons"   datacontext="{binding}"> <grid.resources>     <c:myconverter x:key="converter" />     <c:templateselector x:key="borderselector" />     <datatemplate x:key="greenitemtemplate">         <border borderthickness="3" borderbrush="green">             <stackpanel horizontalalignment="stretch">                 <textblock text="{binding path=groupname}"/>                 <textblock text="{binding path=mytype}"/>             </stackpanel>         </border>     </datatemplate>            <datatemplate x:key="reditemtemplate">         <border borderthickness="3" borderbrush="red">             <stackpanel horizontalalignment="stretch">                 <textblock text="{binding path=groupname}"/>                 <textblock text="{binding path=mytype}"/>             </stackpanel>         </border>     </datatemplate>    <datatemplate x:key="yellowitemtemplate">         <border borderthickness="3" borderbrush="yellow">             <stackpanel horizontalalignment="stretch">                 <textblock text="{binding path=groupname}"/>                 <textblock text="{binding path=mytype}"/>             </stackpanel>         </border>     </datatemplate> </grid.resources>  <listbox itemssource="{binding mycollectionofmyclassobjects}"           name="listbox1"          horizontalcontentalignment="stretch"            itemtemplateselector="{staticresource borderselector}"          > </listbox> 

however, although binding works fine (the list of non-formatted objects appears in list), itemtemplateselector not calling templateselector methods. put breakpoint @ beginning of methods , it's never called.

does knows problem?

@rachel right. datatemplateselector gets invoked once @ load pretty , not inpc changes.

what can require use listboxitemstyle datatrigger switching template used

something like:

<controltemplate x:key="greenitemtemplate">   <border borderbrush="green"           borderthickness="3">     <stackpanel horizontalalignment="stretch">       <textblock text="{binding path=groupname}" />       <textblock text="{binding path=mytype}" />     </stackpanel>   </border> </controltemplate> <controltemplate x:key="reditemtemplate">   <border borderbrush="red"           borderthickness="3">     <stackpanel horizontalalignment="stretch">       <textblock text="{binding path=groupname}" />       <textblock text="{binding path=mytype}" />     </stackpanel>   </border> </controltemplate> <controltemplate x:key="yellowitemtemplate">   <border borderbrush="yellow"           borderthickness="3">     <stackpanel horizontalalignment="stretch">       <textblock text="{binding path=groupname}" />       <textblock text="{binding path=mytype}" />     </stackpanel>   </border> </controltemplate> <style x:key="mylistboxitemstyle"         targettype="{x:type listboxitem}">   <setter property="template"           value="{dynamicresource greenitemtemplate}" />   <style.triggers>     <datatrigger binding="{binding mytype}"                   value="c">       <setter property="template"               value="{dynamicresource reditemtemplate}" />     </datatrigger>     <datatrigger binding="{binding mytype}"                   value="b">       <setter property="template"               value="{dynamicresource yellowitemtemplate}" />     </datatrigger>   </style.triggers> </style> 

and usage:

<listbox itemcontainerstyle="{staticresource mylistboxitemstyle}"          itemssource="{binding mycollectionofmyclassobjects}"           name="listbox1"          horizontalcontentalignment="stretch" /> 

Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -