如何在列表视图中更改基于行颜色的Cell值

时间:2013-11-03 18:13:01

标签: c# wpf

我想根据一列中的值更新行颜色,我已经将ItemContainerStyle应用于列表视图。

代码

<ListView KeyboardNavigation.TabIndex="3" x:Name="xListView"
          SelectionMode="Extended" ScrollViewer.CanContentScroll="True" 
          ScrollViewer.VerticalScrollBarVisibility="Auto"
      DataContext="{Binding Path=FilteredItems}" ItemsSource="{Binding}" 
          GridViewColumnHeader.Click="ListViewHeader_Click"
          Loaded="xListView_Loaded"
      ItemContainerStyle="{StaticResource alternatingListViewItemStyle}" 
          AlternationCount="2"
          MouseDoubleClick="xListView_MouseDoubleClick">
    <ListView.View>
    <GridView>
        <GridViewColumn Header="{x:Static res:RecoveryManager.ColHeader_Name}" 
                        DisplayMemberBinding="{Binding Path=Name}"
                        HeaderStringFormat="{}{0}      "/>
        <GridViewColumn Header="{x:Static res:AccountManager.ColHeader_ID}" 
                        DisplayMemberBinding="{Binding Path=ID}"
                        HeaderStringFormat="{}{0}      "/>
        <GridViewColumn Header="{x:Static res:AccountManager.ColHeader_filename}"
                        DisplayMemberBinding="{Binding Path=filename}" 
                        HeaderStringFormat="{}{0}      "/>
        <GridViewColumn Header="{x:Static res:AccountManager.ColHeader_TransactStatus}"
                        DisplayMemberBinding="{Binding Path=TransactStatus}" 
                        HeaderStringFormat="{}{0}      ">
    <GridViewColumn.CellTemplate>
            <DataTemplate>
               <TextBlock x:Name="transType" Text="{Binding TransactStatus}"/>
               <DataTemplate.Triggers>
                 <DataTrigger Binding="{Binding TransactStatus}"
                                       Value="pending">
                    <Setter TargetName="transType" Property="Background" 
                                      Value="#FF0000" />
                 </DataTrigger>
               </DataTemplate.Triggers>
            </DataTemplate>
        </GridViewColumn.CellTemplate>
       </GridViewColumn>
    </GridView>
  </ListView.View>
</ListView>

1 个答案:

答案 0 :(得分:1)

你快到了。只需从DisplayMemberBinding中删除GridViewColumn即可,因为您已经为列提供了CellTemplate后无需进行设置。 您无需同时设置两者。

删除DisplayMemberBinding="{Binding Path=TransactStatus}",您的触发器将完美运行。