New to Claude Skills? Learn how to install them →

Mgithub on GitHub

MVVM Toolkit

OfficialFree

Streamline ViewModel development with source generators.

by github37.7k stars on github/awesome-copilot
4 views
Updated Aug 10, 2026
Get this skill

Free · Opens the source repo

What MVVM Toolkit does

The MVVM Toolkit is designed for developers working with the Model-View-ViewModel (MVVM) architectural pattern in .NET applications. It simplifies the creation and management of ViewModels, properties, commands, and validation through the use of source generators. By utilizing attributes like [ObservableProperty] and [RelayCommand], developers can automatically generate boilerplate code, reducing the need for manual implementation and minimizing errors. This toolkit is particularly useful in applications built with frameworks such as WPF, WinUI 3, MAUI, Uno, and Avalonia.

The toolkit includes essential components such as ObservableObject, ObservableValidator, and RelayCommand, which facilitate the implementation of observable properties and commands in a straightforward manner. For instance, using [ObservableProperty] on a private field automatically creates a public property that implements INotifyPropertyChanged, allowing the UI to react to changes in the data model seamlessly. This feature is critical for maintaining a responsive user interface in MVVM applications.

In addition to the core functionalities, the MVVM Toolkit offers companion skills like mvvm-toolkit-messenger for pub/sub patterns and mvvm-toolkit-di for dependency injection integration. These extensions enable developers to build more complex applications while adhering to best practices in software design. The toolkit's structured approach to ViewModel patterns and validation ensures that developers can implement robust and maintainable code efficiently.

Overall, the MVVM Toolkit is an invaluable resource for .NET developers looking to enhance their MVVM applications with minimal overhead. By leveraging source generators and established patterns, it allows for a more streamlined development process that focuses on functionality rather than repetitive code writing.

When to use it

Use the MVVM Toolkit when developing .NET applications that follow the MVVM pattern and require efficient property and command management.

When not to use it

Avoid using this toolkit if your application does not utilize the MVVM pattern or if you prefer manual implementation of ViewModel logic.

What you can build with it

Creating a Simple ViewModel

Use the MVVM Toolkit to quickly set up a ViewModel with observable properties using [ObservableProperty].

Implementing Commands

Leverage [RelayCommand] to easily create commands for user interactions without boilerplate code.

Validating User Input

Utilize the validation features of the toolkit to ensure user input meets specified criteria in forms.

How to install MVVM Toolkit

View source

1. Install with the skills CLI

npx skills add github/awesome-copilot/mvvm-toolkit --agent claude-code

2. Or install it manually

Download the skill folder and drop it into ~/.claude/skills/ for all projects, or .claude/skills/ to scope it to one repo. Restart Claude Code so it picks up the new skill.

Anthropic's agentic coding CLI, and the reference implementation of Agent Skills. Drop a skill folder into ~/.claude/skills and Claude Code loads it automatically whenever a task matches the skill's description. Claude Code docs

Inside SKILL.md

Written by github

CommunityToolkit.Mvvm (core)

Use this skill when authoring or reviewing ViewModels, properties, commands, or validation in apps that use CommunityToolkit.Mvvm 8.x.

Companion skills. Load mvvm-toolkit-messenger for IMessenger pub/sub patterns. Load mvvm-toolkit-di for Microsoft.Extensions.DependencyInjection integration.

Quick recap. [ObservableProperty] on private fields in partial classes; [RelayCommand] on instance methods; inherit from ObservableObject (or ObservableValidator for input forms, ObservableRecipient when using IMessenger).


Package & setup

<ItemGroup>
  <PackageReference Include="CommunityToolkit.Mvvm" Version="8.*" />
</ItemGroup>

Targets: netstandard2.0, netstandard2.1, net6.0+. Works on .NET, .NET Framework, Mono. Source generators ship in the same NuGet — no extra analyzer reference required.

Namespaces:

using CommunityToolkit.Mvvm.ComponentModel;   // ObservableObject, [ObservableProperty]
using CommunityToolkit.Mvvm.Input;             // [RelayCommand], RelayCommand, AsyncRelayCommand

