The core technology of WPF-data binding
One of the core technical advantages of WPF is data binding. Data binding can update the interface by operating on data.
The two classes most commonly used for data binding are ObservableCollection
ObservableCollection represents a dynamic data collection. This collection will provide notifications when items are added, items are removed, or the entire list is refreshed. The interface display can be updated by updating the collection data.
Dictionary dictionary class has extremely poor retrieval and data operation performance, so some collections of configuration items are saved using it.
Therefore, everyone thought of whether there is a class that combines ObservableCollection and Dictionary, so the ObservableDictionary class was formed.
There are many versions of the ObservableDictionary class on the Internet. As far as I know, the earliest and most classic one is ItemsControl to a dictionary in Dr.WPF. Most other versions are modified with reference to this (the wrong one It’s just that I am ignorant).
The version I provide today also refers to other versions on the Internet and those in Dr.WPF.
The definition in Dr.WPF is like this:
public class ObservableDictionary <TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback, INotifyCollectionChanged, INotifyPropertyChanged
If you look carefully, you will find that the interface inherited here is the same as that inherited by Dictionary
So, the version I provide today directly inherits from Dictionary
I have tested it, there are no BUGs, and the performance is excellent. The code is as follows:
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 }
In terms of extension, you can modify it with the Dr.WPF version, which is more efficient More technical and more scalable!
The above is the detailed content of The core technology of WPF-data binding. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Developed by GudioVanRossum in 1991. It supports multiple programming paradigms, including structured, object-oriented, and functional programming. Before we dive into this topic, let's review the basic concepts relevant to the questions we provide. A dictionary is a unique, mutable, and ordered set of items. Curly braces are used when writing dictionaries, and they contain keys and values: key names can be used to refer to dictionary objects. Data values are stored in dictionaries in the form of key:value pairs. Ordered and unordered meaning When we say that a dictionary is ordered, we mean that its contents have a certain order and do not change. Unordered items lack a clear order and therefore cannot be used

The dictionary in Python is a flexible and powerful data structure that can store key-value pairs and has fast search and insertion functions. However, if you are not careful with dictionary key-value pairs, you may encounter the problem of empty dictionary keys. This problem often causes the code to crash or output unexpected results. This article will introduce two methods to solve the empty dictionary key error in Python. Method 1: Use if statements to prevent empty dictionary keys. Python dictionaries cannot have duplicate keys, otherwise the previous key-value pairs will be overwritten. When the value of a dictionary key is empty

It is difficult to implement collection-like functions in the Go language, which is a problem that troubles many developers. Compared with other programming languages such as Python or Java, the Go language does not have built-in collection types, such as set, map, etc., which brings some challenges to developers when implementing collection functions. First, let's take a look at why it is difficult to implement collection-like functionality directly in the Go language. In the Go language, the most commonly used data structures are slice and map. They can complete collection-like functions, but

Dictionaries are a powerful data type in Python. It consists of key-value pairs. Searching, appending and other operations can be efficiently completed through this data type. While accessing values in a dictionary is simple, there may be situations where you need to look up the next key in the dictionary. Python provides several ways to achieve this, depending on your specific requirements. In this article, we will explore different ways of getting the next key in a dictionary in Python. Using the keys and index methods dictionaries are unordered collections in Python. So we first need to convert the keys into some sorted form. We can first append all keys in the form of a list. Next, we can find the next key by indexing the list. With the help of keys we can also

Java is a powerful programming language that is widely used in various types of software development. In Java development, scenarios that often involve sorting collections are involved. However, if performance optimization is not performed for collection sorting, the execution efficiency of the program may decrease. This article will explore how to optimize the performance of Java collection sorting. 1. Choose the appropriate collection class In Java, there are many collection classes that can be used for sorting, such as ArrayList, LinkedList, TreeSet, etc. Different collection classes are in

C++ differs from Python in terms of a dictionary with the same name, but it has the same data structure with similar functionality. C++ supports mapping, which can be used in the STL class std::map. The map object contains a pair of values in each entry, one is the key value and the other is the map value. Key values are used to search for and uniquely identify entries in the map. While mapped values are not necessarily unique, key values must always be unique in the map. Let's take a look at how to use mapping. First, let's see how to define a mapped data structure in C++. Syntax #includemap<data_type1,data_type2>myMap; Let’s take an example to see how to do this − Example #incl

Dictionaries are known as collection data types. They store data in the form of key-value pairs. They are ordered and mutable, i.e. they follow a specific order and are indexed. We can change the value of a key so it is manipulable or changeable. Dictionaries do not support data duplication. Each key can have multiple values associated with it, but a single value cannot have multiple keys. We can perform many operations using dictionaries. The whole mechanism depends on the stored value. In this article, we will discuss the techniques you can use to remove "null values" from a dictionary. Before starting the main operation, we must have an in-depth understanding of value handling in dictionaries. Let’s take a quick overview of this article. This article is divided into two parts - Part 1st will focus on the concept of "null value" and its significance. In part 2nd

Practical Guide to Where Method in Laravel Collections During the development of the Laravel framework, collections are a very useful data structure that provide rich methods to manipulate data. Among them, the Where method is a commonly used filtering method that can filter elements in a collection based on specified conditions. This article will introduce the use of the Where method in Laravel collections and demonstrate its usage through specific code examples. 1. Basic usage of Where method
