dotnet / wpf

WPF is a .NET Core UI framework for building Windows desktop applications.
MIT License
7.1k stars 1.17k forks source link

Behavior change from .NET Core SDK 3.0.101 to 3.1.100 when generating MenuItems in WPF #2404

Open davikor opened 4 years ago

davikor commented 4 years ago

I write a WPF app targeting .NET Framework 4.7.2 and added a global.json to control which version of dotnet CLI tools to be used in building and deploying.

In this app I define a menu of MenuItems of which some contain MenuItems as their children (a submenu). In one place I set the UsesItemContainerTemplate-property of the parent to true and define an ItemContainerTemplate containing a MenuItem at the root. As expected the parents ItemContainerGenerator respects this template and does not generate a menu item around this parents children.

After I switched the SDK version in global.json from 3.0.101 to 3.1.100 I noticed that this behavior changed. My submenuitems now are wrapped into an extra MenuItem.

I created a super-simple example-app that reproduces this issue. It can be found in this GitHub repo alongside with screenshots of the issue.

After checkout you can build and run this project in VS16.5 and it will behave as (I) expect it. Then you can change the SDK version in global.json to 3.1.100 and rebuild it in VS. When you run it now, you will see the unexpected result.

On a side note: When running dotnet clean/run from the console, the unexpected behavior always occurs, regardless of the SDK version defined in global.json. Unfortunately my knowledge of the dotnet / MSBuild system is not sufficient to further investigate here.

Also, I am not 100% sure whether this is the right place to post this issue. If there is a place better suited for my issue, please let me know and I will move this issue.

SusLes commented 4 years ago

@livarcocc

livarcocc commented 4 years ago

This seems like a WPF issue. Adding @rladuca and @vatsan-madhavan

vatsan-madhavan commented 4 years ago

Thanks - I’m transferring it over to wpf.

gomathip02 commented 4 years ago

@arpitmathur - is this related to the menu accessibility changes for 3.1?

davikor commented 4 years ago

some additional information: When compiled with SDK version 3.0.101 the compiler adds the following x:Key attribute to the ItemContainerTemplate (in BAML): x:Key="{ItemContainerTemplateKey {x:Type local:MenuItemViewModel}}"

When compiled with SDK 3.1.100 (or using dotnet build instead of Visual Studio's MSBuild) the added x:Key attribute looks like this: x:Key="{DataTemplateKey {x:Type local:MenuItemViewModel}}"

(I checked my decompiled assembly for differences)

As a workaround, I tried to add the correct ItemTemplateKey to my XAML but it gets replaced during build. That is the only (obvious) difference I could spot. I also checked the platform on which both assemblies were run and both were run on the same runtime (.NET Framework 4.8.4075.0) so my guess is that the behavior change is somehow related to this key.

inforithmics commented 4 years ago

I have the same issue targeting NET472.

Compiling the Project with the Legacy Project Format targeting 4.7.2 NET Framework BAML: x:Key="{ItemContainerTemplateKey {x:Type ...

Converting the Project to the new Project Format targeting 4.7.2 NET Framework with .NET Core SDK 3.1.100 BAML: x:Key="{DataTemplateKey {x:Type

mcdis commented 4 years ago

Reproduced the same bug. Waiting for fix...

mdonoughe commented 4 years ago

Workaround: this seems to work if you remove DataType="{x:Type …}" and put x:Key="{ItemContainerTemplateKey {x:Type …}}" instead.