I have a ClassWrapper class and a BaseClassWrapper class. The BaseClassWrapper has an object of type ClassDTO and inside it has an ObservableCollection that is what I want to "observe". When I create an object of type "ClassWrapper" and add an item to the collection ClassWrapper.ClassDTO.MyCollection.Add(new OtherClass())
the observer does not work.
But if I create ClassDTO or an ObservableCollection inside ClassWrapper (not in BaseWrapper) it works perfectly. Why does this happen?
public class ClassWrapper : BaseClassWrapper
{
public ClassWrapper()
{
Observable.FromEventPattern<NotifyCollectionChangedEventHandler, NotifyCollectionChangedEventArgs>
(x => ClassDTO.MyCollection.CollectionChanged += x, x => ClassDTO.MyCollection.CollectionChanged -= x)
.Where(x => x.EventArgs.Action == NotifyCollectionChangedAction.Add ||
x.EventArgs.Action == NotifyCollectionChangedAction.Replace ||
x.EventArgs.Action == NotifyCollectionChangedAction.Remove)
.Throttle(TimeSpan.FromMilliseconds(250))
.Subscribe(x =>
{
RaisePropertyChanged(SomeProperty);
});
}
}
public abstract class BaseClassWrapper : ObservableObject // MVVM Light
{
public ClassDTO ClassDTO { get; set; } = new ClassDTO();
}
public class ClassDTO
{
public ObservableCollection<OtherClass> MyCollection { get; set; } = new ObservableCollection<OtherClass>();
}