在嵌套类中冒泡"INotifyPropertyChanged"



我想构建一个可以得出的类。该类应建立逻辑,以便它隧道从属性中的属性划分事件,这些事件本身就是具有属性的类。

所以请查看代码。目的是将catList[0].MyPerson.Name = "Peter";更改为Peter。

我的问题是,我不知道当我在nestedpropertyholder中时,我如何获得实现NestedPropertyholder的班级。换句话说,我如何获得继承者。...希望您了解我要做什么。

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

感谢您的帮助:(

更新:在同一主题上找到一个有趣的线程当实现InotifyPropertychanged的嵌套属性时,父对象必须偏向更改?

您可以注册/订阅类的属性Changedevent并提高。

示例:

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

相关内容

  • 没有找到相关文章

最新更新