【问题标题】:How do I add a close button to a tab of the MahApps TabControl?如何将关闭按钮添加到 MahApps TabControl 的选项卡?
【发布时间】:2014-12-12 23:09:18
【问题描述】:

我试图按照此说明操作,但我刚刚开始使用 WPF。

如何使用可以在不同 TabControl 中重复使用的 UserControl 来执行此操作? 还有哪一个是TabControl样式中的“Header”ContentPresenter?

以下是在以下位置找到的说明 https://github.com/MahApps/MahApps.Metro/issues/281

另一种方法是修改/创建样式 - 然后问题是挂钩 以通用方式实现实际的“关闭”事件。

如果您查看 TabControl 样式,您会看到“标题” 内容演示者。如果您将其包装在堆栈面板中并添加一个按钮 像这样:

<StackPanel Orientation="Horizontal">
  <Label x:Name="root" FontSize="26.67">
      <ContentPresenter ContentSource="Header"  RecognizesAccessKey="True" />
  </Label>
  <Button Content="X" />
</StackPanel>

你得到:

如果您在 Window 或 UserControl(而不是资源 字典),您可以将其连接起来,以便 Click 可以触发,然后您可以 从数据绑定集合中或直接从 选项卡控件。

【问题讨论】:

    标签: c# wpf xaml mahapps.metro


    【解决方案1】:

    最简单的方法是使用MetroTabItem。它带有属性CloseButtonEnabled 来启用/禁用关闭按钮。您还可以将命令绑定到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>
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-09
      相关资源
      最近更新 更多