Skip to content
Merged
Prev Previous commit
Next Next commit
"Hacky fix" to showcase what the root cause is
  • Loading branch information
nicolaihenriksen committed Feb 27, 2023
commit b5031fb4c8b6df14ec3b0fc95a03416587f9356f
13 changes: 13 additions & 0 deletions MaterialDesignThemes.Wpf/DialogHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,19 @@ private static void IsOpenPropertyChangedCallback(DependencyObject dependencyObj
var window = Window.GetWindow(dialogHost);
dialogHost._restoreFocusDialogClose = window != null ? FocusManager.GetFocusedElement(window) : null;

/* The root cause of the issue is that dialogHost._restoreFocusDialogClose at this point is a TabItem.
* So when trying to restore focus when the dialog closes, it will call .Focus() on the TabItem which
* effectively does a tab switch if that TabItem is not currently the selected TabItem.
*
* This IS NOT the right fix, but showcases the issue. How can we go about creating a generic fix for this?
* I don't believe TabControl is the only control which is facing this issue. Any control with state similar
* to the TabControl would arguably suffer from the same issue.
*/
if (dialogHost._restoreFocusDialogClose is TabItem { Parent: TabControl tabControl })
{
dialogHost._restoreFocusDialogClose = tabControl;
}

//multiple ways of calling back that the dialog has opened:
// * routed event
// * the attached property (which should be applied to the button which opened the dialog
Expand Down