How to display strings in a WPF ListView in different colours? -


i fill wpf listview strings using databinding. code looks (and work! ;) ):

xaml:

<listview              itemssource="{binding entries}"> </listview> 

i left out code better overview. entries ilist<string>.

so far, working fine. comes problem: string in entries may contain particular keyword indicates string wants displayed red background inside listview. have method getbackground(string s) returns colour depending on string.

how can make listview display items in correct colour. first idea have converter converting string colour using abovementioned method. have add converter , how pass string converter parameter? first idea was:

<listview     itemssource="{binding entries, converter={staticresource entrytocolourconverter},  converterparameter=???}" </listview> 

does have idea how done? on right track?

best wishes, christian

edit 1: changed code (as first step) towards:

<usercontrol.resources>         <datatemplate x:key="entrytemplate">             <textblock                  text="{binding}"                 background="green"/>         </datatemplate> </usercontrol.resources>  ...  <listview              itemssource="{binding entries}"             itemtemplate="{staticresource entrytemplate}> </listview> 

however, not work @ all. if change text static value, result still same previous code.

edit 2: found problem, code looked this:

<listview x:name="lventries"                   itemtemplate="{staticresource entriestemplate}"                   itemssource="{binding entries, notifyontargetupdated=true}">              <listview.view>                  <gridview x:name="gventries">                     <gridviewcolumn                         headercontainerstyle="{staticresource hcs}"                         header="entry"                         textblock.textalignment="left">                     </gridviewcolumn>                 </gridview>             </listview.view>          </listview> 

and listview.view problem. on removing that, working! :) have find out how solve without listview.view

<window.resources>     <local:entrytobackgroundconverter x:key="entrytobackgroundconverter"/>     <datatemplate x:key="entrytemplate">         <textblock text="{binding .}" background="{binding ., converter={staticresource entrytobackgroundconverter}}"/>     </datatemplate>         </window.resources> <grid>                   <listview itemssource="{binding entries}" itemtemplate="{staticresource entrytemplate}"></listview>         </grid> 

converter:

public class entrytobackgroundconverter : ivalueconverter {     public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture)     {         string stringvalue = value string;          if (string.isnullorempty(stringvalue))             return brushes.black;          if (stringvalue == "foreach")             return brushes.blue;         if (stringvalue == "if")             return brushes.blue;          return brushes.black;     }      public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture)     {         return value;     } } 

you're on right lines... should work...


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -