Edit this page

Setting a Theme on a Custom Control

This article explains how to apply a Theme for a control you have additionally extended inheriting any of the telerik controls.

When you are setting the theme to your extended control using StyleManager, you have to set the DefaultStyleKey in the OnInitialized method. This is needed as we are using a composite key to set the theme per control.

Lets say you have a custom control that inherits RadGridView. The snippet below demonstrates how to set any Telerik theme to it:

C#

public CustomTheme()
{
   public class MyCustomGrid:RadGridView
   {
       static MyCustomGrid()
       {
           DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomGrid), new FrameworkPropertyMetadata(typeof(MyCustomGrid)));
       }

       protected override void OnInitialized(EventArgs e)
       {
           base.OnInitialized(e);
           Theme theme = StyleManager.GetTheme(this);
           Type themeType = null;
           if (theme != null)
           {
               themeType = theme.GetType();
           }
           this.DefaultStyleKey = new ThemeResourceKey() { ElementType = typeof(RadGridView), ThemeType = themeType };
       }
   }
}