Reputation: 544
I have BindingList (BindingList (of Foo)) with several objects of this sample class:
Class Foo
Implements INotifyPropertyChanged
Private _name As String
Private _numer As Integer
Private _otherClass As OtherClass
Private _bindingListOfAnotherClass As BindingList (of AnotherClass)
End Class
Now I want to get informed, when any property of an item in this bindingList changes.
As you can see, I have the Interface implemented and I'm also throwing the events. The ListChanged Events are thrown for the primitive properties but not if an property in OtherClass or in AnotherClass changes (or if an item is added / removed in _bindingListOfAnotherclass).
I'm also throwing the PropertyChanged events in OtherClass and AnotherClass.
Any idea?
Upvotes: 1
Views: 1668
Reputation: 11252
Unfortunately there is no auto-wireup for your Foo class to know when OtherClass changes unless Foo specifically listens to the PropertyChanged event of OtherClass by subscribing to it with an event handler. In that event handler you would fire a property changed event on Foo itself saying that its OtherClass property has changed.
This can be a little tricky because if that property is exposed with a getter/setter of some kind, then you need to unsubscribe and resubscribe from the object as it changes.
In the case of your BindingList of AnotherClass, it's a bit different since BindingList does not implement INotifyPropertyChanged. It implements IBindingList which has a ListChanged event. So you would have to subscribe to that.
As long as AnotherClass implements INotifyPropertyChanged, then the binding list should throw its changed event to Foo, which can in turn throw its own PropertyChanged event.
EDIT:
Here is normally how I handle properties which can change that I want to always subscribe to with events:
private Object _myObject;
public Object MyObject
{
get
{
return _myObject;
}
set
{
if (_myObject != value)
{
if (_myObject != null)
_myObject.PropertyChanged -= MyDelegate;
_myObject = value;
if (_myObject != null)
_myObject.PropertyChanged += MyDelegate;
}
}
}
This way you can safely set it from null to a value and back to null again and it will manage event subscriptions for you.
Upvotes: 1