Search code examples
c#wpflistview

Why in wpf it's not showing the added items to the listView control?


Downloaded from : https://github.com/eaplatt3/Lab-ListView

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Lab_ListView
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void AddBtn_Click(object sender, RoutedEventArgs e)
        { 
            for(int ii = 0; ii < 100; ii++)
            {
                VideoGameListView.Items.Add("Hello " + ii.ToString());
            }

            /*string tempPrice;

            VideoGame vg = new VideoGame();

            vg.GameName = GameNameTextBox.Text;
            vg.Rating = RatingTextBox.Text;
            tempPrice = PriceTextBox.Text;
            if (!double.TryParse(tempPrice, out double i)) { }
            vg.Price = i;

            VideoGameListView.Items.Add(vg);

            GameNameTextBox.Text = " ";
            RatingTextBox.Text = " ";
            PriceTextBox.Text = " ";

            GameNameTextBox.Focus();*/
        }

    }
}

if i'm using the code marked with /* and */ it will add items to the listView control VideoGameListView without a problem but when i'm using the loop to add items it looks like it's adding the items the scroll in the listView is moving down when running the application but it's not showing anything.

wpf listview

The xaml control code :

<Window x:Class="Lab_ListView.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Lab_ListView"
        mc:Ignorable="d"
        Title="Video Game List" Height="450" Width="800">
    <Grid Margin="0,0,10,10">
        <Label Content="Game Name" HorizontalAlignment="Left" Margin="40,59,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="GameNameTextBox" HorizontalAlignment="Left" Height="23" Margin="121,62,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="366"/>
        <Button x:Name="AddBtn" Content="Add" HorizontalAlignment="Left" Margin="121,151,0,0" VerticalAlignment="Top" Width="75" Click="AddBtn_Click"/>
        <ListView x:Name="VideoGameListView" HorizontalAlignment="Left" Height="166" Margin="40,203,0,0" VerticalAlignment="Top" Width="447">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Game Name" DisplayMemberBinding="{Binding GameName}"/>
                    <GridViewColumn Header="Rating" DisplayMemberBinding="{Binding Rating}"/>
                    <GridViewColumn Header="Price" DisplayMemberBinding="{Binding Price}"/>
                </GridView>
            </ListView.View>
        </ListView>
        <Label Content="Rating" HorizontalAlignment="Left" Margin="40,90,0,0" VerticalAlignment="Top"/>
        <Label Content="Price" HorizontalAlignment="Left" Margin="40,121,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="PriceTextBox" HorizontalAlignment="Left" Height="23" Margin="121,123,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="366"/>
        <TextBox x:Name="RatingTextBox" HorizontalAlignment="Left" Height="23" Margin="121,92,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="366"/>

    </Grid>
</Window>

Solution

  • You have specified in your XAML that your columns bind to properties of the items named GameName, Rating and Price. When you add VideoGame objects to the list, those properties of those objects are displayed in those columns. If you simply add a string then there are no such properties, so those columns show nothing. If you bind your columns to specific properties then those properties have to exist in the items you add or there's nothing to display in those columns for those items.