c# - Different background on each Listbox item -


i have listbox containing item binding on table consist of 5 columns (idstory, title, created, textstory, , feeling).

<listbox x:name="mainlistbox" height="418" selectionchanged="mainlistbox_selectionchanged"> <listbox.itemtemplate>     <datatemplate>         <stackpanel x:name="listpanel" orientation="horizontal" verticalalignment="top" horizontalalignment="left" margin="0,5,0,0">             <rectangle width="100" height="100" fill="#e34d64" />             <stackpanel orientation="vertical" horizontalalignment="left" verticalalignment="center" margin="15,0,0,0">                 <textblock text="{binding title}" fontsize="26" foreground="black"/>                 <textblock text="{binding created}" fontsize="20" foreground="black"/>                 <textblock text="{binding textstory}" fontsize="20" foreground="black" fontstyle="italic"/>             </stackpanel>             <toolkit:contextmenuservice.contextmenu>                 <toolkit:contextmenu>                     <toolkit:menuitem x:name="menuedit" header="edit story" click="menuedit_click"/>                     <toolkit:menuitem x:name="menudelete" header="delete story" click="menudelete_click"/>                 </toolkit:contextmenu>             </toolkit:contextmenuservice.contextmenu>         </stackpanel>     </datatemplate> </listbox.itemtemplate> 

it works well, want change background on each item depends on column feeling. example if string "sad" listbox item have blue background http://imgur.com/n5longj

what should making real? appreciated. thank you.

for purpose propose binding background of textbox (or maybe better whole stackpanel - choice) feeling property converter:

<textblock text="{binding title}" fontsize="26" foreground="black" background={binding feeling, converter={staticresource txttobrush}}/> 

you have add key converter somewhere in resources:

xmlns:conv="clr-namespace:yournamespace" <conv:texttobrush x:key="txttobrush"/> 

and converter can this:

public class texttobrush : ivalueconverter {     list<tuple<string, brush>> textbrush = new list<tuple<string, brush>>()     {         new tuple<string, brush>("sad", new solidcolorbrush(colors.blue))     };      public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture)     {         if (value == null)             return new solidcolorbrush(colors.transparent);         else         {             foreach (tuple<string,brush> item in textbrush)                 if ((value string) == item.item1) return item.item2 brush;         }         return new solidcolorbrush(colors.transparent);     }      public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture)     {         throw new notimplementedexception();     } } 

and of course property bgcolor should raise onpropertychanged (your item class should impement inotifypropertychanged).


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -