关闭/删除标签项WPF [英] Closing/Removing a tab item WPF

查看:155
本文介绍了关闭/删除标签项WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个窗口的一个标签控件。这些标签都有哪些(理应)允许用户关闭它们简单的上下文菜单。然而,当我点击关闭,没有任何反应。

I have a tab control in a window. The tabs all have simple context menus which (are supposed to) allow the user to close them. However, when I click close, nothing happens.

下面是事件处理程序

void closeTab_Click(object sender, RoutedEventArgs e)
{
    Tabs.Items.Remove((MenuItem)sender);
}



我环顾四周,大约成交的标签,但没有我发现文章。走进如何真正关闭这个选项卡中的细节

I've looked around about closing tabs, but none of the articles I found went into much detail about how to actually close the tab.

新的问题:

void closeTab_Click(object sender, RoutedEventArgs e) 
{ 
    MenuItem close = (MenuItem)sender; 
    Tabs.Items.Remove(Convert.ToInt32(close.Name.Remove(0,3))); 
} 



上下文菜单项正是如此命名为:

The context menu item is named thusly:

Name = "Tab" + Tabs.Items.Count.ToString(), 

它仍然什么都不做。

推荐答案

菜单项不是标签。你无法从的TabControl 将其删除。你需要一个参考标签到的菜单项所属。这可以通过多种方式来实现。

The menu item is not the tab. You cannot remove it from the TabControl. You need a reference to the tab to which the MenuItem belongs. This can be done in various ways.

我看你尝试了一些,而哈克事情出现了姓名和字符串处理,在这里将是一个更干净的方法,它不需要任何的是:

I see you tried some rather hacky things there with names and string manipulation, here would be a more clean approach which does not require any of that:

var target = (FrameworkElement)sender;
while (target is ContextMenu == false)
    target = (FrameworkElement)target.Parent;
var tabItem = (target as ContextMenu).PlacementTarget;
Tabs.Items.Remove(tabItem);

这得到父母直到找到文本菜单并得到 TabItem的 PlacementTarget

This gets the parent until it finds the ContextMenu and gets the TabItem from the PlacementTarget.

这篇关于关闭/删除标签项WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