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

How Can I use Dynamic Themes ??

5 Answers 230 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Ahmed
Top achievements
Rank 1
Ahmed asked on 13 Nov 2010, 01:41 PM
In My Silverlight Application which is Based in Telerik Controls ...... I want to Use Telerik Controls Themes but in Dynamic Way ....... So I want To Let User Select His Theme from Combo Box Appear in The Top of The Page and Change Telerik Controls Themes Automatic  So can U Give me A simple About How can I Handle It ??
(Note : Beside Telerik Controls I use Also Some Silverlight Controls (Containers) : Like Grid or Stack Panel So
1-Can I Give Telerik Themes to Silverlight Controls ?? If That is Possible so How Can I do That ?
2-If The Pre Question is not Possible So How Can I Handle Silverlight Controls if I Give it Static Colors with Telerik Thems ?

5 Answers, 1 is accepted

Sort by
0
Accepted
Vanya Pavlova
Telerik team
answered on 15 Nov 2010, 07:20 AM
Hello Ahmed,


I have prepared an example for you that demonstrates how to dynamically change the theme using RadComboBox. The Telerik themes are designed to work with several Microsoft Controls, such as ListBox, TextBox, Button..... If you want to dynamically change the background of a layout container such as Grid, StackPanel, WrapPanel you should do it manually.

Please see the attached example and if you need any further assistance let me know.
 
Kind regards,
Vanya Pavlova
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
G
Top achievements
Rank 1
answered on 26 Nov 2010, 03:30 PM
That's a nice example however you have to bind the theme to every control which looks like overkill.

Is there anyway you can do the following (Imagine a theme has been selected from a combobox) in code behind and have it take affect?

switch (themeName)
{
    case "Dark":
        StyleManager.ApplicationTheme = new Expression_DarkTheme();
        break;
    case "OfficeBlack":
        StyleManager.ApplicationTheme = new Office_BlackTheme();
        break;
    case "OfficeBlue":
        StyleManager.ApplicationTheme = new Office_BlueTheme();;
        break;

Currently you have to restart the xap to have a new theme take affect or is there something I'm missing here? Restarting is an annoyance in a browser and impossible OOB from what I've been able to find.

Thanks.
0
Vanya Pavlova
Telerik team
answered on 29 Nov 2010, 04:29 PM
Hi GF,


You can use code behind within RadComboBox's SelectionChanged event to achieve your goal, but each time you dynamically change the ApplicationTheme the view will be recreated. Having in mind this fact the preferable approach is to apply the theme using Style Manager to each control as it was demonstrated in my previous example.


Greetings,
Vanya Pavlova
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
0
Sven J
Top achievements
Rank 2
answered on 23 Feb 2011, 12:47 PM
I've encountered the same task - switching theme at runtime. I finally came up with this solution:
(just insert in App.xaml.cs)

        public Theme ApplicationTheme
        {
            get { return StyleManager.ApplicationTheme; }
            set
            {
                StyleManager.ApplicationTheme = value;
                Binding binding = new Binding("ApplicationTheme") { Source = this, Mode = BindingMode.TwoWay };
                foreach (var element in (RootVisual as FrameworkElement).AllChildren<FrameworkElement>().ToList())
                    element.SetBinding(Telerik.Windows.Controls.StyleManager.ThemeProperty, binding);
            }
        }

Extension, I used:
        public static IEnumerable<T> AllChildren<T>(this FrameworkElement ele, Func<T, bool> whereFunc = null) where T : FrameworkElement
        {
            var c = VisualTreeHelper.GetChildrenCount(ele);
            for (var i = 0; i < c; i++)
            {
                var child = VisualTreeHelper.GetChild(ele, i);
                if (child is T)
                {
                    var childOfTypeT = child as T;


                    if (whereFunc != null && !whereFunc(childOfTypeT))
                        continue;


                    yield return childOfTypeT;


                    foreach (var nextchild in childOfTypeT.AllChildren<T>(whereFunc))
                        yield return nextchild;
                }
            }
        }
0
Joel Palmer
Top achievements
Rank 2
answered on 07 Feb 2012, 12:15 AM
Sven,  I'd like to use your approach but I can't get it working.  Is your example using Silverlight 5; Visual Studio 2010?

I get errors on the [RootVisual as FrameworkElement).AllChildren] part of your code because AllChildren isn't an option on that item.

Maybe I don't have your references.  Is FrameworkElement from System.Windows?

Thanks, Joel
Tags
General Discussions
Asked by
Ahmed
Top achievements
Rank 1
Answers by
Vanya Pavlova
Telerik team
G
Top achievements
Rank 1
Sven J
Top achievements
Rank 2
Joel Palmer
Top achievements
Rank 2
Share this question
or