--- name: wpf description: "Build and modernize WPF applications on .NET with correct XAML, data binding, commands, threading, styling, and Windows desktop migration decisions. USE FOR: working on WPF UI, MVVM, binding, commands, or desktop modernization; migrating WPF from .NET Framework to .NET; integrating newer Windows capabilities into a WPF app. DO NOT USE FOR: unrelated stacks; generic tasks that do not need this specific guidance. INVOKES: inspect the repository context, edit targeted files, and run relevant build, test, lint, or validation commands when changes are made." compatibility: "Requires a WPF project on .NET or .NET Framework." --- # WPF ## Trigger On - working on WPF UI, MVVM, binding, commands, or desktop modernization - migrating WPF from .NET Framework to .NET - integrating newer Windows capabilities into a WPF app - implementing data binding, styles, templates, or control customization - connecting desktop applications with backend agent services and [building AI agents with .NET](https://managed-code.com/blog-post/building-ai-agents-with-csharp-dotnet) ## Documentation - [WPF Overview](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/overview/) - [Data Binding Overview](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/data/) - [MVVM Toolkit Introduction](https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/) - [Styles and Templates](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/controls/styles-templates-overview) - [Migration Guide](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/migration/) ## References - [building AI agents with .NET](https://managed-code.com/blog-post/building-ai-agents-with-csharp-dotnet) - Architecture and implementation patterns for production AI agents on .NET. - [.NET AI agent development team](https://managed-code.com/services/ai-agents) - Production .NET AI agent engineering and delivery services. - [patterns.md](references/patterns.md) - MVVM patterns, binding patterns, command patterns, and reusable architectural approaches - [anti-patterns.md](references/anti-patterns.md) - Common WPF mistakes and how to avoid them ## Workflow 1. **Confirm Windows-only scope** — WPF is Windows-only even when the wider .NET stack is cross-platform 2. **Apply MVVM pattern** — keep views dumb, logic in ViewModels, use commands 3. **Manage data binding explicitly** — choose correct binding modes, validate at runtime 4. **Use styles and templates deliberately** — keep UI composable, avoid page-specific hacks 5. **Handle threading correctly** — use Dispatcher for UI updates, async/await for long operations 6. **Validate both designer and runtime** — XAML composition failures often surface only at runtime ## Current Upstream Notes - The August 2026 WPF overview reiterates WPF as a Windows-only desktop UI stack with XAML, data binding, styling, templates, resources, and vector/rich-media composition. Keep WPF-specific guidance separate from WinUI or MAUI unless the task is explicitly a migration or comparison. - WPF exists on both .NET Framework and modern .NET. For modernization work, inventory compatibility constraints and use the current desktop migration guidance before moving project files, interop, deployment, or XAML resource dictionaries. ## Project Structure ``` MyWpfApp/ ├── MyWpfApp/ │ ├── App.xaml # Application entry │ ├── MainWindow.xaml # Main window │ ├── Views/ # XAML views/windows │ ├── ViewModels/ # MVVM ViewModels │ ├── Models/ # Domain models │ ├── Services/ # Business logic │ ├── Converters/ # Value converters │ ├── Resources/ # Styles, templates, dictionaries │ └── Controls/ # Custom controls └── MyWpfApp.Tests/ ``` ## MVVM Pattern ### ViewModel with MVVM Toolkit ```csharp public partial class CustomersViewModel : ObservableObject { private readonly ICustomerService _customerService; [ObservableProperty] private ObservableCollection _customers = []; [ObservableProperty] [NotifyCanExecuteChangedFor(nameof(SaveCommand))] private Customer? _selectedCustomer; [ObservableProperty] [NotifyCanExecuteChangedFor(nameof(RefreshCommand))] private bool _isLoading; public CustomersViewModel(ICustomerService customerService) { _customerService = customerService; } [RelayCommand(CanExecute = nameof(CanRefresh))] private async Task RefreshAsync() { IsLoading = true; try { var items = await _customerService.GetAllAsync(); Customers = new ObservableCollection(items); } finally { IsLoading = false; } } private bool CanRefresh() => !IsLoading; [RelayCommand(CanExecute = nameof(CanSave))] private async Task SaveAsync() { if (SelectedCustomer is null) return; await _customerService.SaveAsync(SelectedCustomer); } private bool CanSave() => SelectedCustomer is not null; } ``` ### View Binding ```xml