DJ van Wyk
DJ van Wyk

Reputation: 571

Multicolor text in WPF DataGrid Cell

I know of several ways to change the text color in a datagridcell, but I'm looking for a way to display three different numbers in three different colors in the same cell. An example would look like this : Grid

I know I can do it in three different cells, but in the bigger picture of my "pivot" grid that will not be an option.

In case it makes any difference; I plan to set this through code (C#) in an observable collection.

My version of the answer: Out of interest sake (and for posterity) this is the code I used to build the colors into the 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 up changing the background color instead of the font color, but the idea stays the same. Thank you @xxMUROxx for your guidance.

Upvotes: 0

Views: 1170

Answers (1)

Michael Mairegger
Michael Mairegger

Reputation: 7301

You can use the DataGridTemplateColumn as 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 it with C# you should put your DataTemplate into a resource and reference it by code:

DataGrid dg;
var dgt = new DataGridTemplateColumn();
dgt.CellTemplate = // locate resource here
dg.Columns.Add(dgt);

Upvotes: 3

Related Questions