这是我一直在使用的,据我所知,它从记忆中删除了 tabitem。将计时器留在 tabitem 中的问题是 GC 不会收集和处理它,因为它检测到计时器仍在使用中。
编码:
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);
}
}
}
xml:
<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>
这是带有 Tabcontrol 以添加选项卡的页面:
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;
}