您如何从标签标题中的按钮中找到TabiTem的标签?

发布于 2025-02-07 18:33:23 字数 2110 浏览 1 评论 0原文

我正在尝试制作一个系统来从选项卡控件中添加和删除选项卡。通过选择最后一个选项卡(在另一种方法中添加,但由“+”作为标头组成)。新创建的标签标头的标题是由文本块和一个按钮组成的网格。单击按钮时,它将运行CLOSE_CLICK。该代码在删除标签方面非常有用,尽管如何获得单击其关闭按钮的标签的标签?

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);
    }

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);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南街九尾狐 2025-02-14 18:33:23

如何获得单击其关闭按钮的标签的标签?

铸造TabItem在您的CLOSS_CLICK事件处理程序中:

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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文