Accessing the current theme and its paletteInstance

3 posts, 0 answers
  1. Stephane
    Stephane avatar
    4 posts
    Member since:
    Aug 2012

    Posted 02 Aug 2012 Link to this post

    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:

    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)


    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. Masha
    Masha avatar
    355 posts

    Posted 08 Aug 2012 Link to this post

    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.

    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. Kristoffer
    Kristoffer avatar
    30 posts
    Member since:
    Oct 2012

    Posted 12 Dec 2012 Link to this post

    I think it makes sense to add an API for this. Also, saving the theme in a variable is not reliable.
Back to Top