gpt4 book ai didi

c# - 如何在动态 WPF 选项卡控件的选项卡中添加关闭按钮

转载 作者:太空狗 更新时间:2023-10-29 22:27:19 26 4
gpt4 key购买 nike

我有一个 WPF 选项卡控件..

我在那个 TabControl 动态添加标签

现在我想在选项卡控件的每个选项卡中提供一个关闭按钮..

所以请告诉我如何在该选项卡控件中添加关闭按钮..

添加标签的代码

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

关闭标签的代码

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

我用的是本文的TabControl

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

提前致谢..

最佳答案

我已经尝试了几种解决方案并努力找到看起来不错的东西,并且还在鼠标悬停时突出显示按钮中的“X”。我终于以这个结束了。它也不需要太多代码。希望对您有所帮助:

enter image description here

<TabControl>
<TabItem>
<TabItem.Header>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0">Output</TextBlock>
<Button Grid.Column="1" Name="button_close" Click="button_close_Click">
<Button.Template>
<ControlTemplate TargetType="Button">
<Path Data="M0,0 L8,8 M8,0 L0,8" StrokeThickness="3" VerticalAlignment="Center" Margin="5,4,0,2">
<Path.Style>
<Style TargetType="{x:Type Path}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="Stroke" Value="LightGray" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Stroke" Value="Black" />
</Trigger>
</Style.Triggers>
</Style>
</Path.Style>
</Path>
</ControlTemplate>
</Button.Template>
</Button>
</Grid>


</TabItem.Header>
<TabItem.Content>
</TabItem.Content>

关于c# - 如何在动态 WPF 选项卡控件的选项卡中添加关闭按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13449451/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com