This is what I've been using, and as far as I can tell it removes the tabitem from memory. The problem with leaving a timer inside of the tabitem, is that the GC won't collect and dispose of it because it detects that the timer is still in use.
The Code:
namespace Reports.Controls
{
/// <summary>
/// Interaction logic for Test.xaml
/// </summary>
public partial class ReportTab : TabItem
{
public delegate void CloseEvents(ReportTab TabIndex);
public event CloseEvents Closing;
public ReportTab(string Title)
{
InitializeComponent();
tbTitle.Text = Title;
}
private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Closing(this);
}
}
}
The xaml:
<TabItem x:Class="Reports.Controls.ReportTab"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<TabItem.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Main" Name="tbTitle" Margin="0,0,8,0"/>
<Image Height="13"
Source="pack://application:,,/Images/Icons/close.png"
MouseLeftButtonUp="Image_MouseLeftButtonUp"/>
</StackPanel>
</TabItem.Header>
<Grid>
//Tabitem stuff
</Grid>
</TabItem>
Here's the page with the Tabcontrol to add a tab:
void AddTab(string Title)
{
Controls.ReportTab rt = new Controls.ReportTab(Title);
rt.Closing += new Controls.ReportTab.CloseEvents(rt_Closing);
tabControl.SelectedIndex = tabControl.Items.Add(rt);
}
/// <summary>
/// Moves the Tab Control back to the Main tab
/// after a tab is removed
/// </summary>
/// <param name="TabIndex"></param>
void rt_Closing(Controls.ReportTab TabIndex)
{
tabControl.Items.Remove(TabIndex);
//This resets the tabcontrol back to it's first tabindex
tabControl.SelectedIndex = 0;
}