winforms-mvvm

Implement MVVM pattern in WinForms on .NET 8+ with Commands, DataContext, and testable ViewModels.

1|1|Updated Jun 12, 2022
One-click install
npx skills add https://github.com/KlausLoeffelmann/WinFormsDevTools --skill winforms-mvvm-klausloeffelmann
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: winforms-mvvm
Source: https://github.com/KlausLoeffelmann/WinFormsDevTools/tree/main/.github/skills/winforms-mvvm
Command: npx skills add https://github.com/KlausLoeffelmann/WinFormsDevTools --skill winforms-mvvm-klausloeffelmann

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires CommunityToolkit.Mvvm.

What problem does it solve? Traditional WinForms applications mix business logic with UI event handlers, making code untestable and hard to maintain. This Skill guides you through implementing the MVVM pattern in WinForms on .NET 8+, separating business logic into ViewModels that can be unit tested without creating Forms. ## Core Features & Use Cases - Command and DataContext Binding: Bind ButtonBase.Command, ToolStripItem.Command, and the cascading Control.DataContext property introduced in .NET 8 to eliminate Click event handlers. - MVVM Community Toolkit Integration: Generate observable properties and RelayCommands with source generators in a separate ViewModels class library, plus .datasource files for Designer support. - Migration and Testing Guidance: Convert event-handler-based Forms to MVVM, adapt ObservableCollection to BindingList, and unit test ViewModels with xUnit and Moq. - Use Case: You have a legacy PersonEditForm with database calls inside BtnSave_Click. Use this Skill to extract the logic into a PersonViewModel with a SaveCommand, bind it via BindingSource, and write unit tests that verify the save behavior without launching any UI. ## Quick Start Use the winforms-mvvm skill to refactor my Form's Click event handlers into a testable ViewModel with Commands and data binding.

Frequently Asked Questions about winforms-mvvm

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I implement MVVM in WinForms on .NET 8?▼

Create ViewModels in a separate class library using the CommunityToolkit.Mvvm package with ObservableObject and RelayCommand attributes. Bind controls through BindingSource, use ButtonBase.Command for actions, and set Control.DataContext for hierarchical binding.

How to bind a Button click to a ViewModel command in WinForms?▼

Add a Binding on the button's Command property targeting the ViewModel's generated command, for example new Binding("Command", bindingSource, "SaveCommand", true). The button automatically enables or disables based on CanExecute, and no Click event handler is needed.

Why are MVVM Toolkit properties and commands not generated in my WinForms project?▼

The MVVM Toolkit source generators do not cascade across projects, so ViewModels must live in a separate class library referencing CommunityToolkit.Mvvm. Also verify the ViewModel class is declared partial and rebuild to trigger the generators.

Can I bind ObservableCollection directly to a DataGridView in WinForms?▼

No, ObservableCollection uses INotifyCollectionChanged while WinForms requires IBindingList. Use an ObservableBindingList adapter to bridge the two, and dispose the adapter when the Form closes.

Does WinForms DataContext work like WPF data binding?▼

Control.DataContext in .NET 8+ is an ambient property that flows down the control hierarchy, similar to WPF. Child controls react via OnDataContextChanged, but you still wire actual bindings through BindingSource rather than WPF-style markup.

Why is my button not disabling when the command CanExecute changes?▼

The command state is not reevaluated automatically when related properties change. Call NotifyCanExecuteChanged on the generated command inside the partial OnPropertyChanged hook, such as OnSelectedPersonChanged, to refresh the button state.