您如何从标签标题中的按钮中找到TabiTem的标签?
我正在尝试制作一个系统来从选项卡控件中添加和删除选项卡。通过选择最后一个选项卡(在另一种方法中添加,但由“+”作为标头组成)。新创建的标签标头的标题是由文本块和一个按钮组成的网格。单击按钮时,它将运行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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
铸造
TabItem
在您的CLOSS_CLICK
事件处理程序中:Cast
tabItem
in yourClose_Click
event handler: