更新 WPF/WCF 应用程序中的多个数据绑定用户控件
我的大脑一直在试图解决我遇到的问题,一些帮助会大有帮助。
我有一个应用程序,它从 WCF 服务下载数据结构,并将数据存储在可观察的集合属性中。我有一个包含网格的用户控件,并绑定在用户控件初始化程序上或通过 x:Static
数据绑定。用户控件或可观察集合属性可以在同一台计算机上多次访问。
如果我打开用户控件的多个实例并使用网格修改记录,我会将更改发送到 WCF 服务,该服务修改 SQL 数据库,向修改后的对象添加一些位,然后将其传递回 WPF(可能在将来)通过回发到其他客户端应用程序)应用程序和网格更新。然而,它仅在一个特定的用户控件网格上更新。
如果我强制其他用户控件网格重新绑定,则会反映数据更改,这不是我想要的。因此,我决定使用 LINQ 在可观察集合中查找要修改的对象,然后使用 IList.IndexOf
将对象添加到集合中它在集合中的位置,然后删除旧的对象目的。执行此方法似乎确实效果很好,但我一直在读到有更好的方法来实现此目的。
任何人都可以帮助阐明我想要实现的目标,或者是我已经完成通知所有绑定网格进行更改的正确/唯一方式,我的所有属性都在这些值上实现了 inotifypropertychanged 。
提前致谢
My brain in stuck trying to resolve an issue i have encounted and some assistance would go a long way.
I have an application which downloads a data structure from a WCF service and the data is stored in an observable colletion property. I have a user control that contains a grid and is bound either on the user control initilizer or throught an x:Static
databinding. The user control or the observable collection property can be accessed on the same machine more then once.
If I have multiple instance of the user control open and modify a record using the grid, I send the change to the WCF service which modifies the SQL database adds a few bits to the modified object and is passes it back to WPF (perhaps in future to other client apps through postbacks) app and the grid updates. However it only updates on that one particular usercontrols grid.
If I force the other user controls grid to rebind then the data change is reflected which is not what I want. So I decided to use LINQ to find the object to be modified in the observable collection, add then inserted the object to the collection using the IList.IndexOf
to the place it was in the collection and remove the old object. Doing this method does seem to work fine but I have been reading that there is a better way of acheiving this.
Can anyone help shed some light on what I am trying to acheive or is the way that I have accomplished notifying all bound grids that a change is made the right/only of doing this all my properties implement inotifypropertychanged on there values.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该集合由 wcf 服务创建并移交给静态类以提供对其的访问。网格绑定到这个静态类。
根据您的评论,我必须重写我的答案:
您有一个网格绑定到的静态属性。如果您为此属性分配一个新集合,则网格仍绑定到旧集合,因此它们不会更新,因为从它们的角度来看没有任何变化。
如果您更改集合中的一项,则您更改的是该项,而不是集合。所以绑定的网格不会更新,因为从它的角度来看没有任何改变。
当您想要更新网格时,您必须在添加所有集合项时注册 ChangedEvent-Handler。每次由于您更改了集合中的一项而调用此处理程序时,您都必须触发网格进行更新。
这通常是通过包装集合(模型)的视图模型来完成的。然后网格绑定到该视图模型,视图模型负责通知网格(视图)任何更改。查看 MVVM 设计模式,它将为您提供解决问题的标准化方法。
The collection is created by the wcf service and handed over to a static class to provide access to it. The grids are bound to this static class.
According to your comments I have to rewrite my anser:
You have a static property the grids are bound to. If you assign a new collection to this property, the grids are still bound to the old one and therefore they do not update because from their point of view nothing changed.
If you change one item inside the the collection you changed the item and not the collection. So the bound grids wont update because fromt theit point of view nothing changed.
When you want to have the grids updated you have to register a ChangedEvent-Handler on all your collection items when they are added. Every time this handler is invoke because you changed one item of the collection you have to trigger the grids to update.
This is usually done by a view model that wraps the collection (the model). The grids then bind to this view model, which takes care of informing the grids (the views) of any change. Have a look at the MVVM design pattern which will provide you a standardized way to solve your problem.