0

I have a ListView like this:

        <ListView x:Name="ItemsListView"
                ItemsSource="{Binding Items}"
                VerticalOptions="FillAndExpand"
                HasUnevenRows="true"
                RefreshCommand="{Binding LoadItemsCommand}"
                IsPullToRefreshEnabled="False"
                IsRefreshing="{Binding IsBusy, Mode=OneWay}"
                CachingStrategy="RecycleElement"
                ItemSelected="OnItemSelected">

and my ViewModel has this definitions and my Model class looks like this:

    public ObservableCollection<IncomingJobItem> Items { get; set; }
    public class IncomingJobItem
    {
        public int Index { get; set; }

        [JsonProperty("id")]
        public string Id { get; set; }

        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("amount")]
        public int Amount { get; set; }
    }

Now when I update ViewModelObject.Items and call NotifyPropertyChanged the already existing items in the ListView do not get updated.

Example:

// change already existing item
ViewModelObject.Items[0].Amount = 3;

// notify item in Items has changed
ViewModelObject.NotifyPropertyChanged("Items");

// nothing happens, item 0 in the list stays with the old amount...
// expected is to update the item in the list to display 3

But when I create and add a new element to ViewModelObject.Items, the ListView adds the new Item but does not update the already existing item. What can I do?

Also ViewModelObject.NotifyPropertyChanged("Items.Amount"); does nothing.

Milos
  • 330
  • 1
  • 9
  • 1
    you need to call NotifyPropertyChanged on the individual property that has been updated, not on the collection – Jason Aug 23 '20 at 23:11
  • @Jason I tried this but it does not work (NotifyPropertyChanged with Amount and Items.Amount does nothing) – Milos Aug 23 '20 at 23:18
  • 1
    IncomingJobItem is NOT implementing INPC. You actually have to implement the interface on the class, and call the PropertyChanged method from the property's setter. You can't just call it from some random class and expect it to work. There are many, many articles that explain how to do this. Like https://stackoverflow.com/questions/1315621/implementing-inotifypropertychanged-does-a-better-way-exist – Jason Aug 23 '20 at 23:28
  • @Jason No it did not... But thank you very much, your hint got me to the solution. This concept is new to me so please excuse my noob mistake. – Milos Aug 23 '20 at 23:33

1 Answers1

0

Thanks to @Jason in the comments I got the answer.

My model class was not implementing INotifyPropertyChanged.

An implementation can be as simple as this:

    public class IncomingJobItem : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged([CallerMemberName] string name = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }

        public int Index { get; set; }

        [JsonProperty("id")]
        public string Id { get; set; }

        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("amount")]
        public int Amount { get; set; }
    }

And updating can be done like this:

incomingJobItemObj.OnPropertyChanged("Amount"); // or any other property name
Milos
  • 330
  • 1
  • 9
  • again, the PropertyChanged call should be **inside the setter**. While what you're doing may work, it's not a good design. – Jason Aug 23 '20 at 23:52