【问题标题】:How to add custom-control-derived TabItem to TabControl in WPF?如何将自定义控件派生的 TabItem 添加到 WPF 中的 TabControl?
【发布时间】:2011-01-28 05:15:21
【问题描述】:

我想拥有自己的基本 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 标记,则错误表示无法定义不同的基类。

如何解决这个问题?

【问题讨论】:

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


    【解决方案1】:

    好吧,我傻了,应该是这样

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-06
      • 2018-04-21
      • 1970-01-01
      • 2013-11-23
      • 2010-10-19
      • 1970-01-01
      • 2010-11-12
      相关资源
      最近更新 更多