Skip to content

NavigationView in 2.4 doesn't return the Item anymore in ItemInvoked #2520

@michael-hawker

Description

@michael-hawker

Describe the bug
The 2.4 release broke the ItemInvoked API contract by returning the realized UI Element from the new underlying ItemsRepeater instead of the Item from the underlying MenuItemsSource collection.

Steps to reproduce the bug

Sample Code
    <Page.Resources>
        <DataTemplate x:Key="CategoryTemplate"
                      x:DataType="local:SampleCategory">
            <Grid>
                <TextBlock VerticalAlignment="Center"
                           FontFamily="Segoe UI"
                           FontSize="15px"
                           FontWeight="Normal"
                           Text="{x:Bind Name}" />
            </Grid>
        </DataTemplate>
    </Page.Resources>

    <winui:NavigationView x:Name="NavView"
                          PaneDisplayMode="Top"
                          MenuItemTemplate="{StaticResource CategoryTemplate}"
                          ItemInvoked="NavView_ItemInvoked"
                          SelectionFollowsFocus="Disabled"
                          IsSettingsVisible="True"/>
Code Behind
        private List<SampleCategory> sampleCategories = new List<SampleCategory>()
        {
            new SampleCategory()
            {
                Name = "Controls",
                Samples = new Sample[] {
                    new Sample()
                    {
                        Name = "UniformGrid",
                        Subcategory = "Panels",
                        About = "Some Info...",
                    },
                    new Sample()
                    {
                        Name = "WrapPanel",
                        Subcategory = "Panels",
                        About = "Some Info...",
                    }
                }
            },
            new SampleCategory()
            {
                Name = "Extensions",
                Samples = new Sample[] {
                    new Sample()
                    {
                        Name = "Mouse",
                        About = "Some Info...",
                    },
                    new Sample()
                    {
                        Name = "OnDevice",
                        About = "Some Info...",
                    }
                }
            }
        };

        public MainPage()
        {
            this.InitializeComponent();

            NavView.MenuItemsSource = sampleCategories;
        }

Steps to reproduce the behavior:

  1. Open sample (or re-create from pasted code below)
  2. Run sample
  3. Click on 'Controls' item in NavigationView
  4. Should see message dialog from our working 2.3.x code (but don't)
  5. Note: that we don't get the same result between the previous package and 2.4, this breaks apps trying to upgrade!

See the 2.3.200213001 branch on the same repo. You can switch to that which has the previous NuGet package setup to see the difference in behavior.

Actual behavior
NavigationViewItemInvokedEventArgs.ItemInvoked is a UIElement of the realized data template.

Expected behavior
Should return the underlying Item in the args from the MenuItemsSource (as it did before in 2.3).

Screenshots

Item Invoked for Item (Controls):

  • 2.3.200213001
    image

  • 2.4.2
    image
    Note: Here we're not getting our item as we'd expect!

Item Invoked for Settings (this looks the same and seems fine, but adding for completeness)
  • 2.3.200213001
    image

  • 2.4.2
    image

Version Info

NuGet package version: Microsoft.UI.Xaml 2.4.2

Windows 10 version Saw the problem?
Insider Build (xxxxx)
May 2020 Update (19041) Yes
November 2019 Update (18363)
May 2019 Update (18362)
October 2018 Update (17763)
April 2018 Update (17134)
Fall Creators Update (16299)
Creators Update (15063)
Device form factor Saw the problem?
Desktop Yes
Mobile
Xbox
Surface Hub
IoT

Additional context

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions