Changing association property (EntityCollection) don't rise PropertyChanged

我只是一个虾纸丫 提交于 2019-12-24 00:07:41

问题


I want to bind some column data of readonly DataGrid to Association property of Entity through Converter (convert collection from this association property to string). When I try to add/remove elements from collection, binding don't fire. PropertyChanged also, don't rising.

contractPosition.PropertyChanged += (s, e2) =>
    {
           a = 0;//don't fire
    };

contractPosition.ContractToOrderLinks.Remove(link);

Here is the fragment of contractPosition Entity (generated by EF4):

[Association("ContractPosition_ContractToOrderLink", "PositionId", "ContractPositionId")]
        [XmlIgnore()]
        public EntityCollection<ContractToOrderLink> ContractToOrderLinks
        {
            get
            {
                if ((this._contractToOrderLinks == null))
                {
                    this._contractToOrderLinks = new EntityCollection<ContractToOrderLink>(this, "ContractToOrderLinks", this.FilterContractToOrderLinks, this.AttachContractToOrderLinks, this.DetachContractToOrderLinks);
                }
                return this._contractToOrderLinks;
            }
        }

Why PropertyChanged don't rise? How can I implement binding refresh?


回答1:


There are a few different events to listen to:

  1. INotifyPropertyChanged.PropertyChanged

    Fires when the value of _contractToOrderLinks changes. In your sample code, the value never changes, the event is never called, and the event never fires.

  2. INotifyCollectionChanged.CollectionChanged

    Fires when an object is added, an object is removed and, when the collection is cleared.

  3. EntityCollection<>.EntityAdded

    Fires when an object is added.

  4. EntityCollection<>.EntityRemoved

    Fires when an object is removed. I am not sure if this fires for each entity when the collection is cleared.

I prefer to use the INotifyCollectionChanged.CollectionChanged event. However, EntityCollection<> explicitly implements the interface so you must cast it first. Try this:

((INotifyCollectionChanged)contractPosition.ContractToOrderLinks).CollectionChanged += (s, e) =>
    {
           a = 0; //does fire
    };

contractPosition.ContractToOrderLinks.Remove(link);


来源:https://stackoverflow.com/questions/6264979/changing-association-property-entitycollection-dont-rise-propertychanged

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!