ObservableCollection 更新后,Datagrid 中的项目不会在所有客户端上更新
我正在使用 SL4、MVVM Toolkit、Entity Framework 4 和 Entity Framework 4。 WCF RIA 服务。我有一个网格,其数据源是 ObservableCollection
。 Ticket OC 由 RIA 服务填充:
private void LoadTickets()
{
IsBusy = true;
_Context.Load(_Context.GetOpenTicketsForUserQuery(Session.UserId), GetTicketsCallback, null);
}
private void GetTicketsCallback(LoadOperation<Ticket> lo)
{
ListOfTickets = (ObservableCollection<Ticket>)lo.Entities;
}
当我向 OC 添加新的 Ticket 对象时,一旦我刷新网格,网格就会在所有客户端上显示新项目(每 30 秒我通过调用 刷新每个客户端的网格LoadTickets()
)。如果我也从网格中删除一个项目,这也有效。但是,当我更新 Ticket 对象中的属性并保存它(在 DataContext 上调用 SubmitChanges() )时,即使刷新网格后,此更改也不会显示在其他客户端上。我必须刷新浏览器中的整个页面才能看到更改。我在这里读过很多类似的问题,他们都说你必须在对象属性以及 ObservableCollection 上实现 INotifyPropertyChanged。
但是,据我所知,实体框架会自动执行此操作!?我可以在 Model Designer.cs 中看到我的所有属性,并且在设置属性时它们都会引发 PropertyChanged 事件。我只是想知道这是否与我使用 RIA 服务有关?我尝试将 Ticket 对象中的属性添加到 RIA 服务元数据中,并从此处调用 RaisePropertyChanged() 事件,但它也不起作用。
[MetadataTypeAttribute(typeof(TicketMetadata))]
public partial class Ticket
{
internal sealed class TicketMetadata : NotifiableObject
{
[Required]
[StringLength(255, MinimumLength=15)]
public string TicketSummary
{
get { return TicketSummary; }
set
{
TicketSummary = value;
RaisePropertyChanged("TicketSummary");
}
}
}
}
谁能帮我解释一下吗?这让我发疯!我是 Silverlight 开发的新手,所以如果这是一个愚蠢的问题,我深表歉意:)
I am using SL4, MVVM Toolkit, Entity Framework 4 & WCF RIA Services. I have a grid whose DataSource is an ObservableCollection<Ticket>
. The Ticket OC is populated by a RIA Service:
private void LoadTickets()
{
IsBusy = true;
_Context.Load(_Context.GetOpenTicketsForUserQuery(Session.UserId), GetTicketsCallback, null);
}
private void GetTicketsCallback(LoadOperation<Ticket> lo)
{
ListOfTickets = (ObservableCollection<Ticket>)lo.Entities;
}
When I add a new Ticket object to the OC the grid shows the new item on all clients once I refreh the grid ( Every 30 seconds I refresh the grid for each client by calling LoadTickets()
). This works if I remove an item from the grid as well. However, when I update a property in the Ticket object and Save it ( Call SubmitChanges() on the DataContext ) this change does NOT show on the other clients even after I refresh the grid. I have to refresh the whole page in the browser to see the changes. I have read many similar questions on here and they all say you must implement INotifyPropertyChanged on your object properties as well as the ObservableCollection.
But, AFAIK the Entity Framework does this automatically !? I can see all my properties in the Model Designer.cs and they all raise a PropertyChanged event when the property is set. I'm just wondering if this has something do with the fact I am using RIA Services ? I have tried adding a property from my Ticket object to my RIA Service Metadata and calling the RaisePropertyChanged() event from here but it didn't work either.
[MetadataTypeAttribute(typeof(TicketMetadata))]
public partial class Ticket
{
internal sealed class TicketMetadata : NotifiableObject
{
[Required]
[StringLength(255, MinimumLength=15)]
public string TicketSummary
{
get { return TicketSummary; }
set
{
TicketSummary = value;
RaisePropertyChanged("TicketSummary");
}
}
}
}
Can anyone shed any light on this for me? It's driving me crazy!! I'm new to Silverlight Development so apologies if this is a stupid question :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧——我已经查到真相了。在 Load() 事件中,我需要设置
LoadBehaviour
这解决了我的问题。
Ok - I got to the bottom of this. In the Load() event I need to set the
LoadBehaviour
This has resolved my issue.