如何使用WPF的數(shù)據(jù)綁定?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
江北網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),江北網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為江北上千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站建設(shè)要多少錢(qián),請(qǐng)找那個(gè)售后服務(wù)好的江北做網(wǎng)站的公司定做!
WPF最核心的技術(shù)優(yōu)勢(shì)之一就是數(shù)據(jù)綁定。數(shù)據(jù)綁定,可以通過(guò)對(duì)數(shù)據(jù)的操作來(lái)更新界面。
數(shù)據(jù)綁定最經(jīng)常用到的是ObservableCollection<T> 和 Dictionary<T, T> 這兩個(gè)類(lèi)。
ObservableCollection表示一個(gè)動(dòng)態(tài)數(shù)據(jù)集合,在添加項(xiàng)、移除項(xiàng)或刷新整個(gè)列表時(shí),此集合將提供通知,可以通過(guò)更新集合數(shù)據(jù)來(lái)更新界面顯示。
Dictionary字典類(lèi),檢索和數(shù)據(jù)操作性能極性,所以一些配置項(xiàng)的集合都使用它來(lái)保存。
因此,大家就想到的,有沒(méi)有ObservableCollection和Dictionary相結(jié)合的類(lèi)呢,于是就形成的ObservableDictionary類(lèi)。
網(wǎng)上有很多版本的ObservableDictionary類(lèi),據(jù)我了解到的,最早且最經(jīng)典的就是Dr.WPF里面的ItemsControl to a dictionary,其他的版本多數(shù)是參考這個(gè)來(lái)修改的(不對(duì)的那就是我孤陋寡聞了)。
今天我提供的這個(gè)版本,也是參考了網(wǎng)上的其他版本和Dr.WPF里的。
Dr.WPF里的定義是這樣的:
public class ObservableDictionary <TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback, INotifyCollectionChanged, INotifyPropertyChanged
大家細(xì)心點(diǎn)就會(huì)發(fā)現(xiàn),這里繼承的接口和Dictionary<TKey, TValue>所繼承的大部分相同,只是多了INotifyCollectionChanged, INotifyPropertyChanged
于是,今天我提供的版本,就直接繼承于Dictionary<TKey, TValue>和INotifyCollectionChanged, INotifyPropertyChanged。
本人測(cè)試過(guò),無(wú)BUG,性能也極佳,下面上代碼:
public class ObservableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, INotifyCollectionChanged, INotifyPropertyChanged { public ObservableDictionary() : base() { } private int _index; public event NotifyCollectionChangedEventHandler CollectionChanged; public event PropertyChangedEventHandler PropertyChanged; public new KeyCollection Keys { get { return base.Keys; } } public new ValueCollection Values { get { return base.Values; } } public new int Count { get { return base.Count; } } public new TValue this[TKey key] { get { return this.GetValue(key); } set { this.SetValue(key, value); } } public TValue this[int index] { get { return this.GetIndexValue(index); } set { this.SetIndexValue(index, value); } } public new void Add(TKey key, TValue value) { base.Add(key, value); this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, this.FindPair(key), _index)); OnPropertyChanged("Keys"); OnPropertyChanged("Values"); OnPropertyChanged("Count"); } public new void Clear() { base.Clear(); this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); OnPropertyChanged("Keys"); OnPropertyChanged("Values"); OnPropertyChanged("Count"); } public new bool Remove(TKey key) { var pair = this.FindPair(key); if (base.Remove(key)) { this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, pair, _index)); OnPropertyChanged("Keys"); OnPropertyChanged("Values"); OnPropertyChanged("Count"); return true; } return false; } protected void OnCollectionChanged(NotifyCollectionChangedEventArgs e) { if (this.CollectionChanged != null) { this.CollectionChanged(this, e); } } protected void OnPropertyChanged(string propertyName) { if (this.PropertyChanged != null) { this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } #region private方法 private TValue GetIndexValue(int index) { for (int i = 0; i < this.Count; i++) { if (i == index) { var pair = this.ElementAt(i); return pair.Value; } } return default(TValue); } private void SetIndexValue(int index, TValue value) { try { var pair = this.ElementAtOrDefault(index); SetValue(pair.Key, value); } catch (Exception) { } } private TValue GetValue(TKey key) { if (base.ContainsKey(key)) { return base[key]; } else { return default(TValue); } } private void SetValue(TKey key, TValue value) { if (base.ContainsKey(key)) { var pair = this.FindPair(key); int index = _index; base[key] = value; var newpair = this.FindPair(key); this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, newpair, pair, index)); OnPropertyChanged("Values"); OnPropertyChanged("Item[]"); } else { this.Add(key, value); } } private KeyValuePair<TKey, TValue> FindPair(TKey key) { _index = 0; foreach (var item in this) { if (item.Key.Equals(key)) { return item; } _index++; } return default(KeyValuePair<TKey, TValue>); } private int IndexOf(TKey key) { int index = 0; foreach (var item in this) { if (item.Key.Equals(key)) { return index; } index++; } return -1; } #endregion }
看完上述內(nèi)容,你們掌握如何使用WPF的數(shù)據(jù)綁定的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
標(biāo)題名稱(chēng):如何使用WPF的數(shù)據(jù)綁定
轉(zhuǎn)載來(lái)源:http://bm7419.com/article26/igdjjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開(kāi)發(fā)、搜索引擎優(yōu)化、網(wǎng)站內(nèi)鏈、響應(yīng)式網(wǎng)站、網(wǎng)站導(dǎo)航、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)