First, I would like to give some context of the problem: We have a great legacy of WinForms, which we decided to move to WPF. But it is impossible (for many reasons, including business) to completely rewrite the application from scratch, and we will make this migration step by step (one form after another). So now we need to use the windows and WPF controls in the WinForms application.
Today I took the first step in the migration process: I recreated a simple form in WPF (this became Window). The new window that I just recreated in WPF is opened by clicking a button on another form (which is the standard form of WindowsForms). Important note: my Windows is configured for 120 DPI , not 96 by default. Everything was fine until I started the application to see what the new window looks like.
So, here is what I saw: The application started normally and looked as always. I opened the "parent" form and pressed a button to open a new WPF window. After clicking the button, the WPF window opened as expected, but all the visible elements of the application (main window, "parent" form, buttons, toolbars, etc.), including the new WPF window, increased their size, they began to look bigger . That is not all. When I closed the WPF window, nothing happened with the scale - all the elements of the application remained large. Then I closed the “parent” form from which I opened the WPF window. And there was a magic - all elements of the application were resized to their original sizes. Then I again opened the "parent" form and the WPF window - but the scale did not change, all elements of the application retained their sizes. I repeated “open-close” from the “parent” form and the WPF window many times (I didn’t restart the application during one run of the application) - without resizing the elements.
After that, I did some research and came to the following: If I put any code that uses any type of WPF collectors in the Program.cs file before the main application is initialized, the application will always draw all its elements more than they were before, but when you open the WPF window, there will be no scale changes. An example of the Program.cs file (in my case it was called EntryPoint):
internal sealed class EntryPoint {
I added the line marked **** (sorry, I can’t figure out how to make the line bold in the code snippet). During several tests, I realized that it doesn’t matter which code I add - it can create a new System.Windows.Window object without even showing it, or any other line of code that uses any type of WPF compiler.
So, the main question: what can cause the described behavior? If anyone came across this problem or had any ideas, it would be great if you shared your thoughts.
Thank you for reading this not so short question.