Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the WPF reference MVVM framework and how to use it

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I will talk to you about what WPF refers to the MVVM framework and how to use it. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

1.NuGet references the MVVM framework package

After the framework package is introduced, the sample code for the ViewModel layer is created by default in the directory.

two。 The second step is to associate some business code with MainWindow.xaml through context by creating some business code in MainViewModel, while MainWindow is the part that references the business logic through the writing of Binding.

In MainViewModel, add the same class name and student list, which are displayed on the text and list, respectively, and Command is used to bind DataGrid on the double-click command to display the student information of the clicked line by double-clicking:

MainViewModel inherits ViewModelBase, and the inherited parent class is the base class that implements two-way notification in the MVVM framework. By referencing this class, attributes such as it can be added by adding RaisePropertyChanged ().

-add some simulation data for MainViewModel

-Design the UI layer, add a text to display the class name, add a DataGrid to display the student list, and add a bound command (red area) to the DataGrid

On the above red circle code, for a binding syntax, mainly in MouseBinding, MouseAction as the type of event triggered, CommandParameter is the parameter passed by the command, that is, the type of line selected by DataGrid Student.

Command is the Command defined in MainViewModel.

-now that the binding of the front end has been set up and the background code has been added, you only need to associate MainViewModel with MainWindow through the DataContext of MainWindow.

The running effect is as follows:

RaisePropertyChanged () implements dynamic notification updates

+ when updating ClassName through the set accessor, call the RaisePropertyChanged method, and refresh the updated value of the interface.

+ add a UpdateCommand with no parameters, and set it to UpdateText to manually update the ClassName to "Class 3, Senior 3":

+ UI layer adds a simple button to bind the UpdateCommand command in the background

Run the test results:

After reading the above, do you have any further understanding of what WPF refers to the MVVM framework and how to use it? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report