Universal rule. Every type that uses [ObservableProperty] or [RelayCommand] — and every enclosing type, if nested — must be declared partial. Without it, the generators emit MVVMTK0008 / MVVMTK0042.


Source generators cheat sheet

AttributeApplied toGenerates
[ObservableProperty]private fieldPublic INotifyPropertyChanged property + OnXxxChanging/OnXxxChanged partial-method hooks
[NotifyPropertyChangedFor(nameof(Other))]observable fieldAlso raises PropertyChanged for the listed property
[NotifyCanExecuteChangedFor(nameof(MyCommand))]observable fieldCalls MyCommand.NotifyCanExecuteChanged() on change
[NotifyDataErrorInfo]observable field on ObservableValidatorCalls ValidateProperty(value) from the setter
[NotifyPropertyChangedRecipients]observable field on ObservableRecipientBroadcast(old, new) after the change
[RelayCommand]instance methodLazy RelayCommand / AsyncRelayCommand exposed as IRelayCommand / IAsyncRelayCommand
[RelayCommand(CanExecute = nameof(CanX))]instance methodWires CanExecute to a method or property
[RelayCommand(IncludeCancelCommand = true)]async method with CancellationTokenAlso generates XxxCancelCommand
[RelayCommand(AllowConcurrentExecutions = true)]async methodAllows queued/parallel invocations (default disables while running)
[RelayCommand(FlowExceptionsToTaskScheduler = true)]async methodSurfaces exceptions via ExecutionTask instead of awaiting and rethrowing
[property: SomeAttr]observable field or [RelayCommand] methodForwards SomeAttr onto the generated property (e.g., [JsonIgnore])

Naming. Field name / _name / m_nameName. Method LoadAsyncLoadCommand (the Async suffix is stripped; a leading On is also stripped).

See references/source-generators.md for the full attribute reference with generated-code samples.


ViewModel patterns

Simple observable property

public partial class ContactViewModel : ObservableObject
{
    [ObservableProperty]
    private string? name;
}

Hooks: OnXxxChanging / OnXxxChanged

[ObservableProperty]
private string? name;

partial void OnNameChanged(string? value) =>
    Logger.LogInformation("Name changed to {Name}", value);

Both single-arg (value) and two-arg (oldValue, newValue) overloads are available. Implement only the ones you need; unimplemented hooks are elided by the compiler (zero runtime cost).

Dependent properties + dependent commands

[ObservableProperty]
[NotifyPropertyChangedFor(nameof(FullName))]
[NotifyCanExecuteChangedFor(nameof(SaveCommand))]
private string? firstName;

[ObservableProperty]
[NotifyPropertyChangedFor(nameof(FullName))]
[NotifyCanExecuteChangedFor(nameof(SaveCommand))]
private string? lastName;

public string FullName => $"{FirstName} {LastName}".Trim();

Wrapping a non-observable model

public sealed class ObservableUser(User user) : ObservableObject
{
    public string Name
    {
        get => user.Name;
        set => SetProperty(user.Name, value, user, (u, n) => u.Name = n);
    }
}

Pass a static lambda (no captured state) to keep the call allocation-free.


Commands

[RelayCommand]
private void Refresh() => Items.Reset();

[RelayCommand]
private async Task LoadAsync()
{
    foreach (var item in await service.GetItemsAsync())
        Items.Add(item);
}

[RelayCommand(IncludeCancelCommand = true)]
private async Task DownloadAsync(CancellationToken token)
{
    await using var stream = await http.GetStreamAsync(url, token);
    // ...
}

[RelayCommand(CanExecute = nameof(CanSave))]
private Task SaveAsync() => repo.SaveAsync(Name!);

private bool CanSave() => !string.IsNullOrWhiteSpace(Name);

Reach for manual RelayCommand / AsyncRelayCommand constructors only when you must own the command's lifetime explicitly or compose it from non-trivial sources. The attribute style covers ~95% of cases.

See references/relaycommand-cookbook.md for sync / async / cancellable / concurrency / error-surfacing recipes.


