Skip to content

NavigationViewItem's IsSelected property doesn't update to true when NavigationView's SelectedItem is updated in the View Model. This is causing VisualStates to not work as expected. #14668

Open

Description

@Suriman

Current behavior

When the default NavigationView's SelectedItem is setted in the View Model, the SelectionChanged event is invoked correctly but:

  • When obtaining the SelectedItemContainer (through event arguments), the IsSelected value is still false.
    Once you change tab, then it works as spected (IsSelected value is setted to true).
  • The SelectedItemContainer (through event arguments) is null for WPF & WASM. Only Windows is returning the container.
    A workaround in Windows is to set the IsSelected value to true when the SelectionChanged event is invoked.
  • This behavior causes that VisualStates don´t work as expected.

Expected behavior

IsSelected value should be setted true when setting the NavigationView's SelectedItem through the View Model. This should solve VisualStates current behavior.

How to reproduce it (as minimally and precisely as possible)

How to reproduce it:
1 - Open the attached project and compile it.
2 - Execute the Windows, WPF & WASM projects and debug the SelectionChanged event in MainPage's code behind (MainPage.xaml.cs).
There you can see that the event is invoked but the property IsSelected is not setted to true.
With WPF & WASM you should see that the SelectedItemContainer is returning null.

Workaround

Works on UWP/WinUI: Yes, but with the workaround.

Works on UWP/WinUI

Yes

Environment

Uno.UI / Uno.UI.WebAssembly / Uno.UI.Skia

NuGet package version(s)

NuGet package(s): Uno.WinUI, Uno.Material.WinUI
NuGet package version(s): Uno.WinUI 5.0.48, Uno.Material.WinUI 4.0.6

Affected platforms

WebAssembly, Skia (WPF), Windows

IDE

Visual Studio 2022

IDE version

17.8.1

Relevant plugins

No response

Anything else we need to know?

NavigationViewItemIsSelectedValue.zip

