Search code examples
wpflistviewdatagridviewcolumn

WPF how to change text color of a GridViewColumn?


I have this GridViewColumn:

<GridViewColumn Width="180" Header="Status" DisplayMemberBinding="{Binding Status}"/>

Now i want to be able to change this Column text color also via code behind. I try:

<GridViewColumn Width="180" Header="Status" DisplayMemberBinding="{Binding Status}">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <TextBlock x:Name="Txt" Text="{Binding Status}" Foreground="Yellow" />
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

But it seems that the text color is not changing.


Solution

  • Since you already specifying the celltemplate for each item, you don't need to include the DisplayMemberBinding property, just remove it

    <GridViewColumn Width="180" Header="Status">
        <GridViewColumn.CellTemplate>
            <DataTemplate>
                <TextBlock x:Name="Txt" Text="{Binding Status}" Foreground="Yellow" />
            </DataTemplate>
        </GridViewColumn.CellTemplate>
    </GridViewColumn>