ArgumentException on setting DefaultStyleKey in custom control deriving from user control - Silverlight 4
I have created user control MyUserControl. Now I want to create custom control MyCustomControl that derives from MyUserControl. MyCustomControl.cs code is following:
public class MyCustomControl : MyUserControl
{
public MyCustomControl()
{
this.DefaultStyleKey = typeof(MyCustomControl);
}
}
I have Themes/Generic.xaml fil开发者_如何学运维e with the style
<Style TargetType="local:MyCustomControl">
...
</Style>
Instantiating MyCustomControl at runtime I get ArgumentException executing the line
this.DefaultStyleKey = typeof(MyCustomControl);
What am I missing?
Assigning a type that derives from UserControl
to DefaultStyleKey
is explicitly disallowed by throwing a ArgumentException
(why an ArgumentException and why no explanitory message is included only the SL team know).
A UserControl
cannot be templated receiving instead its own associated Xaml. Thats the whole point of UserControl
. You need to convert MyUserControl
into a templatable control was well if you wish to inherit off it in manner you are attempting.
精彩评论