๐งฑ MVVM in WPF
Windows Presentation Foundation (WPF) fully supports the Model-View-ViewModel (MVVM) pattern, which promotes a clear separation of concerns between the UI and business logic.
The MVVM infrastructure used with Atc.Wpf comes from Atc.XamlToolkit, providing a robust foundation for implementing MVVM effectively, reducing boilerplate code and simplifying development.
โ๏ธ Features
The MVVM infrastructure is provided by the Atc.XamlToolkit and Atc.XamlToolkit.Wpf packages, which you add to your project directly (Atc.Wpf does not depend on them):
| ๐งฉ Component | ๐ฆ Source Namespace | ๐ Description |
|---|---|---|
ViewModelBase |
Atc.XamlToolkit.Mvvm | A base class for ViewModels. |
MainWindowViewModelBase |
Atc.XamlToolkit.Mvvm | A base class for the main window ViewModel. |
ViewModelDialogBase |
Atc.XamlToolkit.Mvvm | A base class for dialog ViewModels. |
ObservableObject |
Atc.XamlToolkit.Mvvm | A base class for observable objects implementing INotifyPropertyChanged. |
RelayCommand |
Atc.XamlToolkit.Command | A command supporting CanExecute. |
RelayCommand<T> |
Atc.XamlToolkit.Command | A command with a generic parameter and CanExecute. |
RelayCommandAsync |
Atc.XamlToolkit.Command | An asynchronous command supporting CanExecute. |
RelayCommandAsync<T> |
Atc.XamlToolkit.Command | An asynchronous command with a generic parameter and CanExecute. |
๐ For detailed information about source generators and commands, refer to the ViewModel Source Generation documentation.
๐ Migrating an existing project from hand-rolled INotifyPropertyChanged / RelayCommand boilerplate? See the Migration guide.
๐ฆ For more information about the MVVM infrastructure, see the Atc.XamlToolkit repository.
๐ Getting started using ViewModelBase
Below is a simple example demonstrating how to create a ViewModel using ViewModelBase:
public class MyViewModel : ViewModelBase
{
private string myProperty;
public string MyProperty
{
get => myProperty;
set
{
if (myProperty == value)
{
return;
}
myProperty = value;
RaisePropertyChanged();
}
}
}