如何更改Listview

时间:2017-11-09 02:18:53

标签: c# wpf listview colors

我想根据值更改颜色:Pass(蓝色)和Fail(红色)。我想只改变州值的文字颜色。

main.xaml

<ListView x:Name="record_List"
           HorizontalAlignment="Left"
           VerticalAlignment="Top"
           Height="203" Width="382" Margin="553,454,0,0" 
           BorderThickness="0">
    <ListView.View>
        <GridView ColumnHeaderContainerStyle="{StaticResource myHeaderStyle}">
            <GridViewColumn Header="State" Width="52"
                            DisplayMemberBinding="{Binding state}"/>
            <GridViewColumn Header="Stuednt num" Width="80"
                            DisplayMemberBinding="{Binding snum}"/>
            <GridViewColumn Header="Name" Width="75"
                            DisplayMemberBinding="{Binding name}"/>
            <GridViewColumn Header="Check time"
                            DisplayMemberBinding="{Binding check_time}"/>
        </GridView>
    </ListView.View>
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <EventSetter Event="PreviewMouseLeftButtonDown"
                         Handler="ListViewItem_PreviewMouseLeftButtonDown"/>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

2 个答案:

答案 0 :(得分:1)

对列状态使用样式触发器,您尝试: 修正:

<GridViewColumn Header="State" Width="52" >
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding state}">
                                    <TextBlock.Style>
                                        <Style TargetType="{x:Type TextBlock}">
                                            <Setter Property="Foreground" Value="Black"></Setter>
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding state}" Value="Pass">
                                                    <Setter Property="Foreground" Value="Blue"/>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding state}" Value="Fail">
                                                    <Setter Property="Foreground" Value="Red"/>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </TextBlock.Style>
                                </TextBlock>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

答案 1 :(得分:0)

最简单的方法是使用style和DataTriggers:

    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="Background" Value="Gray"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding testResult}" Value="Pass">
                    <Setter Property="Background" Value="Green"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding testResult}" Value="Fail">
                    <Setter Property="Background" Value="Red"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>