您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“c#中ObservableCollection<T>的示例代碼”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“c#中ObservableCollection<T>的示例代碼”這篇文章吧。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections.ObjectModel; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { //可觀察的集合 ObservableCollection<string> oc = new ObservableCollection<string>(); oc.CollectionChanged+=oc_CollectionChanged; oc.Add("a"); oc.Add("b"); oc.Insert(2, "c"); oc.Remove("c"); foreach (var item in oc) { Console.WriteLine(item); } Console.ReadKey(); //輸出: //操作:Add //被添加的索引:0 //操作:Add //被添加的索引:1 //操作:Add //被添加的索引:2 //操作:Remove //被添加的索引:2 //a //b } private static void oc_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { Console.WriteLine("操作:{0}", e.Action.ToString());//引起該事件的操作 if (e.NewItems != null)//添加 { Console.WriteLine("被添加的索引:{0}", e.NewStartingIndex.ToString()); } if (e.OldItems != null)//刪除 { Console.WriteLine("被刪除的索引:{0}", e.OldStartingIndex.ToString()); } } } }
以上是“c#中ObservableCollection<T>的示例代碼”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。