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
TabControl.SelectedIndex via DialogHost (repo) #3094
When opening a dialog await DialogHost.Show(view); via MenuItem (System.Windows.Controls) and setting the SelectedIndex of a TabControl by e.g. clicking a button, the selected tab is still the old one after the dialog has been closed.
The property that holds the SelectedIndex receives the new value but instantly switches back to the previous index, which can result in a flicker.
Reproduction
In order to reproduce the bug, please select the second tab (Info). Then click on Menu in the top right corner. Select Settings and click on the button in the dialog.
Debugging
I have found out that it does make a difference whether the dialog is opened via said MenuItem, a PopupBox (Material Design) or a button on any view:
It does work es expected in both of the last two cases, just the MenuItem-way fails.
In order to make it work, insert await Task.Delay(500); (at least 200-300) before setting SelectedIndex to its new value. I'm not a fan of using Task.Delay() but currently out of ideas how to make it work without it.
Using DI does not make a difference in any of the above mentioned behaviors.
@VR462002 Thank you for the great explanation and repo! I'll have a look to see if I can figure out what is going on. I can definitely reproduce the issue with the temporary tab switch (and wrong destination tab).
@VR462002 I am still working on it, but the PR above was me confusing myself a bit.
The root cause, is that the DialogHost, when closed, tries to restore focus to the element which had focus before opening the popup. In your case, that element is the TabItem at index 1. So even though you switch to the tab at index 0, the "restore focus" brings the tab at index 1 back in focus (causing the tab switch).
I am considering what is the right approach to fix this. Ideally, focus should just return to the TabControl (not the TabItem), but it may require adding special handling of a bunch of different controls to handle these types of things (NavigationRail and other controls which have state like this). That feels a bit yucky. I think I will create a draft PR showcasing the issue which includes such a "hacky" fix, just so it can be discussed with some context.
Activity
nicolaihenriksen commentedon Feb 24, 2023
@VR462002 Thank you for the great explanation and repo! I'll have a look to see if I can figure out what is going on. I can definitely reproduce the issue with the temporary tab switch (and wrong destination tab).
nicolaihenriksen commentedon Feb 25, 2023
@VR462002 I am still working on it, but the PR above was me confusing myself a bit.
The root cause, is that the
DialogHost, when closed, tries to restore focus to the element which had focus before opening the popup. In your case, that element is theTabItemat index 1. So even though you switch to the tab at index 0, the "restore focus" brings the tab at index 1 back in focus (causing the tab switch).I am considering what is the right approach to fix this. Ideally, focus should just return to the
TabControl(not theTabItem), but it may require adding special handling of a bunch of different controls to handle these types of things (NavigationRailand other controls which have state like this). That feels a bit yucky. I think I will create a draft PR showcasing the issue which includes such a "hacky" fix, just so it can be discussed with some context.