Skip to content

Several child modal windows created in one container change their order. #1802

@Alex564Alex

Description

@Alex564Alex

Problem: Several child modal windows created in one container change their order.

In a WPF application, we create two tabs. Each tab contains a UserControl and a Grid inside it. In tab № 1, we programmatically create a WindowContainer. Inside WindowContainer, we add a child modal window ChildWindow № 1, and then a child modal window ChildWindow № 2. Both windows are added to one WindowContainer. This is done intentionally!
Further actions that lead to the problem:

  1. The user switches to tab № 2. The contents of tab № 1 (two modal windows) are hidden.
  2. The user switches to tab № 1, which contains both windows. Window № 1 is on top (accessible to the user), and window № 2 is below (i.e. under window № 1 and inaccessible to the user). This is a problem, because the windows were created in a different order!
  3. The user switches to tab № 2.
  4. The user returns to tab № 1. Now the order of the windows is restored. Window № 2 is on top, and window № 1 is below. That is, the windows are located in the correct order in which they were created by the user.

How to solve this problem? So that the windows always remain in the order in which they were created.


Next, the same action, but combined with the log:

In a WPF application, we create two tabs. Each tab contains a UserControl and a Grid inside it. In tab № 1, we programmatically create a WindowContainer. Inside WindowContainer, we add a child modal window ChildWindow № 1, and then a child modal window ChildWindow № 2. Both windows are added to one WindowContainer. This is done intentionally!
2025-08-08 16:01:11.5110 Event_Window_GotFocus. №: 1 (total: 1); ZIndex: 1;
2025-08-08 16:01:11.5116 Event_Window_IsVisibleChanged. IsVisible: True; №: 1 (total: 1); ZIndex: 1;
2025-08-08 16:01:12.7825 Event_Window_Activated. №: 1 (total: 1); ZIndex: 2;
2025-08-08 16:01:12.7829 Event_WinContainer_Loaded
2025-08-08 16:01:12.7831 Event_ChildWindow_Loaded.
...
2025-08-08 16:01:13.4891 Event_Window_LostFocus. №: 1 (total: 1); ZIndex: 2;
2025-08-08 16:01:13.4895 Event_WinContainer_LostFocus
2025-08-08 16:01:13.4905 Event_Window_GotFocus. №: 1 (total: 1); ZIndex: 2;
2025-08-08 16:01:15.7320 Event_Window_LostFocus. №: 1 (total: 1); ZIndex: 2;
2025-08-08 16:01:15.7322 Event_WinContainer_LostFocus
2025-08-08 16:01:15.7325 Event_Window_GotFocus. №: 1 (total: 1); ZIndex: 2;
2025-08-08 16:01:17.3307 Event_Window_LostFocus. №: 1 (total: 1); ZIndex: 2;
2025-08-08 16:01:17.3309 Event_WinContainer_LostFocus
2025-08-08 16:01:17.3311 Event_Window_GotFocus. №: 1 (total: 1); ZIndex: 2;
2025-08-08 16:01:17.3358 Event_Window_LostFocus. №: 1 (total: 1); ZIndex: 2;
2025-08-08 16:01:17.3360 Event_WinContainer_LostFocus
2025-08-08 16:01:17.3361 Event_Window_GotFocus. №: 1 (total: 1); ZIndex: 2;
...
2025-08-08 16:01:17.7139 Event_Window_LostFocus. №: 1 (total: 2); ZIndex: 2;
2025-08-08 16:01:17.7140 Event_WinContainer_LostFocus
2025-08-08 16:01:17.7171 Event_WinContainer_LostFocus
2025-08-08 16:01:17.7173 Event_Window_GotFocus. №: 2 (total: 2); ZIndex: 3;
2025-08-08 16:01:17.7174 Event_Window_IsVisibleChanged. IsVisible: True; №: 2 (total: 2); ZIndex: 3;
2025-08-08 16:01:17.8637 Event_ChildWindow_Loaded.

