一、ObservableCollection和List的区别1)ObservableCollection比较简单,继承Collection、INotifyCollectionChanged、INotifyPropertyChangedCollection:提供泛型集合的基类。INotifyCollectionChanged:通知侦听器集合的动态更改,例如添加和删除项时或重置整个集合对象时。INotifyPropertyChanged:通知客户端属性值已更改。因此,ObservableCollection类的方法对数据的操作非常少,重点是在自身能力发生变化(无论是属性还是集合)时调用通知事件。(一般用来更新UI,当然也可以用来写其他东西,这个以后再写)2)List比较多,继承了IList、ICollection、IEnumerable、IList、ICollection、IEnumerable。IList:表示一组可以通过索引单独访问的对象。ICollection:定义操作泛型集合的方法。IEnumerable:公开支持对指定类型的集合进行简单迭代的枚举数。IList:表示可以通过索引单独访问的对象的非泛型集合。ICollection:定义所有非泛型集合的大小、枚举数和同步方法。IEnumerable:公开支持对非泛型集合进行简单迭代的枚举器。二、举例:1、举例1:MainWindow.xaml:person1=newList();privateObservableCollectionperson2=newObservableCollection();publicDemoTestDiff(){InitializeComponent();person1.Add(newPerson(){Name="张三"});person1.Add(newPerson(){Name="李四"});listbind.ItemsSource=person1;person2.Add(newPerson(){Name="张三"});person2.Add(newPerson(){Name="李四"});observbind.ItemsSource=person2;}privatevoidbutton1_Click(objectsender,RoutedEventArgse){person1.Add(newPerson(){Name="王五"});person2.Add(newPerson(){Name="WangWu"});}运行程序,点击button按钮,然后只添加ObservableCollection,表示当collection对象的collection发生变化时,只有ObservableCollection会发送通知更新UI,这只是其中之一2.例2以下方法可以更新ListView的UI:privateObservableCollection_previewList=newObservableCollection();//////预览信息列表///publicObservableCollectionPreviewList{get{return_previewList;}set{SetProperty(ref_previewList,value);}//set{_previewList=value;RaisePropertyChanged("PreviewList");}}3.ObservableCollection与List的相互转换https:///www.cnblogs.com/warioland/archive/2011/11/08/2240858.html从数据库中取出的collection是List类型,如果需要转换成ObservableCollection类型怎么办呢?方法如下:ttList=newList(tObjectStruct.ToList());ObservableCollectiontObjectStruct=newdatabaseretrieval:publicvoidAdvancedSearchFunc(AdvancedSearchNotificationadvancedSearchNotification){try{KrayMobileDREntitiesdataBase=newKrayMobileDREntities();//每次使用patientInfoHistoryModel.Clear()前必须清空;//首先从数据库中提取数据,放入集合中。ListpatientInfoList=dataBase.PatientInfo_Table.Where(u=>u.PatientKey.ToString().Equals(advancedSearchNotification.PatientInfo)||u.PatientID.ToString().Equals(advancedSearchNotification.StudyID)||u.PatientName.ToString().Equals(advancedSearchNotification.PatientName)).ToList();ListpatientStudyList=dataBase.PatientStudy_Table.Where(u=>u.PatientKey<10).ToList();//按条件查询集合Listlist=(frompIinpatientInfoListwhere(pI.PatientKey<1000)selectnewPatientInfoHistroryModel(){PatientInfo=pI.PatientKey.ToString(),StudyID=pI.PatientID.ToString(),PatientName=pI.PatientName.ToString(),PatientSex=pI.PatientSex.ToString(),PatientAge=pI.PatientAge.ToString(),PatientBrith=pI.PatientBirthDate.ToString(),PatientHeight=pI.PatientHeight.ToString(),PatientWeight=pI.PatientWeight.ToString(),RecordSource=pI.PatientSource.ToString(),//StudyTime=PS.StudyDatetime,//EquipmentType=PS.Study医师,//StudyPart=PS.StudyType,//SequenceAmount=PS.SeriesCount,StudyTime=pI.PatientAge.ToString(),EquipmentType=pI.PatientAge.ToString(),StudyPart=pI.HangFlag.ToString(),SequenceAmount=pI.HangFlag.ToString(),StudyStutas=pI.StudyCompleteFlag.ToString(),SuspendState=pI.HangFlag.ToString(),FilmPrint=pI.PrintFlag.ToString(),}).ToList();patientInfoHistoryModel=列表;数据库.Dispose();}catch(Exceptionex){MessageBox.Show("在病历信息表【高级查询】状态下,出现数据库错误错误信息:-------------"+ex.ToString());LogHelper.Error("OperateDataSheetViewModel.cs::AdvancedSearchFunc()AdvancedSearchFunc()Advancedqueryfailed--"+ex.Message);}4.总结1.ObservableCollection代表一个动态数据集合。添加项目时,移动此集合会在删除项目或刷新整个列表时提供通知。2.List表示可以通过索引访问的对象的强类型列表。提供搜索、排序和操作列表的方法。(大部分操作使用Linq,非常强大和方便。参考链接:https://blog.csdn.net/xpj8888/article/details/84782949关注二维码。获取请联系CSharp编程大全公众号转载本文。