how to give different text color for each item of listview in xamarin forms -
i have list of string getting assigned listview in xamarin forms in below code
` list<string> list = new list<string>(); list.add("a"); list.add"b"); list.add("c"); list.add("d"); var listview = new listview(); listview.itemssource = list; content = listview;
` how show listview items text in different color like
a --> in red color
b --> in blue color
c --> in green color
d --> in yellow color
you can use ivalueconverter in binding
public class stringtocolorconverter : ivalueconverter { #region ivalueconverter implementation public object convert (object value, type targettype, object parameter, system.globalization.cultureinfo culture) { if (value string && value != null) { string s = (string)value; switch(s){ case "a": return color.red; case "b": return color.blue; default: return color.black } } return color.black; } public object convertback (object value, type targettype, object parameter, system.globalization.cultureinfo culture) { throw new notimplementedexception (); } #endregion }
usage in xaml: add namespace:
xmlns:local="clr-namespace:meditation;assembly=meditation"
create , add page staticresources:
<contentpage.resources> <resourcedictionary> <local: stringtocolorconverter x:key="cnvinvert"></local: stringtocolorconverter > </resourcedictionary> </contentpage.resources>
add binding:
<label verticaloptions="end" horizontaloptions="center" text="{binding ., converter={staticresource cnvinvert}}">
Comments
Post a Comment