> For the complete documentation index, see [llms.txt](https://framework.aic.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://framework.aic.io/technical-guidelines-code-standards-and-tech-stack/wpf-standards/mvvm-standards.md).

# MVVM Standards

## Purpose

MVVM keeps WPF and MAUI applications testable and maintainable.

## Responsibilities

| Component              | Responsibility                                          |
| ---------------------- | ------------------------------------------------------- |
| View                   | Layout, visual states, bindings, controls               |
| ViewModel              | Presentation state, commands, UI workflow orchestration |
| Model / Domain         | Business state and rules                                |
| Application service    | Use case execution                                      |
| Infrastructure service | External systems, storage, platform APIs                |

## View Rules

* XAML should not contain business decisions.
* Code-behind should be limited to UI-specific behaviour.
* Avoid direct service calls from views.
* Bind to ViewModel properties and commands.

## ViewModel Rules

ViewModels should:

* be testable without UI runtime
* expose immutable or observable state
* use async commands for I/O
* handle cancellation where practical
* validate user input
* expose user-friendly error state
* not depend directly on WPF controls or MAUI pages

## Example ViewModel

```csharp
public sealed partial class CaseSearchViewModel : ObservableObject
{
    private readonly ICaseSearchService _caseSearchService;

    [ObservableProperty]
    private string _query = string.Empty;

    [ObservableProperty]
    private bool _isBusy;

    public ObservableCollection<CaseSummary> Results { get; } = new();

    public CaseSearchViewModel(ICaseSearchService caseSearchService)
    {
        _caseSearchService = caseSearchService;
    }

    [RelayCommand]
    private async Task SearchAsync(CancellationToken cancellationToken)
    {
        IsBusy = true;
        try
        {
            var results = await _caseSearchService.SearchAsync(Query, cancellationToken);
            Results.Clear();
            foreach (var result in results)
            {
                Results.Add(result);
            }
        }
        finally
        {
            IsBusy = false;
        }
    }
}
```

## MVVM Checklist

* Can ViewModel be unit tested?
* Are commands asynchronous where needed?
* Is UI state explicit?
* Are errors represented cleanly?
* Is business logic outside the ViewModel?
* Are platform-specific dependencies abstracted?
