C# – How to add Close Button in tab of Dynamic WPF Tab control

ctabcontrolwpf

I have a WPF Tab Control..

i am adding tabs in that TabControl dynamically

Now i want to give a Close Button in every Tab of Tab Control..

So please tell me hopw to add Close button in that Tab Control..

Code for adding tab

private void AddTab(ITabbedMDI mdiChild)
    {
       if (_mdiChildren.ContainsKey(mdiChild.UniqueTabName))
        {
            //user control is already opened in tab. 
            //So set focus to the tab item where the control hosted
            foreach (object item in tcMdi.Items)
            {
                TabItem ti = (TabItem)item;
                if (ti.Name == mdiChild.UniqueTabName)
                {
                    ucChildLoc = (UserControl)mdiChild;
                    ti.Focus();
                    //tcMdi.Width = this.ucChildLoc.Width;
                    //tcMdi.Height = this.ucChildLoc.Height;
                    break;
                }
            }
        }
   }

Code for Closing tab

private void CloseTab(ITabbedMDI tab, EventArgs e)
    {
        TabItem ti = null;
        foreach(TabItem item in tcMdi.Items)
        {
            if (tab.UniqueTabName == ((ITabbedMDI)item.Content).UniqueTabName)
            {
                ti = item;
                break;
            }
        }
        if (ti != null)
        {
            _mdiChildren.Remove(((ITabbedMDI)ti.Content).UniqueTabName);
            tcMdi.Items.Remove(ti);
        }
    }

I am using TabControl of this article

http://www.codeproject.com/Articles/32362/Tabbed-MDI-in-WPF

Thanks in Advance..

Best Answer

if you make the tab control as a custom control and inherit the functionality from tab control and then add close button and handle its events then it can work

    <UserControl x:Class="CloseableHeader"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  mc:Ignorable="d" 
  d:DesignHeight="23" d:DesignWidth="81" Margin="0">
<Grid>
  <Button Content="X"  Height="19" HorizontalAlignment="Right" Margin="0,3,4,0" 
      Name="button_close" VerticalAlignment="Top" Width="20" FontFamily="Courier" 
      FontWeight="Bold" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" 
      FontStretch="Normal" Visibility="Visible" 
      FontSize="14" Padding="0" ToolTip="Close"/>
  <Label Content="TabItem"  Height="23" HorizontalAlignment="Left" 
      Margin="4,1,0,0" Name="label_TabTitle" VerticalAlignment="Top" 
      FontFamily="Courier" FontSize="12" />
</Grid>

    class ClosableTab : TabItem
     {
        // Constructor
      public ClosableTab()
      {
         // Create an instance of the usercontrol
         closableTabHeader = new CloseableHeader();
         // Assign the usercontrol to the tab header
         this.Header = closableTabHeader;
       }
    }

see in this article for details http://www.codeproject.com/Articles/84213/How-to-add-a-Close-button-to-a-WPF-TabItem this might be helpful

Related Topic