Binding to Commands

Controls that carry out an action, such as Button have a Command property which can be bound to an ICommand. When the control is activated (e.g. when a button is clicked) the ICommand.Execute method will be called.

A good implementation of ICommand can be found in ReactiveUI's ReactiveCommand. If you've created your application using the Avalonia MVVM Application template then this will be available by default. See the ReactiveUI documentation for more information.

An example:

namespace Example
{
    public class MainWindowViewModel : ViewModelBase
    {
        public MainWindowViewModel()
        {
            DoTheThing = ReactiveCommand.Create(RunTheThing);
        }

        public ReactiveCommand<Unit, Unit> DoTheThing { get; }

        void RunTheThing()
        {
            // Code for executing the command here.
        }
    }
}
<Window xmlns="https://github.com/avaloniaui">
    <Button Command="{Binding DoTheThing}">Do the thing!</Button>
<Window>

CommandParameter

You can also pass a parameter to the command using the CommandParameter property:

namespace Example
{
    public class MainWindowViewModel : ViewModelBase
    {
        public MainWindowViewModel()
        {
            DoTheThing = ReactiveCommand.Create<int>(RunTheThing);
        }

        public ReactiveCommand<int, Unit> DoTheThing { get; }

        void RunTheThing(int parameter)
        {
            // Code for executing the command here.
        }
    }
}
<Window xmlns="https://github.com/avaloniaui">
    <Button Command="{Binding DoTheThing}" CommandParameter="42">Do the thing!</Button>
<Window>

Like any other property, CommandParameter can also be bound.

Binding To Methods

Sometimes you just want to call a method when a button is clicked without the ceremony of creating a command. You can do that too!

namespace Example
{
    public class MainWindowViewModel : ViewModelBase
    {
        public void RunTheThing(int parameter)
        {
            // Code for executing the command here.
        }
    }
}
<Window xmlns="https://github.com/avaloniaui">
  <Button Command="{Binding RunTheThing}" CommandParameter="42">Do the thing!</Button>
<Window>