一、ObservableCollection和List的區別
1)ObservableCollection比較簡(jiǎn)單,繼承了Collection, INotifyCollectionChanged, INotifyPropertyChanged
Collection:為泛型集合提供基類(lèi)。
INotifyCollectionChanged:將集合的動(dòng)態(tài)更改通知給偵聽(tīng)器,例如,何時(shí)添加和移除項或者重置整個(gè)集合對象。
INotifyPropertyChanged:向客戶(hù)端發(fā)出某一屬性值已更改的通知。
所以再ObservableCollection這個(gè)類(lèi)的方法,對數據的操作很少,重點(diǎn)放在了當自己本事變化的時(shí)候(不管是屬性,還是集合)會(huì )調用發(fā)出通知的事件。(一般用于更新UI,
當然也可以用于寫(xiě)其他的事情。這個(gè)以后會(huì )寫(xiě))
2)List就比較多了,繼承了IList, ICollection, IEnumerable, IList, ICollection, IEnumerable。
IList:表示可按照索引單獨訪(fǎng)問(wèn)的一組對象。
ICollection:定義操作泛型集合的方法。
IEnumerable:公開(kāi)枚舉器,該枚舉器支持在指定類(lèi)型的集合上進(jìn)行簡(jiǎn)單迭代。
IList:表示可按照索引單獨訪(fǎng)問(wèn)的對象的非泛型集合。
ICollection:定義所有非泛型集合的大小、枚舉器和同步方法。
IEnumerable:公開(kāi)枚舉器,該枚舉器支持在非泛型集合上進(jìn)行簡(jiǎ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綁定數據" VerticalAlignment="Top" />
<TextBlock Height="44" HorizontalAlignment="Left" Margin="12,125,0,0" Name="textBlock2" Text="ObservableCollection綁定數據" VerticalAlignment="Top" Width="112" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="77,211,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
xaml頁(yè)面很簡(jiǎn)單,托2個(gè)listbox分別用來(lái)綁定ObservableCollection和List
Person.cs:
1 public class Person
2 {
3 public string Name { get; set; }
4 }
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 = "王五" });
}
運行程序點(diǎn)擊button按鈕,然后只有ObservableCollection的有添加。表示當集合對象的集合改變時(shí),只有ObservableCollection會(huì )發(fā)出通知更新UI。
這只是他們兩個(gè)區別之一。
2、舉例2
以下方法可以更新ListView的UI:
private ObservableCollection<PreviewListModel> _previewList = new ObservableCollection<PreviewListModel>();
/// <summary>
/// 預覽信息列表
/// </summary>
public ObservableCollection<PreviewListModel> PreviewList
{
get { return _previewList; }
set { SetProperty(ref _previewList, value); }
//set { _previewList = value; RaisePropertyChanged("PreviewList"); }
}
三、 ObservableCollection和List的互相轉換
https://www.cnblogs.com/warioland/archive/2011/11/08/2240858.html
從數據庫檢索的出來(lái)的集合是List<T>類(lèi)型,我們需要把它轉成ObservableCollection類(lèi)型怎么辦?如下方法:
T tList = new List(tObjectStruct .ToList());
ObservableCollection tObjectStruct = new ObservableCollection(tList);
數據庫檢索:
public void AdvancedSearchFunc(AdvancedSearchNotification advancedSearchNotification)
{
try
{
KrayMobileDREntities dataBase = new KrayMobileDREntities();
//每次使用前必須清零
patientInfoHistroryModel.Clear();
//先把數據庫的數據提取出來(lái),放到集合中。
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("病人歷史記錄信息表【高級查詢(xún)】狀態(tài)下,發(fā)生數據庫錯誤。錯誤信息:--------------" + ex.ToString());
LogHelper.Error("OperateDataSheetViewModel.cs::AdvancedSearchFunc()高級查詢(xún)失敗--" + ex.Message);
}
}
四、總結
1、ObservableCollection表示一個(gè)動(dòng)態(tài)數據集合,在添加項、移除項或刷新整個(gè)列表時(shí),此集合將提供通知。
2、List表示可通過(guò)索引訪(fǎng)問(wèn)的對象的強類(lèi)型列表。提供用于對列表進(jìn)行搜索、排序和操作的方法。(大部分操作用Linq,很強大也很方便。)
參考連接:
https://blog.csdn.net/xpj8888/article/details/84782949