0

Is it possible to create an AttachedEvent for an ItemsControl, which will be fired when the ItemsSource or the Items have changed?

Something that will be used like this:

<ItemsControl ItemsSource="{Binding ItemList}"
              ext:ItemsControlHelper.ItemsChanged="OnItemsChanged">
    ...
</ItemsControl>

I think it should be possible to do this with the DependencyPropertyDescriptor but I just can't wrap my head around the exact implementation with an AttachedEvent.

Community
  • 1
  • 1
Staeff
  • 4,994
  • 6
  • 34
  • 58
  • your ItemsSource must be drived from INotifyCollectionChanged or INotifyPropertyChanged. Than you can easily do this. – D J Apr 17 '14 at 08:59
  • I know I can do this in the code behind, with just adding an Event listener for NotifyPropertyChanged to the DataContext (ViewModel), but it's a not very generic solution, as I have to know what property to look out for, which means I have to look out for a hardcoded string, which can lead to propblems if the property is changed in the future. I think it's simply a cleaner solution to know as little as possible about my ViewModel. – Staeff Apr 17 '14 at 09:04
  • ViewModel knowing as little as it can about the View? Good... View knowing as little as it can about the ViewModel? Not really viable in MVVM. – Doug Apr 18 '14 at 07:20

0 Answers0