Question about Diagram.UnloadingClearsPartManager

I think I got it.

Again, I've tried to move the UserControl to some collapsed Dummy Grid after nulling the TabItems Content. After calling Clear() on the Dummy Grids Children, the UserControl is removed from the memory.
Strange one.
Thank a lot for your help walter!!

Wait, are you saying that your code (shown above) has been working as expected after all?

Here’s that code with some cleanups:

private void btnRemove_Click(object sender, RoutedEventArgs e) { if (tabControl.SelectedIndex != -1) { TabItem item = (TabItem)tabControl.Items[tabControl.SelectedIndex]; UserControl contr = item.Content as UserControl; if (contr != null) { item.Content = null; dummyGrid.Children.Add(contr); dummyGrid.Children.Clear(); } tabControl.Items.RemoveAt(tabControl.SelectedIndex); } }
And to be clear, you only checked memory after calling GC.Collect(), right?

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(...)
<Window x:Class="NwoodsTestClient2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="860" Width="525">








Add to Tab
Remove selected TabItem
Switch to Dummy Grid
Remove from Dummy Grid




Add to Grid
Remove from Grid



GC














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();
}
}
}