Changing theme in runtime
1 Answer 11 Views
Piotr
Top achievements
Rank 1
Piotr asked on 02 Jun 2021, 03:45 PM

I have a panel class ExRadPanel, that inherits from RadPanel. I try to apply to it a copy of the style that is used for PropertyGridHelpElement in RadPropertyGrid. Below is my test code, but it doesn't work properly. Some style is attached to FillPrimitive inside ExRadPanel, but the visual effect is not like I expect.

 Is that possible to make modify a theme in runtime to make a copy of the required style setting with proper settings? 

private static void FixTheme(Theme theme)
{
    // try to add registration
    var sg = theme.FindStyleGroup(typeof(RadPropertyGrid).FullName);
    sg.Registrations.Add(new MyStyleRegistration
    {
        RegistrationType = "ElementTypeControlType",
        ElementType      = "Telerik.WinControls.RootRadElement",
        ControlType      = typeof(ExRadPanel).FullName
    });
    sg.Registrations.Add(new MyStyleRegistration
    {
        RegistrationType = "ElementTypeControlType",
        ElementType      = typeof(RadPanelElement).FullName,
        ControlType      = typeof(ExRadPanel).FullName
    });

    var copy = sg.PropertySettingGroups.ToArray();
    foreach (var settingGroup in copy)
    {
        if (settingGroup.Selector is null)
            continue;
        if (settingGroup.Selector.Value != nameof(PropertyGridHelpElement))
            continue;
        {
            // trying this ... 
            var newSg = new PropertySettingGroup
            {
                Selector = new ElementSelector(settingGroup.Selector.Type, nameof(RadPanelElement)),
                BasedOn  = settingGroup.BasedOn
            };
            newSg.Repositories.AddRange(settingGroup.Repositories);
            newSg.PropertySettings.AddRange(settingGroup.PropertySettings);
            sg.PropertySettingGroups.Add(newSg);
        }
        {
            // ... and this    
            var newIi = new PropertySettingGroup
            {
                Selector = new ElementSelector(ElementSelectorTypes.TypeSelector, typeof(FillPrimitive).FullName),
                BasedOn  = settingGroup.BasedOn
            };
            newIi.Repositories.AddRange(settingGroup.Repositories);
            newIi.PropertySettings.AddRange(settingGroup.PropertySettings);
            sg.PropertySettingGroups.Add(newIi);
        }
    }
}

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 03 Jun 2021, 05:36 AM
Hello, Piotr,


Modifying a theme by code is possible even though it is not the most straightforward approach. It is necessary to clone the theme and change the desired repository values that are currently applied to the respective control elements. However, you need to pay attention to what repositories are available in this particular theme and modify them. Then, apply this cloned theme to the respective control. 

The following code snippet demonstrates how to iterate through all repositories and how to access some specific style group at run time:

 //iterate through all repositories
Theme theme = ThemeRepository.FindTheme("ControlDefault");
if (theme != null)
{
    foreach (StyleRepository repository in theme.Repositories)
    {
        
         
    }
}
 
//access some specific style group
Theme controlDefaultTheme = ThemeRepository.FindTheme("ControlDefault");
StyleGroup group = controlDefaultTheme.FindStyleGroup("Telerik.WinControls.UI.RadGridView");
 
foreach (PropertySettingGroup settingGroup in group.PropertySettingGroups)
{
    if (settingGroup.Selector.Value == "GridHeaderCellElement")
    {
        foreach (PropertySetting property in settingGroup.PropertySettings)
        {
            if (property.Name == "BackColor2")
            {
                 
            }
        }
    }
}

I would like to note that the different themes in the Telerik UI for WinForms suite have their own implementation. You can't reply on the fact that the names of the repository settings will be identical in all themes. Usually, manipulating a theme at run time is not a recommended approach as it would include a lot of code for even simple customizations. 

The easiest way to customize a theme is via using Visual Style Builder. The following KB article demonstrates a step-by-step tutorial how to use them: https://docs.telerik.com/devtools/winforms/knowledge-base/customize-a-theme

You can read more details about using the Visual Style Builder here:

1.     Loading predefined themes
2.     Working with Repository Items
3.     Saving and Loading Theme Files
4.     Loading Themes from an External File
5.     Loading Themes from a Resource
6.     Applying Theme to a Control
7.     http://tv.telerik.com/watch/winforms/visualstylebuilder/whats-new-visual-style-builder-q1-2010

In case you are still experiencing any further difficulties, it would be greatly appreciated if you can provide more details about the exact design that you are trying to achieve and which is the theme that you want to customize. A sample screenshot would be very useful in understanding better what is the exact goal. Thus, we would be able to think about a suitable solution and provide further assistance. Thank you. 

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Asked by
Piotr
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or