Reputation: 193
I am workin on tab Controls in WPF. I have added a set of tabs in my tab control and also implemented Close button method which works fine.
private void CloseTab(object source, RoutedEventArgs args)
{
TabItem tabItem = args.Source as TabItem;
if (tabItem != null)
{
TabControl tabControl = tabItem.Parent as TabControl;
if (tabControl != null)
tabControl.Items.Remove(tabItem);
}
}
XAML:
<TabControl Name="ConnectTab" Style="{DynamicResource styleBackground}">
<tablocal:CloseableTabItem Header="Connect" >
</tablocal:CloseableTabItem>
<tablocal:CloseableTabItem Header="I2C" />
<tablocal:CloseableTabItem Header="Voltage" />
<tablocal:CloseableTabItem Header="Clock" />
The problem i am facing is to keep one tab active(shouldnt close) after all the tabs have been closed. Basically the last tab left in tabcontrol shud not close even if user clicks the X button on tab.
Upvotes: 1
Views: 1469
Reputation: 1114
Change the condition to -
if (tabControl != null && tabControl.Items.Count > 1)
tabControl.Items.Remove(tabItem);
Upvotes: 1