You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
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
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.
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?
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.
@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.
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.
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:
@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.
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.
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.
Activity
ramezgerges commentedon Dec 5, 2023
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?
ADD-Noureddine-Maachi commentedon Dec 5, 2023
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.
ramezgerges commentedon Dec 29, 2023
Closing for now. We can reopen if there are new findings.
ADD-Noureddine-Maachi commentedon Jan 2, 2024
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.
agneszitte commentedon Nov 21, 2024
@ADD-Noureddine-Maachi based on the details here microsoft/microsoft-ui-xaml#9185 (comment), are you still experiencing this issue lately?
ADD-Noureddine-Maachi commentedon Nov 21, 2024
@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
SelectedItemfrom aNavigationOptionfrom the view model simulating a 5 seconds background work, does not select anything at all.Suriman commentedon Nov 21, 2024
Here a file to reproduce the issue:
NavigationViewItemIsSelectedIssue.zip
ramezgerges commentedon Nov 22, 2024
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
Loadedand setting the DataContext by the navigation feature.DataContextyourself. It will be done for you by Uno.Extension's navigation feature. For more, see https://platform.uno/blog/uno-extensions-navigation-guide.whereas on WinUI, I'm seeing
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 apublic MainViewModel( IStringLocalizer localizer, IOptions<AppConfig> appInfo, INavigator navigator)constructor.So TL;DR is
12 remaining items
ADD-Noureddine-Maachi commentedon May 26, 2025
@jeromelaban Have you already created the issue to Microsoft? If so, could you post the link so we can track it? Thanks.
jeromelaban commentedon May 26, 2025
@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.
ADD-Noureddine-Maachi commentedon Jun 11, 2025
@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.
MartinZikmund commentedon Jul 1, 2025
I think the reason may be that there were some new changes to
NavigationViewcode since we last updated the control, we will need to port them as wellNavigationView#21054github-actions commentedon May 1, 2026
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.
ADD-David-Antolin commentedon May 4, 2026
Keep it open.