Skip to main content
Version: 0.10.x

The Model-View-ViewModel Pattern

As well as writing code in code-behind, Avalonia supports using the Model-View-ViewModel pattern (or MVVM). MVVM is a common way to structure UI applications that separates view logic from application logic in a way that allows your applications to become unit-testable.

MVVM relies upon Avalonia's binding capabilities to separate your application into a View layer which displays standard Avalonia windows and controls, and a ViewModel layer which defines the functionality of the application independently of Avalonia itself.

The following example shows the code from the previous example implemented using the MVVM pattern:

<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="AvaloniaApplication1.MainWindow"
Title="Window with Button"
Width="250" Height="100">

<!-- Add button to window -->
<Button Content="{Binding ButtonText}" Command="{Binding ButtonClicked}"/>
</Window>

In this example, the code-behind assigns the Window's DataContext property to an instance of MainWindowViewModel. The XAML then uses an Avalonia {Binding} to bind the Button's Content property to the ButtonText property on the MainWindowViewModel. It also binds the Button's Command property to the ButtonClicked method on the MainWindowViewModel.

When the Button is clicked it invokes its Command, causing the bound MainWindowViewModel.ButtonClicked method to be called. This method then sets the ButtonText property which raises the INotifyPropertyChanged.PropertyChanged event, causing the Button to re-read its bound value and update the UI.