Summary of the difference between Winform and WPF data binding

        First of all, let’s talk about WPF. When I first learned about the concept of data binding, I started to understand and use it when I worked on Winform development for 3 years, and then I switched to WPF projects and coded according to the MVVM model.

        I have always thought that data binding, as the core content of MVVM, is only applicable to WPF. Later, I learned that it is also applicable to Winform. The more famous ones, such as the domestic DevExpress control, I believe everyone who develops Winform will have heard of it.

        So, to sum up my personal understanding, the difference between the two in data binding:

WPF:

Core interface: INotifyPropertyChanged

namespace System.ComponentModel
{
    //
    // 摘要:
    //     通知客户端属性值已更改。
    public interface INotifyPropertyChanged
    {
        //
        // 摘要:
        //     在属性值更改时发生。
        event PropertyChangedEventHandler PropertyChanged;
    }
}

Core DataSource container: ObservableCollection<T> inherits from Collection<T> and is located under the System.Collections.ObjectModel namespace

Use process:

①The Model class integrates the INotifyPropertyChanged interface. In the set method of the member property, the property name is used as a parameter to trigger the event PropertyChanged;

② Use the Model instance to assign a value to the DataSource of the WPF user control (which can be a single native control or a custom control composed of native controls), complete the binding, and pay attention to the UpdateSourceTrigger setting of the control;

③ Or use the ObservableCollection<Model> instance to assign a value to the DataSource of the WPF container control to complete the binding;

④ Update the properties of the Model class instance, and you can observe the property changes on the controls bound to the instance;

——Very intuitive, its essence is the processing of the PropertyChanged event and its parameters by the internal code of the control

Winform:

Core interface: still INotifyPropertyChanged

(Container Control) Core Interface: IBindList

The usage is roughly the same as above, change ObservableCollection<Model> to BindingList<Model> to assign the DataSource of the Winform control;

——(guess the essence)

①The Model attribute is assigned in the background code, and the attribute name is used as the parameter PropertyChangedEventArgs to trigger the PropertyChanged event;

②It is further encapsulated as ListChangedEventArgs to trigger the ListChanged event of the container class BindingList;

③In the processing function of OnListChanged, according to the property name of the first step in the parameter, the SortPropertyCore property of type PropertyDescriptor (not so much the base class of the property, but rather the collection of properties encapsulated) retrieves the value after the corresponding property name has been changed, and redraws the control.

 Single control (custom control will be supplemented after understanding later)

Guess you like

Origin blog.csdn.net/qq_23958061/article/details/126539524