Search code examples
silverlightsilverlight-4.0entity-framework-4wcf-ria-services

Changing association property (EntityCollection) don't rise PropertyChanged


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?


Solution

  • 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);