2012-12-03 140 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