为什么不显示地铁标签项目的关闭按钮?

时间:2018-01-05 09:14:08

标签: c# wpf tabcontrol mahapps.metro tabitem

我正在尝试通过编程方式添加MetroTabItem

MainWindow.xaml.cs

private void AddTabItem(UserControl control,string Header)
        {
            MetroTabItem mahtab = new MetroTabItem();
            mahtab.Content = control;
            mahtab.DataContext = control;
            mahtab.Header = Header;
            mahtab.CloseButtonEnabled = true;
            mahtab.Style = (Style)FindResource("TabItem");
            mahtab.IsSelected = true;
            maintab.Items.Add(mahtab);
        }

MainWindow.xaml

<Style x:Key="TabItem" BasedOn="{StaticResource MetroTabItem}" TargetType="{x:Type Controls:MetroTabItem}">
            <Setter Property="CloseButtonEnabled" Value="True"></Setter>
            <Setter Property="Controls:ControlsHelper.HeaderFontSize" Value="20"></Setter>        

        </Style>

当我这样添加时,TabItem标题字体大小正常,但CloseButtonEnabled无效。为什么请让我知道。谢谢。

1 个答案:

答案 0 :(得分:1)

您使用了错误的基本样式(MetroTabItem)作为您自己的样式。您必须继承无密钥MetroTabItem样式,如下所示:

<Style x:Key="TabItem" BasedOn="{StaticResource {x:Type Controls:MetroTabItem}}" TargetType="{x:Type Controls:MetroTabItem}">
  <Setter Property="CloseButtonEnabled" Value="True"></Setter>
  <Setter Property="Controls:ControlsHelper.HeaderFontSize" Value="20"></Setter>        
</Style>

希望这有帮助。