i want to build a class from which i can derive. The class should establish a logic so that it tunnels propertychanged events from Propertys who itself are classes which have Propertys.
So Please look at the Code. The goal is to throw the ListChanged Event from the BindingList when i change catList[0].MyPerson.Name = "Peter";
to Peter.
My Problem is that I dont know how i get the Class which implements NestedPropertyHolder when im in NestedPropertyHolder. In other words how i get my inheritor.... Hope you understand what im trying to do.
static void Main(string[] args)
{
BindingList<Category> catList = new BindingList<Category>();
catList.ListChanged += CatList_ListChanged;
Category cat = new Category();
Person pers = new Person();
pers.Name = "Rene";
cat.MyPerson = pers;
catList.Add(cat);
catList[0].MyPerson.Name = "Peter";
}
public class NestedPropertyHolder
{
public NestedPropertyHolder()
{
//List of Propertys of the class that is deriving from NestedPropertyHolder -> Should have "MyPerson" from Category
List<object> listOfPropertysImplementingINotifyPropertyChanged = new List<object>();
for(int i = 0; i < listOfPropertysImplementingINotifyPropertyChanged.Count; i++)
{
if(listOfPropertysImplementingINotifyPropertyChanged[i] is INotifyPropertyChanged)
{
(listOfPropertysImplementingINotifyPropertyChanged[i] as INotifyPropertyChanged).PropertyChanged += NestedPropertyHolder_PropertyChanged;
}
}
}
private void NestedPropertyHolder_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
//That class that is deriving from NestedPropertyHolder -> should be "Category"
object classThatDerivedFromThisClass = new object();
//classThatDerivedFromThisClass.PropertyChanged(sender, e.PropertyName);
}
}
}
public class Category : NestedPropertyHolder, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
Person myPerson = new Person();
public Person MyPerson
{
get
{
return myPerson;
}
set
{
myPerson = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("MyPerson"));
}
}
}
public class Person : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Name"));
}
}
}
Thanks for your help :)
Update: Found a interesting thread on the same topic When nesting properties that implement INotifyPropertyChanged must the parent object propogate changes?