Skip to content

Commit ded613c

Browse files
authored
Merge branch 'main' into 2.0
2 parents af3b102 + a4c856d commit ded613c

1 file changed

Lines changed: 37 additions & 6 deletions

File tree

WinUIGallery/Samples/SamplePages/TabViewWindowingSamplePage.xaml.cs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ private void Tabs_TabTearOutRequested(TabView sender, TabViewTabTearOutRequested
7777
GetParentTabView(tab)?.TabItems.Remove(tab);
7878
newPage.AddTabToTabs(tab);
7979
}
80+
81+
// Clear the reference now that the tear-out is complete to avoid stale references
82+
// if multiple tear-outs happen in sequence.
83+
tabTearOutWindow = null;
84+
85+
// Close the source window if all tabs have been torn out.
86+
CloseWindowIfEmpty(sender);
8087
}
8188

8289
private void Tabs_ExternalTornOutTabsDropping(TabView sender, TabViewExternalTornOutTabsDroppingEventArgs args)
@@ -90,9 +97,17 @@ private void Tabs_ExternalTornOutTabsDropped(TabView sender, TabViewExternalTorn
9097

9198
foreach (TabViewItem tab in args.Tabs.Cast<TabViewItem>())
9299
{
93-
GetParentTabView(tab)?.TabItems.Remove(tab);
100+
// Find the source TabView before removing the tab, so we can check if it's empty afterwards.
101+
TabView? sourceTabView = GetParentTabView(tab);
102+
sourceTabView?.TabItems.Remove(tab);
94103
sender.TabItems.Insert(args.DropIndex + position, tab);
95104
position++;
105+
106+
// Close the source window if all its tabs have been moved to this window.
107+
if (sourceTabView != null && sourceTabView.TabItems.Count == 0)
108+
{
109+
CloseWindowIfEmpty(sourceTabView);
110+
}
96111
}
97112
}
98113

@@ -113,6 +128,26 @@ private void Tabs_ExternalTornOutTabsDropped(TabView sender, TabViewExternalTorn
113128
return null;
114129
}
115130

131+
private void CloseWindowIfEmpty(TabView tabView)
132+
{
133+
if (tabView.TabItems.Count == 0)
134+
{
135+
// Find the window containing this TabView and close it.
136+
// Walk up from the TabView to find a Page, then use it to locate the window.
137+
DependencyObject current = tabView;
138+
while (current != null)
139+
{
140+
if (current is Page page)
141+
{
142+
WindowHelper.GetWindowForElement(page)?.Close();
143+
return;
144+
}
145+
146+
current = VisualTreeHelper.GetParent(current);
147+
}
148+
}
149+
}
150+
116151
private TabViewItem CreateNewTVI(string header, string dataContext)
117152
{
118153
var newTab = new TabViewItem()
@@ -140,10 +175,6 @@ private void Tabs_AddTabButtonClick(TabView sender, object args)
140175
private void Tabs_TabCloseRequested(TabView sender, TabViewTabCloseRequestedEventArgs args)
141176
{
142177
sender.TabItems.Remove(args.Tab);
143-
144-
if (sender.TabItems.Count == 0)
145-
{
146-
WindowHelper.GetWindowForElement(this)?.Close();
147-
}
178+
CloseWindowIfEmpty(sender);
148179
}
149180
}

0 commit comments

Comments
 (0)