在嵌套类中冒泡“INotifyPropertyChanged”

时间:2018-03-28 08:21:04

标签: c# nested inotifypropertychanged bindinglist propertychanged

我想建立一个我可以从中派生的类。该类应该建立一个逻辑,以便它从Propertys传递属性更改事件,而这些事件本身就是具有Propertys的类。

所以请查看代码。我的目标是在将catList[0].MyPerson.Name = "Peter";更改为Peter时从BindingList中抛出ListChanged事件。

我的问题是,当我在NestedPropertyHolder中时,我不知道如何获得实现NestedPropertyHolder的Class。换句话说,我如何得到我的继承人......希望你明白我想做什么。

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"));
            }
        }
    }

感谢您的帮助:)

更新:在同一主题上找到一个有趣的主题 When nesting properties that implement INotifyPropertyChanged must the parent object propogate changes?

1 个答案:

答案 0 :(得分:0)

您可以注册/订阅该类的PropertyChangedEvent并将其提升。

示例:

public class Category : NestedPropertyHolder, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    Person myPerson;

    public void SomethingChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("MyPerson"));        
    }

    public Person MyPerson
    {
        get
        {
            return myPerson;
        }

        set
        {
            myPerson = value;
            myPerson.PropertyChanged += SomethingChanged;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("MyPerson"));
        }
    }
}