Creating your own ContentControl in Silverlight

Unbelievable, but I had to spend half an hour to find out this.

If you want to create your own ContentControl in Silverlight (for example, your custom Image control displaying a mirror), you can do it as follows:

1) Create a class inheriting from ContentControl.

2) In its ctor, write the following:

           DefaultStyleKey = GetType();

3) Create the folder “Themes”, put the file generic.xaml there and insert the following content:


    <Style TargetType=”local:<yourtype>“>
        <Setter Property=”Template”>
                <ControlTemplate TargetType=”local:<yourtype>“>
                        <ContentPresenter />

Now you can implement your control as usual, and it will get the XAML above as its ContentTemplate by default.

Leave a comment