[フレーム]
BT

InfoQ Software Architects' Newsletter

A monthly overview of things you need to know as an architect or aspiring architect.

View an example

We protect your privacy.

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Unlock the full InfoQ experience

Unlock the full InfoQ experience by logging in! Stay updated with your favorite authors and topics, engage with content, and download exclusive resources.

Log In
or

Don't have an InfoQ account?

Register
  • Stay updated on topics and peers that matter to youReceive instant alerts on the latest insights and trends.
  • Quickly access free resources for continuous learningMinibooks, videos with transcripts, and training materials.
  • Save articles and read at anytimeBookmark articles to read whenever youre ready.

Topics

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

Feb 21, 2023 2 min read

Write for InfoQ

Feed your curiosity. Help 550k+ global
senior developers
each month stay ahead.
Get in touch

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

Edin Kapić

Show moreShow less

Rate this Article

Adoption
Style

This content is in the .NET topic

Related Topics:

Related Content

The InfoQ Newsletter

A round-up of last week’s content on InfoQ sent out every Tuesday. Join a community of over 250,000 senior developers. View an example

We protect your privacy.

BT

AltStyle によって変換されたページ (->オリジナル) /