c# - how to access elements in a StackPanel in listbox -


i have listbox contains stackpanel contains image , textblock. want access textblock via c# (code-behind) change it's font manually.

relevent xaml:

<listbox x:name="categorieslistbox" margin="0,0,-12,0" itemssource="{binding categories}" selectionchanged="categorieslistbox_selectionchanged" >             <listbox.itemtemplate>                 <datatemplate>                     <stackpanel margin="0,0,0,17" width="432" height="62">                         <grid>                             <grid.columndefinitions>                                 <columndefinition width="40"/>                                 <columndefinition width="*"/>                             </grid.columndefinitions>                             <image x:name="catimage" source="{binding icon}"/>                             <textblock x:name="catname"  grid.column="1" text="{binding name_shown}" textwrapping="wrap" style="{staticresource phonetextlargestyle}"/>                         </grid>                     </stackpanel>                 </datatemplate>             </listbox.itemtemplate>         </listbox> 

i need access "catname" textblock cells.

try this--->

private t findfirstelementinvisualtree<t>(dependencyobject parentelement) t : dependencyobject {     var count = visualtreehelper.getchildrencount(parentelement);     if (count == 0)         return null;      (int = 0; < count; i++)     {         var child = visualtreehelper.getchild(parentelement, i);          if (child != null && child t)         {             return (t)child;         }         else         {             var result = findfirstelementinvisualtree<t>(child);              if (result != null)                 return result;          }     }     return null; } 

u can use above method in way--->

listboxitem item = this.list.itemcontainergenerator.containerfromindex(2) listboxitem; textblock txt = findfirstelementinvisualtree<textblock>(item); txt.text = "some text"; 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -