How to set DataGridTextColumn text color?
Asked Answered
D

3

14

I'm trying to change the color of a DataGridTextColumn.

Here's what I'm doing:

<DataGridTextColumn 
    Header="Status" 
    Binding="{Binding IsActive, 
               Converter= {StaticResource BoolToStatusConverter}}"
    Foreground="{Binding Path=IsActive,
               Converter={StaticResource BoolToColorConverter}}"/>

Text is set properly, but the color won't change, and I'm getting following error:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or 
FrameworkContentElement for target element. BindingExpression:Path=IsActive; 
DataItem=null; target element is 'DataGridTextColumn' (HashCode=40349079); target 
property is 'Foreground' (type 'Brush')

What should I do for this to work?

Disseise answered 8/6, 2012 at 14:35 Comment(2)
are you tring to set the background for the whole column, or individual cells in a colum?Nipa
I'm trying to set FOREGROUND for individual cellsDisseise
N
16

You need to specify a Style with a DataTrigger for the column's CellStyle. e.g.

<Page.Resources>
    <Style TargetType="DataGridCell" x:Key="ActiveCellStyle">
        <Setter Property="Foreground" Value="Blue"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsActive}" Value="{x:Null}">
                <Setter Property="Foreground" Value="Green"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding IsActive}" Value="True">
                <Setter Property="Foreground" Value="Red"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
    <Converters:BoolToTextConverter 
        x:Key="BoolToStatusConverter" 
        TargetCondition="True" 
        IsMatchValue="It's active" 
        IsNotMatchValue="It's dead" />
</Page.Resources>
<Grid>
    <DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn 
                Header="Status" 
                Binding="{Binding IsActive, 
                    Converter={StaticResource BoolToStatusConverter}}" 
                CellStyle="{StaticResource ActiveCellStyle}"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>
Nipa answered 8/6, 2012 at 18:28 Comment(5)
Is active may be null. How do I specify that?Disseise
@ArsenZahray: added trigger for null. I assume your status converter handles nulls.Nipa
Apologies as I am new to WPF. I got unresolved symbol error for "Converters" tag in your code, what other code do I need to define?Factorize
@DannyTan Converters is the namespace of the BoolToTextConverter - can't remember whether I created any real code for this or not.Nipa
@Phil: I am getting the following error: The namespace prefix 'CONVERTER' is not defined. Could you please add the code for this to work for everyone?Beriosova
A
9

While not technically a DataGridTextColumn, this is what I usually do:

<DataGridTemplateColumn Header="Status" SortMemberPath="Status">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Status}" Foreground="{Binding Status, Converter={StaticResource StatusToSolidColor}}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

I get the datacontext that I want, and I can resuse converters that I may already have in place in the rest of the application. Futhermore, I don't have to hard code / maintain an extra set of styles and data triggers to get the desired effect.

Aster answered 26/6, 2015 at 13:45 Comment(0)
C
-1

Foreground is a Brush, not a Color. It can parse a color in XAML, but that's not used when you create a binding with a converter.

Use a BoolToBrushConverter, or create a SolidColorBrush as the foreground and bind its "Color" property to the BoolToColorConverter. Like so:

<DataGridTextColumn Header="Status">
    <DataGridTextColumn.Foreground>
        <SolidColorBrush Color="{Binding Path=IsActive, Converter={StaticResource BoolToColorConverter}}" />
    </DataGridTextColumn.Foreground>
</DataGridTextColumn>
Canonry answered 8/6, 2012 at 15:47 Comment(2)
I've noticed that, but my problem is, that WPF does see property IsActive in Binding and does not see it in Foreground. In the mean time, I've modified BoolToColorConverter to return solid brushes instead of colorsDisseise
Binding the Foreground property of a Column does not work.Kazoo

© 2022 - 2024 — McMap. All rights reserved.