开发者

Update several data bound user controls in a WPF/WCF Application

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 b开发者_JAVA百科ut 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