c# - 如何将关闭按钮添加到 MahApps TabControl 的选项卡?

标签 c# wpf xaml mahapps.metro

我试图按照此说明进行操作,但我才刚刚开始使用 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 :

enter image description here

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)到 CloseTabCommandCloseTabCommandParameter

<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/

相关文章:

c# - 绑定(bind)多功能 WPF DataGrid

c# - 在 MVVM 中的多个 ViewModel 之间传播/冒泡事件

silverlight - Windows Phone 中的进度条点间距问题

c# - .NET 中的数组是否失去了意义?

c# - Task 和 Task<T> 可以互换吗?

C# WPF GUI 性能 MVVM

c# - WPF MVVM RelayCommand 每个 DataGrid 行上的多个按钮,仅为选定行设置启用/禁用按钮状态

c# - 在 'code behind' 中设置的 WPF 控件属性,之后忽略绑定(bind)

c# - 更新表单例份验证票 - 奇怪的时间

c# - 在 C# 中获取两个数组之间的 "diff"?