Activity

  1. added
    difficulty/tbd
    kind/bug
    triage/untriaged
    on Dec 4, 2023
  2. added
    area/navigationview 🧭
    on Dec 4, 2023
  3. self-assigned this
    on Dec 4, 2023
  4. ramezgerges commented on Dec 5, 2023

    @ramezgerges
    Contributor

    I can't reproduce this. The container is not null. The container has IsSelected = False inside the event handler but this is the case on both Windows (WinUI) and our Uno targets (WPF, Wasm, etc.). We attempt to match the behaviour on WinUI as closely as we can, so if you see the same behaviour (IsSelected = False) on both Windows and WPF, it's not a bug. @Suriman can you please check again and revise the issue?

  5. ADD-Noureddine-Maachi commented on Dec 5, 2023

    @ADD-Noureddine-Maachi

    You are right about not being able to reproduce it. I remember having reproduced it well...
    I'll check it out and tell you something. Thanks in advance.

  6. ramezgerges commented on Dec 29, 2023

    @ramezgerges
    Contributor

    Closing for now. We can reopen if there are new findings.

  7. ADD-Noureddine-Maachi commented on Jan 2, 2024

    @ADD-Noureddine-Maachi

    Reviewing it, we have seen that it seems to be a WinUI bug and we have reported it to Microsoft. You can follow the progress here.

  8. agneszitte commented on Nov 21, 2024

    @agneszitte
    Member

    @ADD-Noureddine-Maachi based on the details here microsoft/microsoft-ui-xaml#9185 (comment), are you still experiencing this issue lately?

  9. ADD-Noureddine-Maachi commented on Nov 21, 2024

    @ADD-Noureddine-Maachi

    @agneszitte This issue is still happening. I will try to prepare an update repo for you so you can see it better. The main issue here is, setting the SelectedItem from a NavigationOption from the view model simulating a 5 seconds background work, does not select anything at all.

  10. Suriman commented on Nov 21, 2024

    @Suriman
    Author

    Here a file to reproduce the issue:
    NavigationViewItemIsSelectedIssue.zip

  11. ramezgerges commented on Nov 22, 2024

    @ramezgerges
    Contributor

    Edit: The suggestions in this comments are outdated. Further investigation showed that our behaviour is actually similar to WinUI, it's just that the timing is supposed to be non-deterministic and it just so happens that most of the time, the sequence of interleaving threads will behave a certain way. You should follow Nick's advice below and mostly ignore this comment. The only important part is to not set the DataContext yourself.

    @Suriman Hello. I can repro the problem. It seems to be a timing problem between Loaded and setting the DataContext by the navigation feature.

    • You're not supposed to set DataContext yourself. It will be done for you by Uno.Extension's navigation feature. For more, see https://platform.uno/blog/uno-extensions-navigation-guide.
    • Part of the navigation logic sets the DataContext to null at some point to ensure a "fresh" view model. This shouldn't be a problem because you're not supposed to be creating the view model and setting it as the data context yourself. This happens on both WinUI and Uno.
    • Later, the DataContext is correctly set to a newly constructed MainViewModel instance. This makes the DataContext that you created irrelevant, since it gets replaced with a new instance. This also happens on both Uno and WinUI. The only difference is the timing. On Uno, I'm seeing
      • DataContext set to null
      • a new MainViewModel is created
      • Loaded
      • DataContext set to the MainViewModel
        whereas on WinUI, I'm seeing
      • DataContext set to null
      • a new MainViewModel is created
      • DataContext set to a MainViewModel
      • Loaded
    • Oddly enough, if I create a new uno app with dotnet new unoapp -o navigationdatacontextnullrepro -preset "recommended" -platforms "windows" -platforms "desktop" -theme-service False -vscode False -presentation "mvvm" -di -toolkit False -dsp False -pub "navigationdatacontextnullrepro", I see the same problem on WinUI too. The only difference I'm seeing is that the repro in this issue uses a parameterless constructor in MainViewModel, while the newly created app I tried uses a public MainViewModel( IStringLocalizer localizer, IOptions<AppConfig> appInfo, INavigator navigator) constructor.

    So TL;DR is

    • Do not construct your own view models and don't set the DataContext manually.
    • To work around the timing inconsistency, enqueue the logic of Loaded on the dispatcher, like this:
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            DispatcherQueue.TryEnqueue(async () =>
            {
                await ViewModel.LoadAsync();
            });
        }
  12. 12 remaining items

  13. added
    area/navigationview 🧭
    and removed
    area/navigationview 🧭
    on Jan 6, 2025
  14. ADD-Noureddine-Maachi commented on May 26, 2025

    @ADD-Noureddine-Maachi

    @jeromelaban Have you already created the issue to Microsoft? If so, could you post the link so we can track it? Thanks.

  15. jeromelaban commented on May 26, 2025

    @jeromelaban
    Member

    @ADD-Noureddine-Maachi I don't believe it was created so far. It's generally best when a customer creates the issue, it's weighed differently in processing. If you don't mind creating it, we'll upvote it.

  16. ADD-Noureddine-Maachi commented on Jun 11, 2025

    @ADD-Noureddine-Maachi

    @jeromelaban Before creating the issue, I decided to test the behavior in a solo WinUI blank app and seems to work fine.

    Here's the WinUI solution: WinUiNavigationViewIsSelectedValue.zip

    I've tested the same behavior in a new Uno blank app with the latest version. The selected item should be marked as selected but it does not, which in WinUI does.

    Thanks in advance.

  17. added theissue type on Jun 20, 2025
  18. MartinZikmund commented on Jul 1, 2025

    @MartinZikmund
    Member

    I think the reason may be that there were some new changes to NavigationView code since we last updated the control, we will need to port them as well

  19. removed
    blocked/winui
    on Jul 1, 2025
  20. github-actions commented on May 1, 2026

    @github-actions
    Contributor

    This issue has had no activity for 120 days. It will be closed in 10 days if there is no further activity. Remove the Stale label or leave a comment to keep it open.

  21. added
    stale
    on May 1, 2026
  22. ADD-David-Antolin commented on May 4, 2026

    @ADD-David-Antolin

    Keep it open.

  23. removed
    stale
    on May 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/navigationview 🧭kind/bug

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions