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

时间:2010-03-16 16:49:21

标签: c# wpf user-controls controls wpf-controls

我想拥有自己的基础TabItem类,并使用从中派生的其他类。

我在MyNs名称空间中定义基类,如下所示:

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

这就是我为继承它的类所做的事情:

MyNs命名空间中的代码隐藏:

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

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>

我得到的错误是“XML名称空间”http://schemas.microsoft.com/winfx/2006/xaml/presentation'中不存在标记'MyCustomTab'。如果我在XAML中使用TabItem标记,则错误表明无法定义到不同的基类。

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

好的,我很蠢,应该是

<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>