Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Windows Forms Binding Improvements in .NET 7 for MVVM Support

Windows Forms Binding Improvements in .NET 7 for MVVM Support

The .NET 7 framework includes command binding preview features that aim to modernise Windows Forms applications. These features introduce new properties that allow the UI controls to be bound to view models and commands, like other .NET UI frameworks such as MAUI.

It includes the following changes for the developers:

With these features, .NET Windows Forms developers can use ViewModels or controllers to decouple the UI from the business logic. It also simplifies unit testing the logic and the UI separately. Lastly, it lines up with the models generated by modern ORM tools, such as Entity Framework Core, without any dependencies on DataTable or DataSet classes. The Forms Designer in Visual Studio now supports binding to objects that have properties and commands, creating the necessary bindings and binding collections automatically.

Data binding in Windows Forms .NET 7
(image from Microsoft's DevBlog post about .NET 7 improvements for Windows Forms)

Windows Forms Command property supports the ICommand interface (Execute, CanExecute methods and CanExecuteChanged event). If the button is clicked, the related command’s Execute method is called. If the command’s CanExecute method returns false, the button is disabled. The developers can create their own derived command classes in the ViewModel or leverage the community .NET Toolkit that comes with RelayCommand class and code generators to reduce the boilerplate code to a minimum.

While Windows Forms became popular with business application developers as it greatly simplified the creation of Windows GUI applications, it also allowed for maintenance chaos as the form code-behind classes would now contain a mix of concerns: UI code, business logic code, and even database connection code.

Later UI frameworks for .NET (WPF, UWP, and MAUI) allowed a new design pattern that removed the domain-specific code from the UI (now called a View) and added it to a dedicated component called a ViewModel. In this MVVM pattern (Model-View-ViewModel), the ViewModel is responsible for processing the data and executing business logic code. The ViewModel uses .NET properties to bind to the UI, using property getters and setters. It also leverages the INotifyPropertyChanged interface to signal changes in the properties.

Windows Forms supports binding to a data source which can be either a DataSet/DataTable from ADO.NET or an object (since .NET Framework 2.0). While the object data source could decouple the data from the UI layer, the user UI interactions that weren’t data updates (such as button clicks) were still left to the Windows Forms code-behind files. It was also difficult to propagate data sources to the child controls on the form, as the Windows Forms bindings are control-specific.

In May 2021, there was a new API proposal in the Windows Forms project on GitHub that called for adding binding support for commands to buttons and a centralised DataContext property to a form or user control. By August 2022, the feature was largely done, and it had been released with .NET 7 under preview. It is expected that .NET 8, scheduled for November 2023, will include these features as default.

About the Author

Rate this Article