CommunityToolkit.Mvvm8.1 viewmodel use – old style (2)
CommunityToolkit.Mvvm8.1 viewmodel usage – old style (2) Navigation of this series of articles https://www.cnblogs.com/aierong/p/17300066.html https://github.com/aierong/WpfDemo (self-Demo address) 0.Description CommunityToolkit.Mvvm8.1 has a major update function: the source generator function, which greatly simplifies our mvvm code But this article summarizes the original writing method first, and then summarizes the source generator function in the next article 1. Model definition Must inherit: ObservableObject 2. viewmodel code implementation A few key points: SetProperty is to assign values to properties and notify change notificationsButtonClickCommand.NotifyCanExecuteChanged(); //Notification command has changedRelayCommand ButtonClickCommand //Definition command namespace WpfDemoNet6.Demo { public class DataViewModel1 : ObservableObject { private string title = “hello”; public string Title { get { return title; } set { //title = value; //PropertyChanged?.Invoke( this , new PropertyChangedEventArgs( “Name” ) ); //SetProperty is equivalent to setting the value, and the PropertyChanged notification call SetProperty( ref title , value ); } } private bool isEnabled = false; /// /// Is it possible to use /// public bool IsEnabled { get => isEnabled; set { SetProperty( ref isEnabled , value ); // Notify that the command has changed ButtonClickCommand. NotifyCanExecuteChanged(); } } /// /// Order /// public RelayCommand ButtonClickCommand { get; } public DataViewModel1 () { //The first parameter of RelayCommand…