更改关联属性(EntityCollection)不会增加PropertyChanged [英] Changing association property (EntityCollection) don't rise PropertyChanged
问题描述
我想通过Converter将只读DataGrid的某些列数据绑定到Entity的Association属性(将集合从此Association属性转换为字符串)。当我尝试从集合中添加/删除元素时,绑定不会触发。物业也已更改,请勿上升。
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);
这是contractPosition Entity(由EF4生成)的片段:
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;
}
}
为什么PropertyChanged不增加?如何实现绑定刷新?
Why PropertyChanged don't rise? How can I implement binding refresh?
推荐答案
有一些不同的事件需要听:
There are a few different events to listen to:
-
INotifyPropertyChanged.PropertyChanged
当 _contractToOrderLinks
更改。在示例代码中,值从不发生变化,事件被调用从不,并且事件从不被触发。
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.
INotifyCollectionChanged.CollectionChanged
在添加对象时触发
EntityCollection<> .EntityAdded
添加对象时触发。
EntityCollection< > .EntityRemoved
在删除对象时触发。 我不确定清除集合后是否会为每个实体触发该事件。
Fires when an object is removed. I am not sure if this fires for each entity when the collection is cleared.
我更喜欢使用 INotifyCollectionChanged.CollectionChanged
事件。但是, EntityCollection<>
显式实现该接口,因此必须首先强制转换。试试这个:
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);
这篇关于更改关联属性(EntityCollection)不会增加PropertyChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!