分享

C# ObservableCollection和List的区别

 ontheroad96j47 2021-11-08

一、ObservableCollection和List的区别

1)ObservableCollection比较简单,继承了Collection, INotifyCollectionChanged, INotifyPropertyChanged

  Collection:为泛型集合提供基类。

  INotifyCollectionChanged:将集合的动态更改通知给侦听器,例如,何时添加和移除项或者重置整个集合对象。

  INotifyPropertyChanged:向客户端发出某一属性值已更改的通知。

  所以再ObservableCollection这个类的方法,对数据的操作很少,重点放在了当自己本事变化的时候(不管是属性,还是集合)会调用发出通知的事件。(一般用于更新UI,

  当然也可以用于写其他的事情。这个以后会写)

2)List就比较多了,继承了IList, ICollection, IEnumerable, IList, ICollection, IEnumerable。

  IList:表示可按照索引单独访问的一组对象。

  ICollection:定义操作泛型集合的方法。

  IEnumerable:公开枚举器,该枚举器支持在指定类型的集合上进行简单迭代。

  IList:表示可按照索引单独访问的对象的非泛型集合。

  ICollection:定义所有非泛型集合的大小、枚举器和同步方法。

  IEnumerable:公开枚举器,该枚举器支持在非泛型集合上进行简单迭代。

二、举例:

1、举例1:

MainWindow.xaml:

  1. <ListBox x:Name="listbind" Height="61" HorizontalAlignment="Left" Margin="146,12,0,0" VerticalAlignment="Top" Width="120" >

  2. <ListBox.ItemTemplate>

  3. <DataTemplate>

  4. <TextBlock Text="{Binding Name}" />

  5. </DataTemplate>

  6. </ListBox.ItemTemplate>

  7. </ListBox>

  8. <ListBox x:Name="observbind" Height="74" HorizontalAlignment="Left" Margin="146,111,0,0" VerticalAlignment="Top" Width="120" >

  9. <ListBox.ItemTemplate>

  10. <DataTemplate>

  11. <TextBlock Text="{Binding Name}" />

  12. </DataTemplate>

  13. </ListBox.ItemTemplate>

  14. </ListBox>

  15. <TextBlock Height="23" HorizontalAlignment="Left" Margin="38,58,0,0" Name="textBlock1" Text="List绑定数据" VerticalAlignment="Top" />

  16. <TextBlock Height="44" HorizontalAlignment="Left" Margin="12,125,0,0" Name="textBlock2" Text="ObservableCollection绑定数据" VerticalAlignment="Top" Width="112" />

  17. <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="77,211,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />

xaml页面很简单,托2个listbox分别用来绑定ObservableCollection和List

Person.cs:

  1. 1 public class Person

  2. 2 {

  3. 3 public string Name { get; set; }

  4. 4 }

MainWindow.xaml.cs:

  1. private List<Person> person1 = new List<Person>();

  2. private ObservableCollection<Person> person2 = new ObservableCollection<Person>();

  3. public DemoTestDiff()

  4. {

  5. InitializeComponent();

  6. person1.Add(new Person() { Name = "张三" });

  7. person1.Add(new Person() { Name = "李四" });

  8. listbind.ItemsSource = person1;

  9. person2.Add(new Person() { Name = "张三" });

  10. person2.Add(new Person() { Name = "李四" });

  11. observbind.ItemsSource = person2;

  12. }

  13. private void button1_Click(object sender, RoutedEventArgs e)

  14. {

  15. person1.Add(new Person() { Name = "王五" });

  16. person2.Add(new Person() { Name = "王五" });

  17. }

运行程序点击button按钮,然后只有ObservableCollection的有添加。

表示当集合对象的集合改变时,只有ObservableCollection会发出通知更新UI。

这只是他们两个区别之一。 

2、举例2

