change style of each item in listbox C# Wpf -


i want change style of each item in listbox this

enter image description here

but in case, want change color , font style (italic, bold) of each item, items in listbox added programmatically

how change style of each item?

you can create view model class data items has necessary properties, like

class dataitem {     public string text { get; set; }     public brush foreground { get; set; }     public fontstyle fontstyle { get; set; }     public fontweight fontweight { get; set; } } 

and bind these properties in itemtemplate of listbox. example below assumes items property in view model returns collection of dataitem objects.

<listbox itemssource="{binding items}">     <listbox.itemtemplate>         <datatemplate>             <textblock text="{binding text}"                        foreground="{binding foreground}"                        fontstyle="{binding fontstyle}"                        fontweight="{binding fontweight}"/>         </datatemplate>     </listbox.itemtemplate> </listbox> 

if prefer use boolean properties e.g. isbold in item view model, may add itemcontainerstyle datatriggers set appropriate listboxitem properties:

<listbox itemssource="{binding items}">     <listbox.itemtemplate>         <datatemplate>             <textblock text="{binding text}"                        foreground="{binding foreground}"/>         </datatemplate>     </listbox.itemtemplate>     <listbox.itemcontainerstyle>         <style targettype="listboxitem">             <style.triggers>                 <datatrigger binding="{binding isitalic}" value="true">                     <setter property="fontstyle" value="italic"/>                 </datatrigger>                 <datatrigger binding="{binding isbold}" value="true">                     <setter property="fontweight" value="bold"/>                 </datatrigger>             </style.triggers>         </style>     </listbox.itemcontainerstyle> </listbox> 

Popular posts from this blog