Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
558
Color Text of Unbound Field
posted

Hi,

I have an unbound field in a XamDataGrid that contains two numbers (value, percentage change).  Is there a way that I can color one of them to be red while keeping the other black?  If it's easy, I can change the value of the field to be html so that I can define the color of each section on the fly.  In my testing, I was unable to determine how to use the color nor could I get the html idea to work.

Thanks,

Kyle

Parents
No Data
Reply
  • 9836
    posted

    Hello Kyle,

    It is possible to change the default ItemTemplate of the editor that is used in each cell so that the TextBlock contains multiple Run elements. Then you should be able to specify different foreground for each one. Another option is to replace the TextBlock with a ContentControl and bind the Content with a converter. In the converter you will return new Textblock control with custom Inline collection:

     <Border x:Name="MainBorder" 
                   Background="{TemplateBinding Background}" 
                   BorderBrush="{TemplateBinding BorderBrush}" 
                   BorderThickness="{TemplateBinding BorderThickness}" 
                   SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"> 
                   <ContentControl Content="{TemplateBinding DisplayText,
                                              Converter={StaticResource conv}}"/> 
     </Border>
     

     Converter's code:

     

     public  object  Convert(object  value, Type  targetType, object  parameter, System.Globalization.CultureInfo  culture)
             {
                 string  s = value.ToString();
     
                 Run  r1 = new  Run () { Text = s, Foreground = Brushes .Red };
                 Run  r2 = new  Run () { Text = "%" , Foreground = Brushes .Blue };
     
                 TextBlock  tb = new  TextBlock ();
                 tb.Inlines.Add(r1);
                 tb.Inlines.Add(r2);
     
                 return  tb;
     
             }
     

     

    Let me know if this works for you and if you have any questions.

Children