WPF TabItem (Custom Control) does not apply style when add in runtime

Friends, I have a tabcontrol with the following resource:

<TabControl>
   <TabControl.Resources>
      <Style TargetType="{x:Type TabItem}">
         <!-- setters etc -->
      </Style>
   </TabControl.Resources>

   <TabItem Header="FILES"/>
   <TabItem Header="DEBUG"/>
</TabControl>

The tabItem elements that are added from the xaml do display the style, the problem I have is that when I add a tabitem (wpf usercontrol) at runtime it does not apply the style, here an example with two cases.

  • case 1 (it works)
var tab = new TabItem()
{
   Header = "OTHER MENU",
   IsSelected = true
};

tabcontrol.Items.Add(tab);
  • case 2 (doesn't work)
var tab = new Modules.Customer.tab__list()
{
   IsSelected = true
};

tabcontrol.Items.Add(tab);

Modules.Customer.tab__list is this:

XAML code

<TabItem x:Class="Solucion.Presentacion.Modulos.Perfil.tab__listado"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Solucion.Presentacion.Modulos.Perfil"
             mc:Ignorable="d" Header="Perfiles">
   <!-- CODE -->
</TabItem>

C# code

public partial class tab__list : TabItem 
{
   public tab__list()
   {
      InitializeComponent();
   }
}

Any idea how to fix this.

EDIT:

When i add control in xaml the style doesn't work.

<TabControl>
   <TabControl.Resources>
      <Style TargetType="{x:Type TabItem}">
         <!-- setters etc -->
      </Style>
   </TabControl.Resources>

   <TabItem Header="FILES"/>
   <TabItem Header="DEBUG"/>
   <ctrl:tab__list/> <!-- DOESN'T WORK TOO -->
</TabControl>

Solution 1:

First of all, TargetType="{x:Type TabItem}" doesn't target your own tabitem. To avoid duplicating the complete TabItem style, the Style for the custom TabItem should be based on the original TabItem Style by referencing it in the Style.BasedOn property: BasedOn="{StaticResource {x:Type TabItem}}" So your style frame should be like that

   <TabControl.Resources>
       <Style TargetType="{x:Type local:MyTabItem}"
              BasedOn="{StaticResource {x:Type TabItem}}">
           <Setter Property="Background" Value="Red"/>
       </Style>
   </TabControl.Resources>

Customer.tab__list" should be a regular class, it doesn't need to be XAML file. Create a new regular class. it should be "tab__list.cs"

public class MyTabItem : TabItem
{
    public MyTabItem()
    {
        Header = "Perfiles";
    }
}

I made something like that. As i said in comment i use local:MyTabItem because my control is in my local. xmlns:local="clr-namespace:DemoThings"

   <TabControl x:Name="TabControl">

            <TabControl.Resources>
                <Style TargetType="{x:Type local:MyTabItem}">
                    <Setter Property="Background" Value="Red"/>
                </Style>
            </TabControl.Resources>

        <TabItem Header="one regular tab">

        </TabItem>
    </TabControl>

And then i made

TabControl.Items.Add(new MyTabItem());

Result:enter image description here