0

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.

1 Answers1

2

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.

punker76
  • 14,326
  • 5
  • 58
  • 96
  • What's the difference between those 2 ? – C1rdec Jan 05 '18 at 16:12
  • 3
    One of them is the custom TabItem class (the MetroTabItem) which uses an implicite theme style. The other one ist the name of style key (the MetroTabItem) which is set for all default WPF TabItems. So if you want to override the custom TabItem class MetroTabItem you must inherit from the theme style. – punker76 Jan 05 '18 at 20:52