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

时间:2014-04-17 09:51:24

标签: c# .net wpf listview gridviewcolumn

我有一个listView我想在其中显示来自dataBase的项目。它工作正常,但我希望在紫色列表视图中看到单元格中显示的项目为白色,怎么做?

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

2 个答案:

答案 0 :(得分:5)

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

在此处检查DataTemplate: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>

答案 1 :(得分:0)

根据公认的答案,为了使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>

在我的情况下,我决定为文本颜色创建一个属性并将其绑定到该颜色

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