This is a migrated thread and some comments may be shown as answers.

Modifying theme properties at runtime.

1 Answer 240 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Craig
Top achievements
Rank 1
Craig asked on 04 Jan 2013, 11:46 AM
I have developed a custom theme for WinForms (2012.3.1211.40) and am very happy with it.

However, I have one small requirement for a background color change (RadButton) on hover. I would hate to copy the theme (and manage 2 different themes) for this small request.

I found "ButtonElement" and "RootElement" properties on the RadButton. How do I modify values (lets say, a background color) for different states, particularly, the hover state?

Thanks in advance!

1 Answer, 1 is accepted

Sort by
0
Anton
Telerik team
answered on 09 Jan 2013, 08:55 AM
Hi Craig,

I am copying the answer from your ticket to the forum as well, so the community can benefit from it.

There are several approaches to solve this issue.

1. The simplest one: you can create a custom class that inherits from RadButton and handle its events to change color properties with code.

2. You can set a property change behavior similar to the following one:
public class ColorChangePropertyBehavior : PropertyChangeBehavior
{
    public ColorChangePropertyBehavior()
        : base(RadItem.IsMouseOverProperty)
    {
    }
 
    public override void OnPropertyChange(RadElement element, RadPropertyChangedEventArgs e)
    {
        base.OnPropertyChange(element, e);
            RadButtonElement buttonElement = (RadButtonElement)element;
        if ((bool)e.NewValue)
        {
            buttonElement.ButtonFillElement.BackColor = Color.Red;
            buttonElement.ButtonFillElement.BackColor2 = Color.Red;
            buttonElement.ButtonFillElement.BackColor3 = Color.Red;
            buttonElement.ButtonFillElement.BackColor4 = Color.Red;
        }
        else
        {
            buttonElement.ButtonFillElement.ResetValue(FillPrimitive.BackColorProperty, ValueResetFlags.Local);
            buttonElement.ButtonFillElement.ResetValue(FillPrimitive.BackColor2Property, ValueResetFlags.Local);
            buttonElement.ButtonFillElement.ResetValue(FillPrimitive.BackColor3Property, ValueResetFlags.Local);
            buttonElement.ButtonFillElement.ResetValue(FillPrimitive.BackColor4Property, ValueResetFlags.Local);
        }
    }
}

Use the code below to assign the property change behavior with RadButton:
button.ButtonElement.AddBehavior(new ColorChangePropertyBehavior());

3. The third option uses a cloned theme and changes its repository values:
Theme controlDefaultTheme = ThemeRepository.FindTheme("ControlDefault");
Theme clonedTheme = (Theme)controlDefaultTheme.Clone();
clonedTheme.Name = "CustomTheme";
StyleRepository repository = clonedTheme.FindRepository("ButtonMouseOverFill");
repository.FindSetting("BackColor").Value = Color.Red;
repository.FindSetting("BackColor2").Value = Color.Red;
repository.FindSetting("BackColor3").Value = Color.Red;
repository.FindSetting("BackColor4").Value = Color.Red;
ThemeRepository.Add(clonedTheme);

I hope this helps.

Kind regards,
Anton
the Telerik team
Q3'12 SP1 of RadControls for WinForms is out now. See what's new.
Tags
GridView
Asked by
Craig
Top achievements
Rank 1
Answers by
Anton
Telerik team
Share this question
or