我试图按照此说明进行操作,但我才刚刚开始使用 WPF。
如何使用可以在不同 TabControl 中重复使用的 UserControl 来执行此操作? 还有哪个是 TabControl 中的“Header”ContentPresenter风格?
下面是在 https://github.com/MahApps/MahApps.Metro/issues/281
The other way is to modify/create a style - the issue then is hooking it up to an actual 'close' event in a generic fashion.
If you look at the TabControl style, you'll see the "Header" ContentPresenter. If you wrap that in a stackpanel and add a button like so:
<StackPanel Orientation="Horizontal"> <Label x:Name="root" FontSize="26.67"> <ContentPresenter ContentSource="Header" RecognizesAccessKey="True" /> </Label> <Button Content="X" /> </StackPanel>
You get :
If you have that in your Window or UserControl (rather than a resource dictionary), you can wire that up so Click can fire and you can then remove the item from the databound collection or directly from the TabControl.
最佳答案
最简单的方法是使用 MetroTabItem
。它带有属性 CloseButtonEnabled
以启用/禁用关闭按钮。您还可以将命令绑定(bind)到 CloseTabCommand
和 CloseTabCommandParameter
。
<TabControl xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls">
<Controls:MetroTabItem Header="The Header of the TabItem"
CloseButtonEnabled="True"
CloseTabCommand="{Binding CloseTabCommand}"
CloseTabCommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Header}">
<!-- your content of the TabItem -->
</Controls:MetroTabItem>
</TabControl>
希望这对您有所帮助。
关于c# - 如何将关闭按钮添加到 MahApps TabControl 的选项卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27453547/