Search code examples
c#wpftabcontrolmahapps.metrotabitem

Why not show close button of metro tab item?


I am trying to add MetroTabItem by programmatically.

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>

When i adding like this,TabItem Header font size is working but CloseButtonEnabled is not working.Why please let me known.Thanks.


Solution

  • You used the wrong base Style (MetroTabItem) for your own Style. You must inherited from the keyless MetroTabItem Style like this:

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

    Hope this helps.