Sebastian Busek
Sebastian Busek

Reputation: 1032

Refresh data in ListBox after add new one

Good morning,

I want to refresh data with Binding in listbox after then I add new one. I'm using MVVM design patter, so my ViewModel has property AllGroups and every group have property TotalCount and Name. Property AllGroups is ObservableCollection.

In listbox I show name of group and TotalCount. TotalCount has just getter realized by

public int TotalCount
{
  get
  {
    return Items.Count;
  }
}

and TotalCount isn't column.

I have group "All" with 2 items (TotalCount=2), but when I add new item to group and show list of groups I see "All" and TotalCount=2 but when I start app again I show "All" and "3".

So, how can I refresh data manually? Thank for help. :)

Upvotes: 0

Views: 622

Answers (2)

Claus Jørgensen
Claus Jørgensen

Reputation: 26336

Implement INotifyPropertyChanged, raise the NotifyPropertyChanged event for TotalCount when the collection changes, like this:

AllGroups.CollectionChanged += (s, e) => OnNotifyPropertyChanged("TotalCount");

Upvotes: 1

Haris Hasan
Haris Hasan

Reputation: 30107

You should implement INotifPropertyChanged in class that contains TotalCount

Once there is any change in Item collection raise the property changed event for TotalCount property to inform View that TotalCount has been changed so update View with latest value

Upvotes: 2

Related Questions