简体   繁体   English

如何在WPF中向TabControl添加自定义控件派生的TabItem?

[英]How to add custom-control-derived TabItem to TabControl in WPF?

I want to have my own base TabItem class and use other classes that derive from it. 我想拥有自己的基类TabItem类,并使用从中派生的其他类。

I define base class in MyNs namespace like this: 我在MyNs命名空间中定义基类,如下所示:

public class MyCustomTab : TabItem
{
    static MyCustomTab()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomTab), new  FrameworkPropertyMetadata(typeof(TabItem)));
    }
}

And this is what I do for the class that inherits from it: 这就是我为继承它的类所做的事情:

code-behind in MyNs namespace: MyNs命名空间中的代码隐藏:

public partial class ActualTab : MyCustomTab
{
    public ActualTab()
    {
        InitializeComponent();
    }
}

XAML: XAML:

<MyCustomTab x:Class="MyNs.ActualTab"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>

</Grid>
</MyCustomTab>

The error I get is "The tag 'MyCustomTab' does not exist in XML namespace ' http://schemas.microsoft.com/winfx/2006/xaml/presentation '". 我得到的错误是“标签'MyCustomTab'在XML名称空间' http://schemas.microsoft.com/winfx/2006/xaml/presentation '中不存在。” If I use TabItem tag in XAML the error says that it's not possible to define to different base classes. 如果我在XAML中使用TabItem标记,则错误表明无法定义到不同的基类。

How to fix this? 如何解决这个问题?

Ok, I'm stupid, it should've been 好吧,我很蠢,应该是

<MyNs:MyCustomTab x:Class="MyNs.ActualTab"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:MyNs="clr-namespace:MyNs">
<Grid>

</Grid>
</MyNs:MyCustomTab>

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

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