c# - Why are ListBoxItems added in XAML not styled? -


why listboxitems declared within xaml not affected datatemplate? template fine when bind source, , that's i'll doing ultimately, wondering why declared items aren't styled.

<listbox>      <listbox.itemtemplate>          <datatemplate>              <grid>                  <textblock text="{binding}" foreground="red"/>              </grid>           </datatemplate>                         </listbox.itemtemplate>      <listboxitem>lbi 1</listboxitem>      <listboxitem>lbi 2</listboxitem> </listbox> 

here lbi 1 , lbi 2 not red if use listbox.itemsource , bind list, items red.

you need apply style. datatemplate used apply template data bound listbox. hence not applying items directly added child within xaml.

    <listbox>         <listbox.resources>             <style targettype="listboxitem">                 <setter property="foreground" value="red"/>             </style>         </listbox.resources>         <listboxitem>lbi 1</listboxitem>         <listboxitem>lbi 2</listboxitem>     </listbox> 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -