![](/img/trans.png)
[英]How to add custom-control-derived TabItem to TabControl in WPF?
[英]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.