以下方法可以更新ListView的UI:

  1. private ObservableCollection<PreviewListModel> _previewList = new ObservableCollection<PreviewListModel>();

  2. /// <summary>

  3. /// 预览信息列表

  4. /// </summary>

  5. public ObservableCollection<PreviewListModel> PreviewList

  6. {

  7. get { return _previewList; }

  8. set { SetProperty(ref _previewList, value); }

  9. //set { _previewList = value; RaisePropertyChanged("PreviewList"); }

  10. }

三、 ObservableCollection和List的互相转换

https://www.cnblogs.com/warioland/archive/2011/11/08/2240858.html

从数据库检索的出来的集合是List<T>类型,我们需要把它转成ObservableCollection类型怎么办?如下方法:

  1. T tList = new List(tObjectStruct .ToList());

  2. ObservableCollection tObjectStruct = new ObservableCollection(tList);

数据库检索:

  1. public void AdvancedSearchFunc(AdvancedSearchNotification advancedSearchNotification)

  2. {

  3. try

  4. {

  5. KrayMobileDREntities dataBase = new KrayMobileDREntities();

  6. //每次使用前必须清零

  7. patientInfoHistroryModel.Clear();

  8. //先把数据库的数据提取出来,放到集合中。

  9. List<PatientInfo_Table> patientInfoList =

  10. dataBase.PatientInfo_Table.Where(u => u.PatientKey.ToString().Equals(advancedSearchNotification.PatientInfo)

  11. || u.PatientID.ToString().Equals(advancedSearchNotification.StudyID)

  12. || u.PatientName.ToString().Equals(advancedSearchNotification.PatientName)

  13. ).ToList();

  14. List<PatientStudy_Table> patientStudyList = dataBase.PatientStudy_Table.Where(u => u.PatientKey < 10).ToList();

  15. //按条件检索集合

  16. List<PatientInfoHistroryModel> list =

  17. (from pI in patientInfoList

  18. where (pI.PatientKey < 1000)

  19. select new PatientInfoHistroryModel()

  20. {

  21. PatientInfo = pI.PatientKey.ToString(),

  22. StudyID = pI.PatientID.ToString(),

  23. PatientName = pI.PatientName.ToString(),

  24. PatientSex = pI.PatientSex.ToString(),

  25. PatientAge = pI.PatientAge.ToString(),

  26. PatientBrith = pI.PatientBirthDate.ToString(),

  27. PatientHeight = pI.PatientHeight.ToString(),

  28. PatientWeight = pI.PatientWeight.ToString(),

  29. RecordSource = pI.PatientSource.ToString(),

  30. //StudyTime = PS.StudyDatetime,

  31. //EquipmentType = PS.StudyPhysician,

  32. //StudyPart = PS.StudyType,

  33. //SequenceAmount = PS.SeriesCount,

  34. StudyTime = pI.PatientAge.ToString(),

  35. EquipmentType = pI.PatientAge.ToString(),

  36. StudyPart = pI.HangFlag.ToString(),

  37. SequenceAmount = pI.HangFlag.ToString(),

  38. StudyStutas = pI.StudyCompleteFlag.ToString(),

  39. SuspendState = pI.HangFlag.ToString(),

  40. FilmPrint = pI.PrintFlag.ToString(),

  41. }).ToList();

  42. patientInfoHistroryModel = list;

  43. dataBase.Dispose();

  44. }

  45. catch (Exception ex)

  46. {

  47. MessageBox.Show("病人历史记录信息表【高级查询】状态下,发生数据库错误。错误信息:--------------" + ex.ToString());

  48. LogHelper.Error("OperateDataSheetViewModel.cs::AdvancedSearchFunc()高级查询失败--" + ex.Message);

  49. }

  50. }

四、总结

1、ObservableCollection表示一个动态数据集合,在添加项、移除项或刷新整个列表时,此集合将提供通知。

2、List表示可通过索引访问的对象的强类型列表。提供用于对列表进行搜索、排序和操作的方法。(大部分操作用Linq,很强大也很方便。)

参考连接:

https://blog.csdn.net/xpj8888/article/details/84782949

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多