Home > Back-end >  How do you find the tag of a TabItem from a button in the tabs header?
How do you find the tag of a TabItem from a button in the tabs header?

Time:06-15

I am trying to make a system to add and remove tabs from a tab control. Tabs are added by selecting the last tab (added in another method but consists of a " " as the header). The header of newly created tabs header is a grid composed of a text block and a button. When the button is clicked, it will run Close_Click. The code works great at removing the tab, although how do I get the tag of the tab that had its close button clicked?

private void TabControl_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        if (TabControl.SelectedIndex == TabControl.Items.Count - 1)
        {
            string tabName = "Tab"   (TabControl.Items.Count - 1);
            string tabTag = RandomString(16);

            Grid Grid = new Grid();
            TextBlock TextBlock = new TextBlock()
            {
                Text = tabName
            };
            Button Button = new Button()
            {
                Content = "X",
                Width = Double.NaN,
                Height = Double.NaN,
                Background = Brushes.Transparent,
                BorderBrush = Brushes.Transparent
            };

            Button.Click  = Close_Click;
            Grid.ColumnDefinitions.Add(new ColumnDefinition());
            Grid.ColumnDefinitions.Add(new ColumnDefinition());
            Grid.Children.Add(TextBlock);
            Grid.Children.Add(Button);
            Grid.SetColumn(TextBlock, 0);
            Grid.SetColumn(Button, 1);

            TabItem newTabItem = new TabItem
            {
                Header = Grid,
                Name = tabName,
                Tag = tabTag
            };

            TabControl.Items.Insert(TabControl.Items.Count - 1, newTabItem);

            Dispatcher.BeginInvoke((Action)(() => TabControl.SelectedIndex = TabControl.Items.Count - 2));
        }
    }

    private void Close_Click(object sender, RoutedEventArgs e)
    {
        var target = (FrameworkElement)sender;
        while (target is Grid == false)
        {
            target = (FrameworkElement)target.Parent;
        }
        var tabItem = (target as Grid).Parent;
        if (TabControl.SelectedIndex == TabControl.Items.IndexOf(tabItem))
        {
            TabControl.SelectedIndex = TabControl.Items.IndexOf(tabItem) - 1;
        }

        TabControl.Items.Remove(tabItem);
    }

CodePudding user response:

how do I get the tag of the tab that had its close button clicked?

Cast tabItem in your Close_Click event handler:

private void Close_Click(object sender, RoutedEventArgs e)
{
    var target = (FrameworkElement)sender;
    while (target is Grid == false)
    {
        target = (FrameworkElement)target.Parent;
    }
    var tabItem = (target as Grid).Parent as TabItem;
    string tag = tabItem.Tag as string;
    if (TabControl.SelectedIndex == TabControl.Items.IndexOf(tabItem))
    {
        TabControl.SelectedIndex = TabControl.Items.IndexOf(tabItem) - 1;
    }

    TabControl.Items.Remove(tabItem);
}
  • Related