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

时间:2022-11-20 20:56:59

I built WPF application that is linked to Entity Framework.

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

The program adds user details in to a list and all details are stored in a database. So whenever I close and reopen the application, the details that have been added previously are there.

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

The application has a feature to calculate the average module grade.

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

Here is a fragment of my code:

这是我的代码片段:

if (averageResult < 7 && averageResult > 5)
{
    student.Average_Grade = "F";
}

So if this condition is met, I also want the row turn red or a letter F to turn red.

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

I searched the internet but only thing i find is how to change it in a simple WPF application not linked to database.

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

So how do you change the color of a single row in the ListView if WPF application is linked to a database?

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

UPDATE

UPDATE

Ive tried this code:

我试过这段代码:

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

But instead of a single row is changes the color of a whole window

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

LISTVIEW XAML

LISTVIEW XAML

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

1 个解决方案

#1


0  

You can use a converter. Add a new class as bellow:

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

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();
    }
}

And in your xaml you add this:

在你的xaml中你添加这个:

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

And replace the ItemContainerStyle

并替换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>

#1


0  

You can use a converter. Add a new class as bellow:

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

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();
    }
}

And in your xaml you add this:

在你的xaml中你添加这个:

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

And replace the ItemContainerStyle

并替换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>