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:
|