简体   繁体   中英

How to change text color of a gridViewColumn in WPF?

I have A listView In which i want to show items from dataBase. It works fine, But i want to see the items shown in cells as white in a purple listview, How to do it?

 <ListView Margin="127,114,227,357" x:Name="lv" Background="purple" > <ListView.View> <GridView> <GridViewColumn DisplayMemberBinding="{Binding Path=FirstName}" Header="First Name" Width="100" /> <GridViewColumn DisplayMemberBinding="{Binding Path=LastName}" Header="Last Name" Width="100" /> <GridViewColumn DisplayMemberBinding="{Binding Path=Email}" Header="Email" Width="100" /> <GridViewColumn DisplayMemberBinding="{Binding Path=Password}" Header=" Password" Width="100" /> <GridViewColumn DisplayMemberBinding="{Binding Path=Address}" Header="Address" Width="100" /> </GridView> </ListView.View>

You need to use DataTemplate and change the text Foreground property, this is one sample for GridViewColumn.

Check on DataTemplate here: Data Templating Overview

<GridViewColumn DisplayMemberBinding="{Binding Path=FirstName}" Header="First Name" Width="1000"> <GridViewColumn.CellTemplate> <DataTemplate> <TextBlock x:Name="Txt" Text="{Binding FirstName}" Foreground="Purple" /> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn>

As per the accepted answer, in order for the TextBlock to remain binded and the Foreground color to change, the following worked for me:

 <GridViewColumn Header="First Name" Width="1000"> <GridViewColumn.CellTemplate> <DataTemplate> <TextBlock x:Name="Txt" Text="{Binding Path=FirstName}" Foreground="Purple" /> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn>

and in my case I decided to create a property for the text color and bind to that

<GridViewColumn Header="First Name" Width="1000"> <GridViewColumn.CellTemplate> <DataTemplate> <TextBlock x:Name="Txt" Text="{Binding Path=FirstName}" Foreground="{Binding Path=TextColor}" /> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn>

All wrong answers. You simply need to change GridView.ColumnHeaderTemplate property.

 <ListView ItemsSource="{...}" Foreground="White" > <ListView.View> <GridView> <GridView.ColumnHeaderTemplate> <DataTemplate> <TextBlock Foreground="White" Text="{Binding}"/> </DataTemplate> </GridView.ColumnHeaderTemplate> <GridViewColumn DisplayMemberBinding="{Binding Path=FirstName}" Header="First Name" Width="1000"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM