Dynamic Implicit theme issue with BasedOn Style

4 posts, 0 answers
  1. Niko
    Niko avatar
    14 posts
    Member since:
    Jul 2012

    Posted 20 Feb 2014 Link to this post

    Hello,

    I have implemented implicit themes using the Merged Dictionary approach documented on your help site. For changing themes, I clear the application merged dictionary then re-add the dictionaries for the new theme. This works fine, but for many of our controls we also need to apply other styles. We used the 'BasedOn' property of the style like below:

    <Style TargetType="telerik:RadComboBox" BasedOn="{StaticResource RadComboBoxStyle}">
         <Setter Property="Padding" Value="5" />
    </Style>

    When the merged dictionary gets cleared and the new theme applied, it seems that some of these controls using BasedOn do NOT get updated properly. My guess would be that since it's a StaticResource, it will not get updated when the merged dictionary is reset.

    Do you guys know of any workaround or a fix for this?

    Thanks
  2. Vanya Pavlova
    Admin
    Vanya Pavlova avatar
    2019 posts

    Posted 24 Feb 2014 Link to this post

    Hi Niko,



    Generally resources in a merged dictionary occupy a location in the resource lockup scope that is just after the scope of the main resource dictionary they are merged into. What you may do is to isolate these your custom styles in a separate resource dictionary and add it as the last one each time you change the theme. Let's say that you stored these styles in a file with name CustomStyles.xaml with following content:

                          xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Style TargetType="telerik:RadComboBox" BasedOn="{StaticResource RadComboBoxStyle}">
               <Setter Property="Padding" Value="5" />
         </Style>
    </ResourceDictionary>

    Then in your logic for switching the themes you may merge it, as follows:

    foreach (string component in themeComponents)
    {
        string fullName = String.Format("{0}.{1};{2}", baseName, themeName, component);
        Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
        {
            Source = new Uri(fullName, UriKind.RelativeOrAbsolute)
        });
          
    }
    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
    {
        Source = new Uri("CustomStyles.xaml", UriKind.RelativeOrAbsolute)
    });

    Then the styles contained in this custom resource dictionary will be evaluated each time you change the theme.

    Regards,
    Vanya Pavlova
    Telerik
  3. UI for WPF is Visual Studio 2017 Ready
  4. Niko
    Niko avatar
    14 posts
    Member since:
    Jul 2012

    Posted 25 Feb 2014 in reply to Vanya Pavlova Link to this post

    Thank you, Vanya.

    This seems to have solved my problem, but I'm still seeing an issue that is related. 

    When the themes are changed at runtime, I clear the merged dictionaries and then re-add them similar to this: 
    Application.Current.Resources.MergedDictionaries.Clear();
    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() {
            Source = new Uri("/Telerik.Windows.Themes.Office_Black;component/Themes/System.Windows.xaml", UriKind.RelativeOrAbsolute)});
    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() {
            Source = new Uri("/Telerik.Windows.Themes.Office_Black;component/Themes/Telerik.Windows.Controls.xaml", UriKind.RelativeOrAbsolute)});

    This will work with mixed results. One out of three times, this code will hang my application. I see in Task Manager that my application's CPU usage will go up and down, yet the user interface is completely frozen.

    Have you seen this or have any advice? Thank you
  5. Vanya Pavlova
    Admin
    Vanya Pavlova avatar
    2019 posts

    Posted 28 Feb 2014 Link to this post

    Hi Niko,



    Thank you for getting back to us! Actually we are not aware of a specific problem, increasing the CPU usage when switching implicit styles. I believe that the behavior you experience is a project specific.

    Would it be possible to try to isolate the problem in a small repro project and send it to us as an attachment in a new support thread? 


    I look forward to hearing from you! 


    Regards,
    Vanya Pavlova
    Telerik
Back to Top