简体   繁体   English

Listview ItemsSource-复杂-

[英]Listview ItemsSource - Complicated -

I am building a WPF application using C# in VS2010. 我正在VS2010中使用C#构建WPF应用程序。

I have a database -- lets call it mydatabase.mdf -- and I have attached it correctly to my project and built a DataContext . 我有一个数据库-称它为mydatabase.mdf并且我已经正确地将它附加到我的项目中并建立了一个DataContext

Now I have a ListView in my user interface that shows items from a specific table in this database, but the problem is that every time a new item is added to my database I want this ListView to update its items. 现在,我的用户界面中有一个ListView ,它显示该数据库中特定表中的项目,但问题是每次将新项目添加到数据库中时,我都希望此ListView更新其项目。

I have already tried: 我已经尝试过:

listView.items.refresh()

but it didn't work. 但这没用。

And I have tried something, namely applying a query on this database table every time a new item is added, and this query withdraws all the elements; 我尝试了一些方法,即每次添加新项目时都对该数据库表应用查询,并且该查询撤回所有元素; then I do: 然后我做:

listview.itemssource = myquery

but it didn't work either. 但它也不起作用。

please help me pleaseeeeeeeeeeeeeee 请帮助我pleaseeeeeeeeeeeeeee

The database can't notify the client when a new item is added. 添加新项目时,数据库无法通知客户端。

When its your client who adds the item to the database, an ObservableCollection will solve your problem. 当其客户将项目添加到数据库时,ObservableCollection将解决您的问题。 It implements INotifyCollectionChanged and notifies the UI about changes made to the collection (on the client). 它实现INotifyCollectionChanged并通知UI(在客户端上)对该集合所做的更改。

When other clients write to the database as well, your only chance is to poll the database periodically (requery the database) and apply the delta (changes) to your ObservableCollection manually, or completely rebind the datasource (sortings etc. might get lost then) or refresh the ItemsCollection in case you don't use an ObservableCollection or something implementing INotifyCollectionChanged. 当其他客户端也写入数据库时​​,唯一的机会是定期轮询数据库(重新查询数据库)并将增量(更改)手动应用于您的ObservableCollection,或者完全重新绑定数据源(排序等可能会丢失)或刷新ItemsCollection,以防您不使用ObservableCollection或实现INotifyCollectionChanged的东西。

Depending on your archicecture, a Remoting-Service could poll the database for all clients and then notify them all. 根据您的体系结构,Remoting-Service可以轮询数据库中的所有客户端,然后将它们全部通知。 That could be helpful if polling poses a bottleneck. 如果轮询造成瓶颈,那可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM