I guess you have created the WindowDLG with the GUIBuilder.exe. I did a test on my end and it is working. The code I have added between the "USER START" and "USER END" marks was left untouched after loading, modifying and saving again the dialog.
But, I can't say what happens when using CubeMX. This tool was created by ST and we are not responsible for.