简体   繁体   中英

Is it possible to NotifyChanged on an ObservableCollection property via CollectionChanged event?

Is it possible for me to have an ObservableCollection as a property in a class but subscribe to it's CollectionChanged event and call OnPropertyChanged on the ObservableCollection property as to update a UI binding to the collection?

Yes, but usually you wouldn't want to do something like that because of the overhead. If you use controls with an ItemsSource they know how to handle CollectionChanged without re-creating everything.

Instead of binding to the collection and using a converter you can expose a read-only ItemNames property. In the Items.CollectionChanged event handler you raise a PropertyChanged event for the ItemNames property

public class ViewModel
{

      public ViewModel()
      {
           Items = new ObservableCollection<Item>();
           Items.CollectionChanged += (o, e) => NotifyPropertyChanged("ItemNames");
      }
      public ObservableCollection<Item> Items { get; private set; }
      public string ItemNames { get { return String.Join(",", Items); } }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM