The Problem
I'm new to WPF and trying to learn. I have a basic ListView showing info about people such as Name, Age and Grade.
I want the Grade result text to be green if enum is "Pass" and red if "Fail", otherwise the text colour isn't changed.
What I tried
I know you can hard code all text in a column to be green, red etc with Foreground="" but this wouldn't work. I tried implementing a function that checks if each enum in the list equals Pass etc but I couldn't get it and I'm quite stuck here.
XAML
<Grid Margin="10">
<ListView Name="lvUsers">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}" />
<GridViewColumn Header="Grade" Width="100" DisplayMemberBinding="{Binding Grade}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
CS
public partial class MainWindow : Window
{
public class User
{
public string Name { get; set; }
public int Age { get; set; }
public Grade Grade { get; set; }
}
public MainWindow()
{
InitializeComponent();
List<User> items = new List<User>();
items.Add(new User() { Name = "John Doe", Age = 42, Grade = Grade.fail });
items.Add(new User() { Name = "Jane Doe", Age = 39, Grade = Grade.pass });
items.Add(new User() { Name = "Sammy Doe", Age = 13, Grade = Grade.fail });
lvUsers.ItemsSource = items;
}
public enum Grade
{
none = 0,
pass = 1,
fail = 2
};
}
Expected result
I'm don't want to have all text in Grade column to be green/red. And I don't want to add a Colour property inside the User Class.
When the enum value is "Pass" for the User, the "Pass" text in the Grade column will be green. When it's "Fail", the text will be red. Otherwise, text colour is not changed.
Any help is much appreciated, because I'm quite stuck here.