Search code examples
uwpuwp-xamlnavigationview

Navigationview Set SelectedItem for sub-menu item in UWP app


I trying to set default selected item for navigationview by

DashboardMenuItem.IsExpanded = true;
 Microsoft.UI.Xaml.Controls.NavigationViewItem selectedItem =(Microsoft.UI.Xaml.Controls.NavigationViewItem)DashboardMenuItem.MenuItems[0];
NavView.SelectedItem = selectedItem;

Here is my XAML

<MUXC:NavigationView.MenuItems>
                <MUXC:NavigationViewItem
                    x:Name="DashboardMenuItem"
                    Content="{x:Bind DashboardLabel}"
                    Foreground="#FFFFFF"
                    ToolTipService.ToolTip="{x:Bind DashboardLabel}">
                    <MUXC:NavigationViewItem.Icon>
                        <FontIcon FontFamily="Segoe MDL2 Assets" Glyph="&#xE8A9;" />
                    </MUXC:NavigationViewItem.Icon>
                    <MUXC:NavigationViewItem.MenuItems>
                        <MUXC:NavigationViewItem
                            x:Name="ListofPersonMenuItem"
                            Content="{x:Bind ListofPersonLabel}"
                            Foreground="#FFFFFF"
                            ToolTipService.ToolTip="{x:Bind ListofPersonLabel}" />
                        <MUXC:NavigationViewItem
                            x:Name="ListofDiedPersonsMenuItem"
                            Content="{x:Bind ListofDiedPersonsLabel}"
                            Foreground="#FFFFFF"
                            ToolTipService.ToolTip="{x:Bind ListofDiedPersonsLabel}" />
                    </MUXC:NavigationViewItem.MenuItems>
                </MUXC:NavigationViewItem>

But my menu item just have a highlight background only, still don't have the left bar like when we select by clicking. Picture link below (sorry because I cannot post the image yet)

https://i.gyazo.com/f2953c8f092534ea26ceb7fef0120de2.png

So, do you have any suggestion for this please?

Thank in advance


Solution

  • Navigationview Set SelectedItem for sub-menu item in UWP app

    During the testing, the problem is expend animation block select animation that make item indicator dismiss. Currently we have a workaround that add a task delay before set SelectedItem. It will do select animation after DashboardMenuItem expend.

    DashboardMenuItem.IsExpanded = true;
    Microsoft.UI.Xaml.Controls.NavigationViewItem selectedItem = (Microsoft.UI.Xaml.Controls.NavigationViewItem)DashboardMenuItem.MenuItems[0];
    await Task.Delay(100);
    MainNavigation.SelectedItem = selectedItem;