0

I built WPF application that is linked to Entity Framework.

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.

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

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

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 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>
user3438350
  • 11
  • 1
  • 6

1 Answers1

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:

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

And replace the 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>
Amine
  • 1,198
  • 11
  • 19