Multicolor text in WPF DataGrid Cell -
i know of several ways change text color in datagridcell, i'm looking way display 3 different numbers in 3 different colors in same cell. example :
i know can in 3 different cells, in bigger picture of "pivot" grid not option.
in case makes difference; plan set through code (c#) in observable collection.
my version of answer: out of interest sake (and posterity) code used build colors grid :
string xaml = string.format(@"<datatemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'> <stackpanel orientation=""horizontal""> <stackpanel.background> <lineargradientbrush startpoint=""0,0"" endpoint=""1,0""> <gradientstop color=""{{binding path={0}_missing_color}}"" offset=""0.0""/> <gradientstop color=""{{binding path={0}_found_color}}"" offset=""0.5""/> <gradientstop color=""{{binding path={0}_empty_color}}"" offset=""1.0""/> </lineargradientbrush> </stackpanel.background> <textblock text=""{{binding path={0}_missing}}"" foreground=""black"" textalignment=""right"" width=""25""/> <textblock text=""{{binding path={0}_found}}"" foreground=""black"" textalignment=""right"" width=""25""/> <textblock text=""{{binding path={0}_empty}}"" foreground=""black"" textalignment=""right"" width=""25""/> </stackpanel> </datatemplate>", date); stringreader stringreader = new stringreader(xaml); xmlreader xmlreader = xmlreader.create(stringreader); datatemplate tdate = (datatemplate)system.windows.markup.xamlreader.load(xmlreader); cdate.celltemplate = tdate; targetgrid.columns.add(cdate);
i ended changing background color instead of font color, idea stays same. thank @xxmuroxx guidance.
you can use datagridtemplatecolumn
follows
<datagrid> <datagrid.columns> <datagridtemplatecolumn> <datagridtemplatecolumn.celltemplate> <datatemplate> <stackpanel orientation="horizontal"> <textblock text="{binding path=number1}" foreground="red"/> <textblock text="{binding path=number2}" foreground="green" /> <textblock text="{binding path=number3}" foreground="blue" /> </stackpanel> </datatemplate> </datagridtemplatecolumn.celltemplate> </datagridtemplatecolumn> </datagrid.columns> </datagrid>
doing c# should put datatemplate
resource , reference code:
datagrid dg; var dgt = new datagridtemplatecolumn(); dgt.celltemplate = // locate resource here dg.columns.add(dgt);
Comments
Post a Comment