CleanupHelper.xaml.cs
1 // Copyright (c) Microsoft Corporation 2 // The Microsoft Corporation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using Microsoft.UI.Xaml; 6 using Microsoft.UI.Xaml.Controls; 7 using Microsoft.UI.Xaml.Media; 8 9 namespace Microsoft.CmdPal.UI; 10 11 public static class CleanupHelper 12 { 13 public static void Cleanup(FrameworkElement element) 14 { 15 var count = VisualTreeHelper.GetChildrenCount(element); 16 for (var index = 0; index < count; index++) 17 { 18 var child = VisualTreeHelper.GetChild(element, index); 19 if (child is FrameworkElement childElement) 20 { 21 Cleanup(childElement); 22 } 23 } 24 25 switch (element) 26 { 27 case ItemsControl itemsControl: 28 itemsControl.ItemsSource = null; 29 break; 30 case ItemsRepeater itemsRepeater: 31 itemsRepeater.ItemsSource = null; 32 break; 33 case TabView tabView: 34 tabView.TabItemsSource = null; 35 break; 36 } 37 } 38 }