如何更改ListView中单行的颜色?

时间:2016-04-22 14:35:40

标签: c# wpf entity-framework listview data-binding

我构建了与实体框架链接的WPF应用程序。

程序将用户详细信息添加到列表中,所有详细信息都存储在数据库中。因此,每当我关闭并重新打开应用程序时,之前添加的详细信息都会存在。

该应用程序具有计算平均模块等级的功能。

以下是我的代码片段:

 student.Average_Grade = "F" + new SolidColorBrush(Colors.Red);

因此,如果满足此条件,我还希望行变为红色或字母F变为红色。

我搜索了互联网,但我发现的只是如何在一个没有链接到数据库的简单WPF应用程序中更改它。

那么如果WPF应用程序链接到数据库,如何更改ListView中单行的颜色?

更新

我试过这段代码:

 <ListView Grid.Row="0" x:Name="studentListView" SelectionMode="Single" Margin="10,10,-10,10" ItemsSource="{Binding}">
            <ListView.View>
                <GridView>

                    <GridViewColumn x:Name="first_NameColumn" Width="80" Header="First Name">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Margin="-6,-1" Text="{Binding First_Name, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" IsReadOnly="True"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn x:Name="last_NameColumn" Width="80" Header="Last Name">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Margin="-6,-1" Text="{Binding Last_Name, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" IsReadOnly="True"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn x:Name="matric_NumberColumn" Width="80" Header="Matric Number">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Margin="-6,-1" Text="{Binding Matric_Number, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" IsReadOnly="True"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

                    <GridViewColumn x:Name="component1_GradeColumn" Width="80" Header="Component 1 Grade" >
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Margin="6,-1,-6,-1" Text="{Binding Component1_Grade, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" IsReadOnly="True"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn x:Name="component2_GradeColumn" Width="80" Header="Component 2 Grade">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Margin="-6,-1" Text="{Binding Component2_Grade, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" IsReadOnly="True"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn x:Name="component3_GradeColumn" Width="80" Header="Component 3 Grade">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Margin="-6,-1" Text="{Binding Component3_Grade, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" IsReadOnly="True"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn x:Name="average_GradeColumn" Width="80" Header="Average Grade">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Margin="-6,-1" Text="{Binding Average_Grade, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" IsReadOnly="True"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn x:Name="edit">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Content="Edit" Click="OnEdit"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn x:Name="delete">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Content="Delete" Click="OnDelete" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
            <ListView.ItemContainerStyle>
                <Style>
                    <Setter Property="Control.HorizontalContentAlignment" Value="Stretch"/>
                    <Setter Property="Control.VerticalContentAlignment" Value="Stretch"/>
                </Style>
            </ListView.ItemContainerStyle>
        </ListView>

但是,不是单行就会改变整个窗口的颜色

LISTVIEW XAML

{{1}}

1 个答案:

答案 0 :(得分:0)

您可以使用转换器。添加一个新类如下:

public class Converter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if(value.ToString() == "F")
            return new SolidColorBrush(Colors.Red);
        else
            return new SolidColorBrush(Colors.Black);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

在你的xaml中你添加了这个:

<Window.Resources> // Or UserControl.Resources
    <local:Converter x:Key="converter"/> 
</Window.Resources>

并替换ItemContainerStyle

<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <Setter Property="Control.HorizontalContentAlignment" Value="Stretch"/>
        <Setter Property="Control.VerticalContentAlignment" Value="Stretch"/>
        <Setter Property="Foreground" Value="{Binding Average_Grade, Converter={StaticResource converter}}" />
    </Style>
</ListView.ItemContainerStyle>