Basic skills (08) -------- full binding of WPF data in MVVM mode -------- mvvmwpf

Source: Internet
Author: User

Basic skills (08) -------- full binding of WPF data in MVVM mode -------- mvvmwpf
1. Description: bind the name and age of the background code to the text box, click "add age" -- age + 1, click "show age" -- the age dialog box is displayed, you can change the age from the text box to the background and display it to the text box.

The running result and solution management are as follows:

Ii. person class
Using System; using System. collections. generic; using System. linq; using System. text; using System. componentModel; namespace full data binding {class Person: INotifyPropertyChanged // INotifyPropertyChanged yes. net built-in interface. Data Binding checks whether the DataContext has implemented INotifyPropertyChanged. If so, it listens to the event {private string name; // defines the name private int age; // defines the Age public string Name {get; set;} public int Age // defines the Age attribute {get {retur N age;} set {this. age = value; if (PropertyChanged! = Null) {PropertyChanged (this, new PropertyChangedEventArgs ("Age"); // trigger event, parameter description: trigger object, event data }}public event PropertyChangedEventHandler PropertyChanged; // implement the interface and define an attribute change event }}

 

Iii. Background code
Using System; using System. collections. generic; using System. linq; using System. text; using System. windows; using System. windows. controls; using System. windows. data; using System. windows. documents; using System. windows. input; using System. windows. media; using System. windows. media. imaging; using System. windows. navigation; using System. windows. shapes; namespace full data binding {// <summary> // MainWindow. interaction logic of xaml // </summary> public partial class MainWindow: Window {private Person p1 = new Person (); // defines a field and initializes public MainWindow () {InitializeComponent ();} private void Window_Loaded (object sender, RoutedEventArgs e) {p1.Name = "marathon"; p1.Age = 24; txtName. dataContext = p1; txtAge. dataContext = p1;} // Add age private void btnAdd_Click (object sender, RoutedEventArgs e) {p1.Age ++;} // display age private void btnShow_Click (object sender, RoutedEventArgs e) {MessageBox. show (p1.Age. toString ());}}}
4. What is MVVM?

It can be said that MVVM is a model designed for WPF, or MVVM is just a variant of MVC. However, in practice, if you or your team are not used to Binding, it makes little sense to study MVVM.

 


The MVVM design mode of WPF

VIEW: Your interface is XAML.
VIEWMODEL: the business logic of the interface, that is, the previous XAML. CS file.
MODEL: Your Business Data MODEL

According to the above definition, you should write it on VIEWMODEL. Data between two views is transmitted in MVVM through the DataContext attribute. DataContext is crucial in MVVM!
The essence of MVVM is binding, but MVVM requires stricter requirements. The most orthodox MVVM requires that all control programming IDs of XAML be deleted.

It is very error-prone to write MVVM by yourself. I recommend simplemvvmtoolkit. (The project is in codeplex ). It has two advantages: one is the lamda expression used for property change notification, so that the attribute name in PropertyChange will be changed after you use the refactoring tool to modify the model attribute, this avoids errors caused by writing attribute strings directly.
Another is that it provides a global event bus. DataContext Data Exchange requires that there is a reference relationship between two viewmodels, but this is not always the case in actual applications (such as the log collector interface ). With this feature, you can subscribe to multiple ViewModel events. Any ViewModel that publishes events and other subscribed events can process events.

Simplemvvmtoolkit has a small problem: it is ineffective to bind UserControl with its EventToCommand. This is no problem with the RelayCommand of mvvmlight (also in codeplex, therefore, these two libraries are usually used together when I create a project.

Using simplemvvmtoolkit to write MVVM, You can first draw the View and write the Model definitions of the ViewModel and Model. Model binding can be completed by blend with the mouse. Do not use the keyboard. After binding, you only need to focus on the business logic of ViewModel.

How to change data in the MVVM subwindow in WPF

In this case, the data binding in wpf is to establish a relationship between the source and the target of the displayed data. The binding mode usually includes one-way binding and two-way binding (one-way binding by default, and when the data source implements the INotifyPropertyChanged interface, the data source changes, and the front-end display changes). What if it is two-way binding, source and target will change as long as one party changes. In the example, both windows are targets, and the selectedItem and the text box of the Child window in the parent window list are bound to the same object in viewModel. When the target of the Child Window is changed, it will notify the source of viewModel, and then the source will notify the target of the main window. Ah, by the way, in general, triggering data changes is "getting out of focus", that is, when you click OK to make the subwindow text box lose focus. However, if you set UpdateSourceTrigger = PropertyChanged during binding, the data will be changed in real time.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.