2012-12-03 55 views
0

我已經創建了自定義控件ColorToggleButton,它繼承了ToggleButton。在相應的.xaml文件中,ColorToggleButton的一個特定對象是通過TargetType和BasedOn ToggleButton。WPF樣式層次

<Style TargetType="ctl:ColorToggleButton" BasedOn="{StaticResource {x:Type ToggleButton}}"> 

這工作得很好,但如果我用x應用另一種風格的窗口:關鍵,因爲在

<Style x:Key="SameContent"><Setter Property="Content" Value="Same Content" /></Style> 
<ctl:ColorToggleButton Style={StaticResource SameContent} /> 

舊風格似乎得到完全被摧毀,並用新的更換。我可以通過使用支持算法FMP

<Style x:Key="SameContent" BasedOn="{StaticResource {x:Type ctl:ColorToggleButton}}"><Setter Property="Content" Value="Same Content" /></Style> 
<ctl:ColorToggleButton Style={StaticResource MyKey} /> 

規避這個問題,但是這似乎違反直覺的我,看到我彷彿是應用樣式到一個正常的切換按鈕或其他一些默認的控制,我不會用支持算法FMP屬性。這是實現自己的控件的標準方式嗎?我在做可怕的錯誤嗎?

編輯:ColorToggleButton的靜態構造函數如下:

static ColorToggleButton() 
{ 
    DefaultStyleKeyProperty.OverrideMetadata(typeof(ColorToggleButton), new FrameworkPropertyMetadata(typeof(ColorToggleButton))); 
} 

回答

1

在你的控制,你提供的靜態構造函數DefaultStyleKeyProperty覆蓋?

static ColorToggleButton() 
{ 
      DefaultStyleKeyProperty.OverrideMetadata(typeof(ColorToggleButton), new FrameworkPropertyMetadata(typeof(ColorToggleButton))); 
} 
+0

是的,我也更新了這個問題。 – user1787270