This article is a mirror article of machine translation, please click here to jump to the original article.

View: 20694|Reply: 0

[WPF] WPF RelayCommand

[Copy link]
Posted on 12/18/2017 9:16:49 PM | | |

In commonly used Wpf development, we implement the INotifyPropertyChanged interface in the ViewModel to achieve the purpose of notifying UI changes by triggering the PropertyChanged event.

In the MVVMLight framework, the ViewModel we define here is inherited from ViewModelBase, which is encapsulated in the MvvmLight framework, and it has implemented the INotifyPropertyChanged interface.

Therefore, when defining the ViewModel property, we only need to call RaisePropertyChanged(PropertyName) to notify the property change.



Events are the most important way for the UI to interact with background code in WPF/SL applications, and unlike traditional methods, events are handled mainly by binding to commands in mvvm, so to understand how events are handled in mvvm, you must first be familiar with how commands work.

1. RelayCommand command

WPF/SL commands are created by implementing the ICommand interface. ICommand exposes two methods (Execute and CanExecute) and an event (CanExecuteChanged). Execute executes the action associated with the command. CanExecute determines whether a command can be executed on the current command target. The class that implements the ICommand interface in MvvmLight is RelayCommand, which initializes the Execute and CanExecute methods through the constructor, so the constructor passes in the parameters of the delegate type, and the Execute and CanExecute execute the delegated methods, and the relevant code of RelayCommand is as follows:


2. Comand attribute binding

Simple example: a TextBox and a Button, the TextBox is only available when the content is not empty, and the available Button is clicked to show the TextBox content.




ViewMode:







Previous:android-TableLayout as well as the use of TableRow
Next:The main window opens when the WPF login window is closed
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com