繁体   English   中英

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

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

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

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

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

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

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

我猜是因为你已经覆盖了它的默认样式,但是在Generic.xaml中没有为它提供样式。 尝试注释掉这一行来测试:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM