Search code examples
c#wpfxamltabcontroltabitem

Why is my TabItem custom control not showing up in the TabControl


I made a custom control called SmartTabItem, currently just the default implementation:

using System.Windows;
using System.Windows.Controls;

namespace TestControl.Controls
{
    public class SmartTabItem : TabItem
    {
        static SmartTabItem()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(SmartTabItem), new FrameworkPropertyMetadata(typeof(SmartTabItem)));
        }
    }
}

I include it in my TabControl like this:

<Window x:Class="TestControl.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:controls="clr-namespace:TestControl.Controls"
    Title="Window1" Height="300" Width="300">
    <DockPanel Margin="10">
        <TabControl>
            <controls:SmartTabItem Header="One">content of one</controls:SmartTabItem>
            <TabItem Header="Two">content of two</TabItem>
            <TabItem Header="Three">content of three</TabItem>
        </TabControl>
    </DockPanel>
</Window>

But only tabs "Two" and "Three" are displayed. Why isn't the SmartTabItem showing up in the TabControl if it inherits from TabItem?


Solution

  • To use the default style for a TabItem on your SmartTabItem, modify the code like this:

    DefaultStyleKeyProperty.OverrideMetadata(typeof(SmartTabItem), new FrameworkPropertyMetadata(typeof(TabItem)));
    

    This will tell the wpf system to use the TabItem's default style for your tab items. Otherwise, your tab item is truly lookless.