2009-08-04 32 views
4

我做了所謂的SmartTabItem一個自定義的控制,目前只是默認的實現:爲什麼我的TabItem自定義控件沒有出現在的TabControl

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> 

但是隻顯示標籤「Two」和「Three」。 爲什麼SmartTabItem不在TabControl中顯示,如果它繼承自TabItem?

+0

我只是有完全相同的問題。感謝您提出問題。 – jjnguy 2009-10-24 01:37:23

回答

5

要使用一個TabItem默認樣式您SmartTabItem,修改這樣的代碼:

DefaultStyleKeyProperty.OverrideMetadata(typeof(SmartTabItem), new FrameworkPropertyMetadata(typeof(TabItem))); 

這將告訴WPF系統使用TabItem您的標籤項目的默認樣式。否則,你的標籤項目是真正看不見的。

1

我在猜測,因爲您已經重寫了它的默認樣式,但沒有在Generic.xaml中爲它提供樣式。試試這個行註釋掉測試:

DefaultStyleKeyProperty.OverrideMetadata(typeof(SmartTabItem), new FrameworkPropertyMetadata(typeof(SmartTabItem))); 
相關問題