为什么我的TabItem自定义控件没有显示在TabControl中

Edw*_*uay 4 c# wpf xaml tabcontrol tabitem

我创建了一个名为SmartTabItem的自定义控件,目前只是默认实现:

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

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

我将它包含在我的TabControl中,如下所示:

<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>
Run Code Online (Sandbox Code Playgroud)

但只显示"两个"和"三个"标签.如果SmartTabItem继承自TabItem,为什么不在TabControl中显示?

Ken*_* K. 5

要使用您的默认样式,TabItemSmartTabItem修改如下代码:

DefaultStyleKeyProperty.OverrideMetadata(typeof(SmartTabItem), new FrameworkPropertyMetadata(typeof(TabItem)));
Run Code Online (Sandbox Code Playgroud)

这将告诉wpf系统使用TabItem标签项的默认样式.否则,您的标签项目看起来真的没有外观.