更改关联属性(EntityCollection)不会引发PropertyChanged
我想通过转换器将只读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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有几个不同的事件可供监听:
INotifyPropertyChanged.PropertyChanged
当
_contractToOrderLinks
的值更改时触发。在您的示例代码中,值从不更改,事件从不调用,并且事件从不触发。INotifyCollectionChanged.CollectionChanged
添加对象、删除对象以及清除集合时触发。
EntityCollection<>.EntityAdded
添加对象时触发。
EntityCollection<>.EntityRemoved
当对象被移除时触发。 我不确定在清除集合时是否会为每个实体触发此操作。
我更喜欢使用
INotifyCollectionChanged.CollectionChanged
事件。但是,EntityCollection<>
显式实现该接口,因此您必须首先对其进行强制转换。试试这个:There are a few different events to listen to:
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.INotifyCollectionChanged.CollectionChanged
Fires when an object is added, an object is removed and, when the collection is cleared.
EntityCollection<>.EntityAdded
Fires when an object is added.
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: