更改关联属性(EntityCollection)不会引发PropertyChanged

发布于 2024-11-14 12:42:33 字数 1006 浏览 2 评论 0原文

我想通过转换器将只读DataGrid的一些列数据绑定到实体的关联属性(将集合从此关联属性转换为字符串)。当我尝试从集合中添加/删除元素时,绑定不会触发。属性也变了,不升了。

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

contractPosition.ContractToOrderLinks.Remove(link);

这是 ContractPosition 实体的片段(由 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;
            }
        }

为什么 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天赋异禀 2024-11-21 12:42:33

有几个不同的事件可供监听:

  1. INotifyPropertyChanged.PropertyChanged

    _contractToOrderLinks 的值更改时触发。在您的示例代码中,值从不更改,事件从不调用,并且事件从不触发。

  2. INotifyCollectionChanged.CollectionChanged

    添加对象、删除对象以及清除集合时触发。

  3. EntityCollection<>.EntityAdded

    添加对象时触发。

  4. EntityCollection<>.EntityRemoved

    当对象被移除时触发。 我不确定在清除集合时是否会为每个实体触发此操作。

我更喜欢使用 INotifyCollectionChanged.CollectionChanged 事件。但是,EntityCollection<> 显式实现该接口,因此您必须首先对其进行强制转换。试试这个:

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

contractPosition.ContractToOrderLinks.Remove(link);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文