Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

What's new in Windows Forms for .NET 8

Feedback

In this article

This article describes some of the new Windows Forms features and enhancements in .NET 8.

There are a few breaking changes you should be aware of when migrating from .NET Framework to .NET 8. For more information, seeBreaking changes in Windows Forms.

Data binding improvements

A new data binding engine was in preview with .NET 7, and is now fully enabled in .NET 8. Though not as extensive as the existing Windows Forms data binding engine, this new engine is modeled after WPF, which makes it easier to implement MVVM design principles.

The enhanced data binding capabilities make it simpler to fully utilize the MVVM pattern and employ object-relational mappers from ViewModels in Windows Forms. This reduces the amount of code in code-behind files. More importantly, it enables code sharing between Windows Forms and other .NET GUI frameworks like WPF, UWP/WinUI, and .NET MAUI. It's important to note that while the previously mentioned GUI frameworks use XAML as a UI technology, XAML isn't coming to Windows Forms.

TheIBindableComponent interface and theBindableComponent class drive the new binding system.Control implements the interface and provides new data binding capabilities to Windows Forms.

Button commands

Button commands were in preview with .NET 7, and is now fully enabled in .NET 8. Similar to WPF, the instance of an object that implements theICommand interface can be assigned to the button'sCommand property. When the button is clicked, the command is invoked.

An optional parameter can be provided when the command is invoked, by the specifying a value for the button'sCommandParameter property.

TheCommand andCommandParameter properties are set in the designer through theProperties window, under(DataBindings), as illustrated by the following image.

The Visual Studio properties window highlighting a Windows Forms' Button's Command and CommandParameter properties.

Buttons also listen to theICommand.CanExecuteChanged event, which causes the control to query theICommand.CanExecute method. When that method returnstrue, the control is enabled; the control is disabled whenfalseis returned.

Visual Studio DPI improvements

Visual Studio 2022 17.8 Introduces DPI-unaware designer tabs. Previously, the Windows Designer tab in Visual Studio ran at the DPI of Visual Studio. This causes problems when you're designing a DPI-unaware Windows Forms app. Now you can ensure that the designer runs at the same scale as you want the app to run, either DPI-aware or not. Before this feature was introduced, you had to run Visual Studio in DPI-unaware mode, which made Visual Studio itself blurry when scaling was applied in Windows. Now you can leave Visual Studio alone and let the designer run DPI-unaware.

You can enable the DPI-unaware designer for the Windows Forms project by adding<ForceDesignerDPIUnaware> to the project file, and setting the value totrue.

<PropertyGroup>  <OutputType>WinExe</OutputType>  <TargetFramework>net8.0-windows</TargetFramework>  <Nullable>enable</Nullable>  <UseWindowsForms>true</UseWindowsForms>  <ImplicitUsings>enable</ImplicitUsings>  <ForceDesignerDPIUnaware>true</ForceDesignerDPIUnaware>  <ApplicationHighDpiMode>DpiUnawareGdiScaled</ApplicationHighDpiMode></PropertyGroup>

Important

Visual Studio reads this setting when the project is loaded, and not when it's changed. After changing this setting, unload and reload your project to get Visual Studio to respect it.

High DPI improvements

High DPI rendering withPerMonitorV2 has been improved:

  • Correctly scale nested controls. For example, a button that's in a panel, which is placed on a tab page.

  • ScaleForm.MaximumSize andForm.MinimumSize properties based on the current monitor DPI settings.

    Starting with .NET 8, this feature is enabled by default and you need to opt out of it to revert to the previous behavior.

    To disable the feature, addSystem.Windows.Forms.ScaleTopLevelFormMinMaxSizeForDpi to theconfigProperties setting inruntimeconfig.json, and set the value to false:

    {  "runtimeOptions": {    "tfm": "net8.0",    "frameworks": [        ...    ],    "configProperties": {      "System.Windows.Forms.ScaleTopLevelFormMinMaxSizeForDpi": false,    }  }}

Miscellaneous improvements

Here are some other notable changes:

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?