Modifying theme properties at runtime.

2 posts, 0 answers
  1. Craig
    Craig avatar
    1 posts
    Member since:
    Feb 2012

    Posted 04 Jan 2013 Link to this post

    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!

  2. Anton
    Admin
    Anton avatar
    168 posts
    Member since:
    Oct 2014

    Posted 09 Jan 2013 Link to this post

    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.

Back to Top