Actually calling GC.Collect(…) never made any difference.
Above the entire XAML + Code of the MainWindow.
1. btnAddTab_Click(...)
2. btnSwitch_Click(...)
3. btnRemoveFromDummy_Click(...)
Code:
using System;
using System.Windows;
using System.Windows.Controls;
namespace NwoodsTestClient2
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnGC_Click(object sender, RoutedEventArgs e)
{
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
}
private void btnAddTab_Click(object sender, RoutedEventArgs e)
{
TabItem item = new TabItem();
item.Header = "Diagram";
TestControl contr = new TestControl();
item.Content = contr;
tabControl.Items.Add(item);
}
private void btnAddGrid_Click(object sender, RoutedEventArgs e)
{
TestControl contr = new TestControl();
contentGrid.Children.Add(contr);
}
private void btnRemoveTab_Click(object sender, RoutedEventArgs e)
{
if (tabControl.SelectedIndex != -1)
{
TabItem item = (TabItem)tabControl.Items[tabControl.SelectedIndex];
if ((string)item.Header == "Always here")
return;
if (item.Content is TestControl)
{
TestControl contr = (TestControl)item.Content;
item.Content = null;
}
tabControl.Items.RemoveAt(tabControl.SelectedIndex);
item = null;
}
}
private void btnRemoveGrid_Click(object sender, RoutedEventArgs e)
{
contentGrid.Children.Clear();
}
private void btnSwitch_Click(object sender, RoutedEventArgs e)
{
if (tabControl.SelectedIndex != -1)
{
TabItem item = (TabItem)tabControl.Items[tabControl.SelectedIndex];
if ((string)item.Header == "Always here")
return;
if (item.Content is TestControl)
{
TestControl contr = (TestControl)item.Content;
item.Content = null;
dummyGrid.Children.Add(contr);
contr = null;
}
tabControl.Items.RemoveAt(tabControl.SelectedIndex);
item = null;
}
}
private void btnRemoveFromDummy_Click(object sender, RoutedEventArgs e)
{
dummyGrid.Children.Clear();
}
}
}