Base class selection

Base classUse when
ObservableObjectDefault. INotifyPropertyChanged + INotifyPropertyChanging + SetProperty overloads + SetPropertyAndNotifyOnCompletion for Task properties
ObservableValidatorThe VM needs INotifyDataErrorInfo (forms, settings input)
ObservableRecipientThe VM sends or receives IMessenger messages — see the mvvm-toolkit-messenger skill

C# is single-inheritance: ObservableValidator and ObservableRecipient both extend ObservableObject, so combining them requires composition (e.g., inject IMessenger into an ObservableValidator).


Validation

using System.ComponentModel.DataAnnotations;

public sealed partial class RegistrationViewModel : ObservableValidator
{
    [ObservableProperty]
    [NotifyDataErrorInfo]
    [Required, MinLength(2), MaxLength(100)]
    private string? name;

    [ObservableProperty]
    [NotifyDataErrorInfo]
    [Required, EmailAddress]
    private string? email;

    [RelayCommand]
    private void Submit()
    {
        ValidateAllProperties();
        if (HasErrors) return;
        // submit...
    }
}

Other entry points: TrySetProperty, ValidateProperty(value, name), ClearAllErrors(), GetErrors(propertyName). Custom rules support [CustomValidation] methods and custom ValidationAttribute subclasses.

See references/validation.md for the full validator surface area.


Top pitfalls

  1. Forgetting partial. Class (and every enclosing type) must be partial. Compile error MVVMTK0008 / MVVMTK0042.
  2. PascalCase field name. [ObservableProperty] private string Name; collides with the generated property. Use name, _name, or m_name.
  3. async void on [RelayCommand]. The generator only wraps Task-returning methods as IAsyncRelayCommand. async void becomes a sync RelayCommand and exceptions are unobserved. Always return Task.
  4. Forgetting [NotifyCanExecuteChangedFor]. The Save button stays disabled even though CanSave() would now return true.
  5. Mutating the same reference held by an [ObservableProperty] field. EqualityComparer<T>.Default returns true, no notification fires. Replace the instance instead of mutating it.

For the full diagnostic table (MVVMTK0xxx) and more pitfalls, see references/troubleshooting.md.


End-to-end mini walkthrough

A two-pane Notes app demonstrating generators + commands + [NotifyCanExecuteChangedFor]:

public sealed partial class NoteViewModel(INotesService notes,
    IMessenger messenger) : ObservableRecipient(messenger)
{
    [ObservableProperty]
    [NotifyCanExecuteChangedFor(nameof(SaveCommand))]
    [NotifyCanExecuteChangedFor(nameof(DeleteCommand))]
    private string? filename;

    [ObservableProperty]
    [NotifyCanExecuteChangedFor(nameof(SaveCommand))]
    private string? text;

    [RelayCommand(CanExecute = nameof(CanSave))]
    private Task SaveAsync()
    {
        Messenger.Send(new NoteSavedMessage(Filename!));
        return notes.SaveAsync(Filename!, Text!);
    }

    [RelayCommand(CanExecute = nameof(CanDelete))]
    private Task DeleteAsync() => notes.DeleteAsync(Filename!);

    private bool CanSave() =>
        !string.IsNullOrWhiteSpace(Filename) && !string.IsNullOrEmpty(Text);
    private bool CanDelete() => !string.IsNullOrWhiteSpace(Filename);
}

For the full sample (DI wiring, View code-behind, XAML, unit tests), see references/end-to-end-walkthrough.md.


References & companion skills

TopicWhere
Source generator attribute referencereferences/source-generators.md
RelayCommand recipesreferences/relaycommand-cookbook.md
Validation deep divereferences/validation.md
Full Notes-app walkthroughreferences/end-to-end-walkthrough.md
MVVMTK0xxx diagnostics & pitfallsreferences/troubleshooting.md
Messenger pub/subCompanion skill: mvvm-toolkit-messenger
Microsoft.Extensions.DependencyInjection wiringCompanion skill: mvvm-toolkit-di

External sources:

Frequently asked questions about MVVM Toolkit

Similar skills