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

4

我创建了一个名为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)));
        }
    }
}

我将它包含在我的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>

但是只有“Two”和“Three”这两个选项卡被显示出来了。如果SmartTabItem继承自TabItem,为什么它没有出现在TabControl中呢?

我也遇到了完全相同的问题。感谢你提出这个问题。 - jjnguy
2个回答

5

要在SmartTabItem上使用默认样式的TabItem,请按照以下方式修改代码:

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

这将告诉 WPF 系统为您的选项卡使用 TabItem 的默认样式。否则,您的选项卡将真正无外观。

1

我猜测是因为您已经覆盖了其默认样式,但在Generic.xaml中没有为其提供样式。尝试注释掉此行以进行测试:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接