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

Accessing the current theme and its paletteInstance

2 Answers 468 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Stephane
Top achievements
Rank 1
Stephane asked on 02 Aug 2012, 10:46 AM
Hello,
I'm working on an app where users may switch a theme from within the supported telerik themes (metro, vista, office_black, etc...).
The theme is kept as a preference at user level and restored at login.

Now in order to remain consistent with the current theme in use, I want to avoid using any predifines fixed brushes and use the theme palette instead. So I'm facing 2 problems:

1) Retreiving the current application theme in use.
As I load themes using the documented method:

Application.Current.Resources.MergedDictionaries.Clear()
Dim res1 As ResourceDictionary = New ResourceDictionary()
 res1.Source = New Uri("/Telerik.Windows.Themes.Expression_Dark;component/Themes/System.Windows.xaml", UriKind.RelativeOrAbsolute)
Dim res2 As ResourceDictionary = New ResourceDictionary()
res2.Source = New Uri("/Telerik.Windows.Themes.Expression_Dark;component/Themes/Telerik.Windows.Controls.xaml", UriKind.RelativeOrAbsolute)
Dim res3 As ResourceDictionary = New ResourceDictionary()
  res3.Source = New Uri("/Telerik.Windows.Themes.Expression_Dark;component/Themes/Telerik.Windows.Controls.Input.xaml", UriKind.RelativeOrAbsolute)
...

       
Application.Current.Resources.MergedDictionaries.Add(res1)
Application.Current.Resources.MergedDictionaries.Add(res2)
Application.Current.Resources.MergedDictionaries.Add(res3)

In order to retreive the current theme I haven't found anything prettier than checking the current merged dictionnaries and extracting the theme name from there:

Dim str As String = My.Application.Current.Resources.MergedDictionaries(1).Source.OriginalString
str = Strings.Mid(str, 25, str.Length)
str = Left(str, str.IndexOf(";"))
'Returns the Theme's name in use


I can do that or just keep track of the current them when it's loaded.
But isn't there any more obvious way of accessing the current theme in use?

2) Using the current Theme pallete
Once I get the theme name, I could decide of an approriate color for the theme in use...
But, for a metro style i have found the following is possible:
Dim colorPalette = MetroColors.PaletteInstance
Dim converter As New System.Windows.Media.BrushConverter
Me.gridStatus.Background = DirectCast(converter.ConvertFromString(colorPalette.AccentColor.ToString), Brush)

I can alter my interface using the colorpalette maincolor, accentcolor, strongColor, etc... and remain consistent with the theme.
How do I get a color pallete's ref of the other themes (Expression_Dark, Vista, Summer, etc...) and achieve the same result.

Many thanks.

2 Answers, 1 is accepted

Sort by
0
Masha
Telerik team
answered on 08 Aug 2012, 07:43 AM
Hi Stephane,

Sorry for the late reply.

Straight to your questions:

1. We do not provide a direct way to get the currently set theme when using implicit styles. I would suggest to save the theme name in a variable and update it when a new theme is loaded.

2. Unfortunately this approach is applicable only to Metro theme which is especially designed to work in this way - MetroColors class keeps a singleton object in its PaletteInstance property with the colors for the Metro theme. So you can use this in code-behind. We are sorry for inconvenience.


Greetings,
Maria
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Kristoffer
Top achievements
Rank 1
answered on 12 Dec 2012, 08:22 AM
I think it makes sense to add an API for this. Also, saving the theme in a variable is not reliable.
Tags
General Discussions
Asked by
Stephane
Top achievements
Rank 1
Answers by
Masha
Telerik team
Kristoffer
Top achievements
Rank 1
Share this question
or