WPF ListBox not showing MySQL data after converting from console app (video game list)

WPF ListBox not showing MySQL data after converting from console app (video game list)
typescript
Ethan Jackson

I created a console application that connects to a MySQL database and loads a list of video games into memory. It works fine and prints out the games correctly. Here’s the importnant part:

Program class:

internal class Program { static void Main(string[] args) { Statistics statistics = new Statistics(); Console.ReadKey(); } }

Class:

public Statistics() { Initialize(); ShowGameCount(); Oldest(); } public void Initialize() { string connStr = "server=localhost;user=root;database=gamesdb;port=3306;password="; MySqlConnection conn = new MySqlConnection(connStr); try { conn.Open(); string sql = "SELECT id, title, platform, release_year FROM videogames"; MySqlCommand cmd = new MySqlCommand(sql, conn); MySqlDataReader rdr = cmd.ExecuteReader(); games = new List<VideoGame>(); while (rdr.Read()) { var game = new VideoGame(rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2), rdr.GetInt32(3)); games.Add(game); } rdr.Close(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); Environment.Exit(1); } conn.Close(); }

Now I tried to switch to WPF to display the list visually. I created a method ReturnGames() and set it as the ItemsSource of a ListBox, but the list is empty. Here's the WPF part:

private Statistics statistics = new Statistics(); public MainWindow() { InitializeComponent(); gameList.ItemsSource = statistics.ReturnGames().ToString(); }

XAML:

<ListBox x:Name="gameList" />

What should I do to fill the data list in wpf? I used this website to get the basics.

Answer

WPF ListBox is indeed showing the list, but probably displaying nothing because it doesn't know how to render each VideoGame object. You need to either:

  • Override the ToString() method in your VideoGame class.

  • Use a DataTemplate in XAML for custom display.

VideoGame.cs:

public class VideoGame { public int Id { get; set; } public string Title { get; set; } public string Platform { get; set; } public int ReleaseYear { get; set; } public VideoGame(int id, string title, string platform, int year) { Id = id; Title = title; Platform = platform; ReleaseYear = year; } public override string ToString() { return $"{Title} ({Platform}, {ReleaseYear})"; } }

Statistics.cs:

private List<VideoGame> games; public Statistics() { Initialize(); } public void Initialize() { string connStr = "server=localhost;user=root;database=gamesdb;port=3306;password="; using var conn = new MySqlConnection(connStr); try { conn.Open(); string sql = "SELECT id, title, platform, release_year FROM videogames"; MySqlCommand cmd = new MySqlCommand(sql, conn); MySqlDataReader rdr = cmd.ExecuteReader(); games = new List<VideoGame>(); while (rdr.Read()) { var game = new VideoGame(rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2), rdr.GetInt32(3)); games.Add(game); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Database Error", MessageBoxButton.OK, MessageBoxImage.Error); Application.Current.Shutdown(); } } public List<VideoGame> ReturnGames() => games;

MainWindow.xaml:

<Window x:Class="GameApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Title="Video Game List" Height="400" Width="500"> <Grid Margin="10"> <ListBox x:Name="gameList" /> </Grid> </Window>

MainWindow.xaml.cs:

private Statistics statistics = new Statistics(); public MainWindow() { InitializeComponent(); gameList.ItemsSource = statistics.ReturnGames(); }

Now your WPF application will load and display the list of video games using their overridden ToString() method.

Related Articles