EvAlex
EvAlex

Reputation: 2938

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 file 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?

Upvotes: 2

Views: 682

Answers (1)

AnthonyWJones
AnthonyWJones

Reputation: 189477

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.

Upvotes: 2

Related Questions