如何更改 WPF 中 gridViewColumn 的文本颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23129866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:34:03  来源:igfitidea点击:

How to change text color of a gridViewColumn in WPF?

c#.netwpflistviewgridviewcolumn

提问by nizam uddin

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,我想在其中显示数据库中的项目。它工作正常,但我想在紫色列表视图中将单元格中显示的项目显示为白色,该怎么做?

<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>

回答by Sajeetharan

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

您需要使用 DataTemplate 并更改文本 Foreground 属性,这是 GridViewColumn 的一个示例。

Check on DataTemplate here: Data Templating Overview

在此处检查 DataTemplate:数据模板概述

<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>

回答by pay

As per the accepted answer, in order for the TextBlockto remain binded and the Foregroundcolor to change, the following worked for me:

根据接受的答案,为了TextBlock保持绑定并Foreground改变颜色,以下对我有用:

<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>