{"id":210872,"date":"2021-02-19T08:14:52","date_gmt":"2021-02-19T00:14:52","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=210872"},"modified":"2021-02-02T16:54:31","modified_gmt":"2021-02-02T08:54:31","slug":"observablecollection-list","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/observablecollection-list.html","title":{"rendered":"\u6765\u770b\u770bC# ObservableCollection\u548cList\u7684\u533a\u522b"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u804a\u804aC# ObservableCollection\u548cListObservableCollection\u8fd9\u4e2a\u7c7b\u7684\u65b9\u6cd5\uff0c\u5bf9\u6570\u636e\u7684\u64cd\u4f5c\u5f88\u5c11\uff0c\u91cd\u70b9\u653e\u5728\u4e86\u5f53\u81ea\u5df1\u672c\u4e8b\u53d8\u5316\u7684\u65f6\u5019(\u4e0d\u7ba1\u662f\u5c5e\u6027\uff0c\u8fd8\u662f\u96c6\u5408)\u4f1a\u8c03\u7528\u53d1\u51fa\u901a\u77e5\u7684\u4e8b\u4ef6\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u4e00\u3001ObservableCollection\u548cList\u7684\u533a\u522b<\/strong><\/div>\n
1)ObservableCollection\u6bd4\u8f83\u7b80\u5355\uff0c\u7ee7\u627f\u4e86Collection, INotifyCollectionChanged, INotifyPropertyChanged<\/strong><\/span><\/div>\n

Collection\uff1a\u4e3a\u6cdb\u578b\u96c6\u5408\u63d0\u4f9b\u57fa\u7c7b\u3002<\/p>\n

INotifyCollectionChanged\uff1a\u5c06\u96c6\u5408\u7684\u52a8\u6001\u66f4\u6539\u901a\u77e5\u7ed9\u4fa6\u542c\u5668\uff0c\u4f8b\u5982\uff0c\u4f55\u65f6\u6dfb\u52a0\u548c\u79fb\u9664\u9879\u6216\u8005\u91cd\u7f6e\u6574\u4e2a\u96c6\u5408\u5bf9\u8c61\u3002<\/p>\n

INotifyPropertyChanged\uff1a\u5411\u5ba2\u6237\u7aef\u53d1\u51fa\u67d0\u4e00\u5c5e\u6027\u503c\u5df2\u66f4\u6539\u7684\u901a\u77e5\u3002<\/p>\n

\u6240\u4ee5\u518dObservableCollection\u8fd9\u4e2a\u7c7b\u7684\u65b9\u6cd5\uff0c\u5bf9\u6570\u636e\u7684\u64cd\u4f5c\u5f88\u5c11\uff0c\u91cd\u70b9\u653e\u5728\u4e86\u5f53\u81ea\u5df1\u672c\u4e8b\u53d8\u5316\u7684\u65f6\u5019(\u4e0d\u7ba1\u662f\u5c5e\u6027\uff0c\u8fd8\u662f\u96c6\u5408)\u4f1a\u8c03\u7528\u53d1\u51fa\u901a\u77e5\u7684\u4e8b\u4ef6\u3002(\u4e00\u822c\u7528\u4e8e\u66f4\u65b0UI\uff0c\u5f53\u7136\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5199\u5176\u4ed6\u7684\u4e8b\u60c5\u3002\u8fd9\u4e2a\u4ee5\u540e\u4f1a\u5199)<\/p>\n

2)List\u5c31\u6bd4\u8f83\u591a\u4e86\uff0c\u7ee7\u627f\u4e86IList, ICollection, IEnumerable, IList, ICollection, IEnumerable\u3002<\/strong><\/span><\/div>\n

IList\uff1a\u8868\u793a\u53ef\u6309\u7167\u7d22\u5f15\u5355\u72ec\u8bbf\u95ee\u7684\u4e00\u7ec4\u5bf9\u8c61\u3002<\/p>\n

ICollection\uff1a\u5b9a\u4e49\u64cd\u4f5c\u6cdb\u578b\u96c6\u5408\u7684\u65b9\u6cd5\u3002<\/p>\n

IEnumerable\uff1a\u516c\u5f00\u679a\u4e3e\u5668\uff0c\u8be5\u679a\u4e3e\u5668\u652f\u6301\u5728\u6307\u5b9a\u7c7b\u578b\u7684\u96c6\u5408\u4e0a\u8fdb\u884c\u7b80\u5355\u8fed\u4ee3\u3002<\/p>\n

IList\uff1a\u8868\u793a\u53ef\u6309\u7167\u7d22\u5f15\u5355\u72ec\u8bbf\u95ee\u7684\u5bf9\u8c61\u7684\u975e\u6cdb\u578b\u96c6\u5408\u3002<\/p>\n

ICollection\uff1a\u5b9a\u4e49\u6240\u6709\u975e\u6cdb\u578b\u96c6\u5408\u7684\u5927\u5c0f\u3001\u679a\u4e3e\u5668\u548c\u540c\u6b65\u65b9\u6cd5\u3002<\/p>\n

