Tercalen
Tercalen

Reputation: 13

ListView not updating with bound List

I am trying to implement a simple application : I have a list of objects in my ViewModel i want to display in a ListView with a Datatemplate. Everything works well with binding until I add a button that adds an element in the list. When I do that, the ListView doesn't update. However, if I try to scroll down on the list, the whole program stops and the following message appears :

"The application is in break mode, Your app has entered a break state, but there is no code to show because all threads were executing external code (typically system or framework code)."

Here is the code :
Model class:

namespace WpfTest
{
    class Model
    {
        public string Value1 { get; set; }
        public int Value2 { get; set; }
        public bool Value3 { get; set; }

        public Model()
        {
            Value1 = "abc";
            Value2 = 3;
            Value3 = true;
        }
    }
}

ViewModel class :

namespace WpfTest
{
    class ViewModel : INotifyPropertyChanged
    {
        public List<Model> _model;
        private ICommand _addElement;

        public ViewModel()
        {
            _addElement = new RelayCommand(Add);
            _model = new List<Model>()
            {
                new Model(),
                new Model()
            };
        }

        public List<Model> ModelList
        {
            get => _model;
            set => _model = value;
        }

        public ICommand AddElement
        {
            get => _addElement;
            set => _addElement = value;
        }

        public void Add(object o)
        {
            _model.Add(new Model());
            OnPropertyChanged("ModelList");
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string message)
        {
            if(PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(message));
            }
        }
    }
}

XAML view :

<Window x:Class="WpfTest.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:WpfTest"
        xmlns:Mod="clr-namespace:WpfTest" 
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <DataTemplate DataType="{x:Type Mod:Model}">
            <StackPanel Orientation="Horizontal">
                <Label Content="{Binding Value1}"></Label>
                <Label Content="{Binding Value2}"></Label>
                <Label Content="{Binding Value3}"></Label>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
    <StackPanel>
        <Button Command="{Binding AddElement}">Add</Button>
        <ListView ItemsSource="{Binding ModelList}"></ListView>
    </StackPanel>
</Window>

I am using the latest version of Visual Studio Community 2017

Thanks in advance for the help

Upvotes: 1

Views: 646

Answers (1)

Peregrine
Peregrine

Reputation: 4546

Replace List<Model> with ObservableCollection<Model>. That will correctly notify any bound UI elements when the content of the list is changed.

If you want updates to individual model objects to be relected in the UI then the model class has to implement INotifyPropertyChanged, and the property definitions updated accordingly.

Upvotes: 3

Related Questions