New to Telerik UI for WPF? Download free 30-day trial

Setting a Theme on a Custom Control

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

When you are setting the theme to your derived 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 from ScrollViewer. The snippet below demonstrates how to set any Telerik theme to it:

public class ExtendedScrollViewer : ScrollViewer 
{ 
    static ExtendedScrollViewer() 
    { 
        DefaultStyleKeyProperty.OverrideMetadata(typeof(ExtendedScrollViewer), new FrameworkPropertyMetadata(typeof(ExtendedScrollViewer))); 
    } 
 
    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(ScrollViewer), ThemeType = themeType }; 
    } 
} 

See Also

In this article