IEnumerable\uff1a\u516c\u5f00\u679a\u4e3e\u5668\uff0c\u8be5\u679a\u4e3e\u5668\u652f\u6301\u5728\u975e\u6cdb\u578b\u96c6\u5408\u4e0a\u8fdb\u884c\u7b80\u5355\u8fed\u4ee3\u3002<\/p>\n

\u4e8c\u3001\u4e3e\u4f8b:<\/strong><\/div>\n
1\u3001\u4e3e\u4f8b1\uff1a<\/strong><\/span><\/div>\n

MainWindow.xaml\uff1a<\/p>\n

xaml\u9875\u9762\u5f88\u7b80\u5355\uff0c\u62582\u4e2alistbox\u5206\u522b\u7528\u6765\u7ed1\u5b9aObservableCollection\u548cList
\nPerson.cs\uff1a<\/p>\n

public class Person   \r\n     { \r\n         public string Name { get; set; } \r\n     } \r\n<\/pre>\n

MainWindow.xaml.cs\uff1a<\/p>\n

private List person1 = new List(); \r\nprivate ObservableCollection person2 = new ObservableCollection(); \r\npublic DemoTestDiff()   \r\n{ \r\nInitializeComponent(); \r\nperson1.Add(new Person() { Name = \"\u5f20\u4e09\" }); \r\nperson1.Add(new Person() { Name = \"\u674e\u56db\" }); \r\nlistbind.ItemsSource = person1; \r\nperson2.Add(new Person() { Name = \"\u5f20\u4e09\" }); \r\nperson2.Add(new Person() { Name = \"\u674e\u56db\" }); \r\nobservbind.ItemsSource = person2; \r\n} \r\nprivate void button1_Click(object sender, RoutedEventArgs e)   \r\n{ \r\nperson1.Add(new Person() { Name = \"\u738b\u4e94\" }); \r\nperson2.Add(new Person() { Name = \"\u738b\u4e94\" }); \r\n} \r\n<\/pre>\n

\u8fd0\u884c\u7a0b\u5e8f\u70b9\u51fbbutton\u6309\u94ae,\u7136\u540e\u53ea\u6709ObservableCollection\u7684\u6709\u6dfb\u52a0\u3002
\n\u8868\u793a\u5f53\u96c6\u5408\u5bf9\u8c61\u7684\u96c6\u5408\u6539\u53d8\u65f6\uff0c\u53ea\u6709ObservableCollection\u4f1a\u53d1\u51fa\u901a\u77e5\u66f4\u65b0UI\u3002<\/p>\n

\u8fd9\u53ea\u662f\u4ed6\u4eec\u4e24\u4e2a\u533a\u522b\u4e4b\u4e00\u3002<\/p>\n

2\u3001\u4e3e\u4f8b2<\/strong><\/span><\/div>\n

\u4ee5\u4e0b\u65b9\u6cd5\u53ef\u4ee5\u66f4\u65b0ListView\u7684UI\uff1a<\/p>\n

\r\nprivate ObservableCollection<PreviewListModel> _previewList = new ObservableCollection<PreviewListModel>();\r\n\/\/\/ <summary>\r\n\/\/\/ \u9884\u89c8\u4fe1\u606f\u5217\u8868\r\n\/\/\/ <\/summary>\r\npublic ObservableCollection<PreviewListModel> PreviewList\r\n{\r\nget { return _previewList; }\r\nset { SetProperty(ref _previewList, value); }\r\n\/\/set { _previewList = value; RaisePropertyChanged(\"PreviewList\"); }\r\n}\r\n<\/pre>\n
\u4e09\u3001 ObservableCollection\u548cList\u7684\u4e92\u76f8\u8f6c\u6362<\/strong><\/div>\n

\u4ece\u6570\u636e\u5e93\u68c0\u7d22\u7684\u51fa\u6765\u7684\u96c6\u5408\u662fList\u7c7b\u578b\uff0c\u6211\u4eec\u9700\u8981\u628a\u5b83\u8f6c\u6210ObservableCollection\u7c7b\u578b\u600e\u4e48\u529e?\u5982\u4e0b\u65b9\u6cd5\uff1a<\/p>\n

T tList = new List(tObjectStruct .ToList()); \r\nObservableCollection tObjectStruct  = new  \r\n<\/pre>\n

\u6570\u636e\u5e93\u68c0\u7d22\uff1a<\/p>\n

\r\npublic void AdvancedSearchFunc(AdvancedSearchNotification advancedSearchNotification)\r\n{\r\ntry\r\n{\r\nKrayMobileDREntities dataBase = new KrayMobileDREntities();\r\n\/\/\u6bcf\u6b21\u4f7f\u7528\u524d\u5fc5\u987b\u6e05\u96f6\r\npatientInfoHistroryModel.Clear();\r\n\/\/\u5148\u628a\u6570\u636e\u5e93\u7684\u6570\u636e\u63d0\u53d6\u51fa\u6765\uff0c\u653e\u5230\u96c6\u5408\u4e2d\u3002\r\nList<PatientInfo_Table> patientInfoList =\r\ndataBase.PatientInfo_Table.Where(u => u.PatientKey.ToString().Equals(advancedSearchNotification.PatientInfo)\r\n|| u.PatientID.ToString().Equals(advancedSearchNotification.StudyID)\r\n|| u.PatientName.ToString().Equals(advancedSearchNotification.PatientName)\r\n).ToList();\r\nList<PatientStudy_Table> patientStudyList = dataBase.PatientStudy_Table.Where(u => u.PatientKey < 10).ToList();\r\n\/\/\u6309\u6761\u4ef6\u68c0\u7d22\u96c6\u5408\r\nList<PatientInfoHistroryModel> list =\r\n(from pI in patientInfoList\r\nwhere (pI.PatientKey < 1000)\r\nselect new PatientInfoHistroryModel()\r\n{\r\nPatientInfo = pI.PatientKey.ToString(),\r\nStudyID = pI.PatientID.ToString(),\r\nPatientName = pI.PatientName.ToString(),\r\nPatientSex = pI.PatientSex.ToString(),\r\nPatientAge = pI.PatientAge.ToString(),\r\nPatientBrith = pI.PatientBirthDate.ToString(),\r\nPatientHeight = pI.PatientHeight.ToString(),\r\nPatientWeight = pI.PatientWeight.ToString(),\r\nRecordSource = pI.PatientSource.ToString(),\r\n\/\/StudyTime       = PS.StudyDatetime,\r\n\/\/EquipmentType   = PS.StudyPhysician,\r\n\/\/StudyPart       = PS.StudyType,\r\n\/\/SequenceAmount  = PS.SeriesCount,\r\nStudyTime = pI.PatientAge.ToString(),\r\nEquipmentType = pI.PatientAge.ToString(),\r\nStudyPart = pI.HangFlag.ToString(),\r\nSequenceAmount = pI.HangFlag.ToString(),\r\nStudyStutas = pI.StudyCompleteFlag.ToString(),\r\nSuspendState = pI.HangFlag.ToString(),\r\nFilmPrint = pI.PrintFlag.ToString(),\r\n}).ToList();\r\npatientInfoHistroryModel = list;\r\ndataBase.Dispose();\r\n}\r\ncatch (Exception ex)\r\n{\r\nMessageBox.Show(\"\u75c5\u4eba\u5386\u53f2\u8bb0\u5f55\u4fe1\u606f\u8868\u3010\u9ad8\u7ea7\u67e5\u8be2\u3011\u72b6\u6001\u4e0b,\u53d1\u751f\u6570\u636e\u5e93\u9519\u8bef\u3002\u9519\u8bef\u4fe1\u606f:--------------\" + ex.ToString());\r\nLogHelper.Error(\"OperateDataSheetViewModel.cs::AdvancedSearchFunc()\u9ad8\u7ea7\u67e5\u8be2\u5931\u8d25--\" + ex.Message);\r\n}\r\n}\r\n<\/pre>\n
\u56db\u3001\u603b\u7ed3<\/strong><\/div>\n

1\u3001ObservableCollection\u8868\u793a\u4e00\u4e2a\u52a8\u6001\u6570\u636e\u96c6\u5408\uff0c\u5728\u6dfb\u52a0\u9879\u3001\u79fb\u9664\u9879\u6216\u5237\u65b0\u6574\u4e2a\u5217\u8868\u65f6\uff0c\u6b64\u96c6\u5408\u5c06\u63d0\u4f9b\u901a\u77e5\u3002
\n2\u3001List\u8868\u793a\u53ef\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u7684\u5bf9\u8c61\u7684\u5f3a\u7c7b\u578b\u5217\u8868\u3002\u63d0\u4f9b\u7528\u4e8e\u5bf9\u5217\u8868\u8fdb\u884c\u641c\u7d22\u3001\u6392\u5e8f\u548c\u64cd\u4f5c\u7684\u65b9\u6cd5\u3002(\u5927\u90e8\u5206\u64cd\u4f5c\u7528Linq\uff0c\u5f88\u5f3a\u5927\u4e5f\u5f88\u65b9\u4fbf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

Collection\uff1a\u4e3a\u6cdb\u578b\u96c6\u5408\u63d0\u4f9b\u57fa\u7c7b\u3002 INotifyCollectionChanged\uff1a\u5c06\u96c6\u5408\u7684\u52a8\u6001\u66f4\u6539 […]<\/p>\n","protected":false},"author":1898,"featured_media":210878,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[651],"class_list":["post-210872","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-c-observablecollectionlist"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/210872","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=210872"}],"version-history":[{"count":13,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/210872\/revisions"}],"predecessor-version":[{"id":211165,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/210872\/revisions\/211165"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/210878"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=210872"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=210872"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=210872"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}