更新ItemsSource Linq到SQL WPF [英] Update ItemsSource Linq to SQL WPF

查看:81
本文介绍了更新ItemsSource Linq到SQL WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPF应用程序中有一个树状视图,该树状视图使用Linq to SQL和数据提供程序链接到SQL Express数据库.简单且有效.

treeMovies.ItemsSource = dataProvider.GetCollection_Movies();

我的问题是,在将新记录添加到数据库后,我无法找到任何方法来刷新树视图,而不会杀死itemsource并重新添加它.有没有一种方法可以刷新树视图以显示新记录,而无需重新加载整个数据库?
谢谢
Scott

I have a treeview in a WPF application that links to a SQL Express DB using Linq to SQL and a data provider. Simple and it works.

treeMovies.ItemsSource = dataProvider.GetCollection_Movies();

My problem is that I can find no way to refresh the treeview after a new record is added to the DB with out killing the itemsource and re-adding it. Is there a way to refresh the treeview to show the new records with out reloading the entire DB?
thanks
Scott

推荐答案

使用ObservableCollection(OC)作为ItemsSource.当您将某些内容添加到数据库时,请将其添加到OC.从数据库中删除时,请从OC中删除.在数据库中修改记录时,请替换或修改OC中的项目.如果您修改项目(例如,更改项目的属性),那么您将要确保该项目也是可观察的(一种实现方法是实现INotifyPropertyChanged接口).
Use an ObservableCollection (OC) as your ItemsSource. When you add something to the DB, add it to the OC. When you delete from the DB, delete from the OC. When you modify a record in the DB, either replace or modify the item in the OC. If you modify the item (e.g., change properties on it), then you''ll want to make sure that item is observable as well (one way to do this is to implement the INotifyPropertyChanged interface).


这篇关于更新ItemsSource Linq到SQL WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