Further actions that lead to the problem:

  1. The user switches to tab № 2. The contents of tab № 1 (two modal windows) are hidden.
    2025-08-08 16:01:50.5046 Event_WinContainer_IsVisibleChanged. IsVisible: False
    2025-08-08 16:01:50.5051 Event_WinContainer_IsVisibleChanged. IsVisible: False
    2025-08-08 16:01:50.5053 Event_Window_IsVisibleChanged. IsVisible: False; №: 1 (total: 2); ZIndex: 2;
    2025-08-08 16:01:50.5186 Event_Window_IsVisibleChanged. IsVisible: False; №: 2 (total: 2); ZIndex: 6;
    ...

  2. The user switches to tab № 1, which contains both windows. Window № 1 is on top (accessible to the user), and window № 2 is below (i.e. under window № 1 and inaccessible to the user). This is a problem, because the windows were created in a different order!
    2025-08-08 16:02:26.9162 Event_WinContainer_IsVisibleChanged. IsVisible: True
    2025-08-08 16:02:26.9171 Event_WinContainer_IsVisibleChanged. IsVisible: True
    2025-08-08 16:02:26.9182 Event_Window_LostFocus. №: 2 (total: 2); ZIndex: 7;
    2025-08-08 16:02:26.9189 Event_WinContainer_LostFocus
    2025-08-08 16:02:26.9195 Event_WinContainer_LostFocus
    2025-08-08 16:02:26.9262 Event_Window_GotFocus. №: 1 (total: 2); ZIndex: 8;
    2025-08-08 16:02:26.9269 Event_Window_IsVisibleChanged. IsVisible: True; №: 1 (total: 2); ZIndex: 8;
    2025-08-08 16:02:27.0897 Event_Window_LostFocus. №: 1 (total: 2); ZIndex: 9;
    2025-08-08 16:02:27.0902 Event_WinContainer_LostFocus
    2025-08-08 16:02:27.0903 Event_WinContainer_LostFocus
    2025-08-08 16:02:27.0905 Event_Window_GotFocus. №: 1 (total: 2); ZIndex: 9;

  3. The user switches to tab № 2.
    2025-08-08 16:02:54.9185 Event_WinContainer_IsVisibleChanged. IsVisible: False
    2025-08-08 16:02:54.9191 Event_WinContainer_IsVisibleChanged. IsVisible: False
    2025-08-08 16:02:54.9195 Event_Window_IsVisibleChanged. IsVisible: False; №: 1 (total: 2); ZIndex: 10;
    2025-08-08 16:02:54.9762 Event_Window_LostFocus. №: 1 (total: 2); ZIndex: 11;
    2025-08-08 16:02:54.9769 Event_WinContainer_LostFocus
    2025-08-08 16:02:54.9772 Event_WinContainer_LostFocus

  4. The user returns to tab № 1. Now the order of the windows is restored. Window № 2 is on top, and window № 1 is below. That is, the windows are located in the correct order in which they were created by the user.
    2025-08-08 16:03:21.1089 Event_WinContainer_IsVisibleChanged. IsVisible: True
    2025-08-08 16:03:21.1093 Event_WinContainer_IsVisibleChanged. IsVisible: True
    2025-08-08 16:03:21.1105 Event_Window_GotFocus. №: 2 (total: 2); ZIndex: 12;
    2025-08-08 16:03:21.1110 Event_Window_IsVisibleChanged. IsVisible: True; №: 2 (total: 2); ZIndex: 12;

private static ChildWindow CreateChildWindow(Panel _contentContainer, object viewModel, object view, string? title) {
   var winContainer = GetWindowContainer(_contentContainer);

   winContainer.Initialized += WinContainer_Initialized;
   winContainer.Loaded += WinContainer_Loaded;
   winContainer.Unloaded += WinContainer_Unloaded;
   winContainer.IsVisibleChanged += WinContainer_IsVisibleChanged;
   winContainer.LostFocus += WinContainer_LostFocus;

   var window = new ChildWindow {
   	Caption = title,
   	WindowStartupLocation = Xceed.Wpf.Toolkit.WindowStartupLocation.Center,
   	IsModal = true,
   	Content = view,
   };
   window.Loaded += ChildWindow_Loaded;

   window.Activated += Window_Activated;
   window.Closing += Window_Closing;
   window.FocusableChanged += Window_FocusableChanged;
   window.GotFocus += Window_GotFocus;
   window.Initialized += Window_Initialized;
   window.IsEnabledChanged += Window_IsEnabledChanged;
   window.IsVisibleChanged += Window_IsVisibleChanged;
   window.LostFocus += Window_LostFocus;
   window.Unloaded += Window_Unloaded;

   if (viewModel is IViewModelCancel vmCancel) {
   	window.KeyUp += (sender, e) => {
   		if (e.Key == Key.Escape) {
   			vmCancel.OnCancel();
   			e.Handled = true;
   		}
   	};
   }
   window.Closed += (sender, e) => {
   	window.Loaded -= ChildWindow_Loaded;
   	winContainer.Children.Remove(window);
   	if (winContainer.Children.Count > 0) {
   		winContainer.Children[winContainer.Children.Count - 1].Focus();
      }
   	else {
   		_contentContainer.Children.Remove(winContainer);
   	}
   };
   winContainer.Children.Add(window);
   return window;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions