聊聊C# ObservableCollection和List
一、ObservableCollection和List的區(qū)別
1)ObservableCollection比較簡單,繼承了Collection, INotifyCollectionChanged, INotifyPropertyChanged
Collection:為泛型集合提供基類。
INotifyCollectionChanged:將集合的動態(tài)更改通知給偵聽器,例如,何時添加和移除項或者重置整個集合對象。
INotifyPropertyChanged:向客戶端發(fā)出某一屬性值已更改的通知。
所以再ObservableCollection這個類的方法,對數(shù)據(jù)的操作很少,重點放在了當(dāng)自己本事變化的時候(不管是屬性,還是集合)會調(diào)用發(fā)出通知的事件。(一般用于更新UI,當(dāng)然也可以用于寫其他的事情。這個以后會寫)
2)List就比較多了,繼承了IList, ICollection, IEnumerable, IList, ICollection, IEnumerable。
IList:表示可按照索引單獨訪問的一組對象。
ICollection:定義操作泛型集合的方法。
IEnumerable:公開枚舉器,該枚舉器支持在指定類型的集合上進(jìn)行簡單迭代。
IList:表示可按照索引單獨訪問的對象的非泛型集合。
ICollection:定義所有非泛型集合的大小、枚舉器和同步方法。
IEnumerable:公開枚舉器,該枚舉器支持在非泛型集合上進(jìn)行簡單迭代。
二、舉例:
1、舉例1:
MainWindow.xaml:
- <ListBox x:Name="listbind" Height="61" HorizontalAlignment="Left" Margin="146,12,0,0" VerticalAlignment="Top" Width="120" >
- <ListBox.ItemTemplate>
- <DataTemplate>
- <TextBlock Text="{Binding Name}" />
- </DataTemplate>
- </ListBox.ItemTemplate>
- </ListBox>
- <ListBox x:Name="observbind" Height="74" HorizontalAlignment="Left" Margin="146,111,0,0" VerticalAlignment="Top" Width="120" >
- <ListBox.ItemTemplate>
- <DataTemplate>
- <TextBlock Text="{Binding Name}" />
- </DataTemplate>
- </ListBox.ItemTemplate>
- </ListBox>
- <TextBlock Height="23" HorizontalAlignment="Left" Margin="38,58,0,0" Name="textBlock1" Text="List綁定數(shù)據(jù)" VerticalAlignment="Top" />
- <TextBlock Height="44" HorizontalAlignment="Left" Margin="12,125,0,0" Name="textBlock2" Text="ObservableCollection綁定數(shù)據(jù)" VerticalAlignment="Top" Width="112" />
- <Button Content="Button" Height="23" HorizontalAlignment="Left"
xaml頁面很簡單,托2個listbox分別用來綁定ObservableCollection和List
Person.cs:
- public class Person
- {
- public string Name { get; set; }
- }
MainWindow.xaml.cs:
- private List<Person> person1 = new List<Person>();
- private ObservableCollection<Person> person2 = new ObservableCollection<Person>();
- public DemoTestDiff()
- {
- InitializeComponent();
- person1.Add(new Person() { Name = "張三" });
- person1.Add(new Person() { Name = "李四" });
- listbind.ItemsSource = person1;
- person2.Add(new Person() { Name = "張三" });
- person2.Add(new Person() { Name = "李四" });
- observbind.ItemsSource = person2;
- }
- private void button1_Click(object sender, RoutedEventArgs e)
- {
- person1.Add(new Person() { Name = "王五" });
- person2.Add(new Person() { Name = "王五" });
- }
- 運(yùn)行程序點擊button按鈕,然后只有ObservableCollection的有添加。
表示當(dāng)集合對象的集合改變時,只有ObservableCollection會發(fā)出通知更新UI。
這只是他們兩個區(qū)別之一。
2、舉例2
以下方法可以更新ListView的UI:
- private ObservableCollection<PreviewListModel> _previewList = new ObservableCollection<PreviewListModel>();
- /// <summary>
- /// 預(yù)覽信息列表
- /// </summary>
- public ObservableCollection<PreviewListModel> PreviewList
- {
- get { return _previewList; }
- set { SetProperty(ref _previewList, value); }
- //set { _previewList = value; RaisePropertyChanged("PreviewList"); }
- }
三、 ObservableCollection和List的互相轉(zhuǎn)換
https://www.cnblogs.com/warioland/archive/2011/11/08/2240858.html
從數(shù)據(jù)庫檢索的出來的集合是List
- T tList = new List(tObjectStruct .ToList());
- ObservableCollection tObjectStruct = new
數(shù)據(jù)庫檢索:
- public void AdvancedSearchFunc(AdvancedSearchNotification advancedSearchNotification)
- {
- try
- {
- KrayMobileDREntities dataBase = new KrayMobileDREntities();
- //每次使用前必須清零
- patientInfoHistroryModel.Clear();
- //先把數(shù)據(jù)庫的數(shù)據(jù)提取出來,放到集合中。
- List<PatientInfo_Table> patientInfoList =
- dataBase.PatientInfo_Table.Where(u => u.PatientKey.ToString().Equals(advancedSearchNotification.PatientInfo)
- || u.PatientID.ToString().Equals(advancedSearchNotification.StudyID)
- || u.PatientName.ToString().Equals(advancedSearchNotification.PatientName)
- ).ToList();
- List<PatientStudy_Table> patientStudyList = dataBase.PatientStudy_Table.Where(u => u.PatientKey < 10).ToList();
- //按條件檢索集合
- List<PatientInfoHistroryModel> list =
- (from pI in patientInfoList
- where (pI.PatientKey < 1000)
- select new PatientInfoHistroryModel()
- {
- 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.StudyPhysician,
- //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();
- patientInfoHistroryModel = list;
- dataBase.Dispose();
- }
- catch (Exception ex)
- {
- MessageBox.Show("病人歷史記錄信息表【高級查詢】狀態(tài)下,發(fā)生數(shù)據(jù)庫錯誤。錯誤信息:--------------" + ex.ToString());
- LogHelper.Error("OperateDataSheetViewModel.cs::AdvancedSearchFunc()高級查詢失敗--" + ex.Message);
- }
四、總結(jié)
1、ObservableCollection表示一個動態(tài)數(shù)據(jù)集合,在添加項、移除項或刷新整個列表時,此集合將提供通知。
2、List表示可通過索引訪問的對象的強(qiáng)類型列表。提供用于對列表進(jìn)行搜索、排序和操作的方法。(大部分操作用Linq,很強(qiáng)大也很方便。
參考連接:
https://blog.csdn.net/xpj8888/article/details/84782949
本文轉(zhuǎn)載自微信公眾號「CSharp編程大全」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系CSharp編程大全公眾號。