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

4

我希望拥有自己的基础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

好的,我很蠢,应该是这样的

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

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