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

Apply theme in a mvvm wpf application (caliburn.micro)

9 Answers 705 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Oliver
Top achievements
Rank 1
Oliver asked on 22 May 2016, 10:33 PM

I am just trying to apply a theme in a mvvm wpf caliburn.micro app. I added references for the necessary no-xaml wpf binaries (Telerik.Windows.Controls/Navigation, Telerik.Windows.Data (Binaries.NoXAML), in addition I added a reference to the required theme (Telerik.Windows.Themes.Green) and added the resources in a merged resource dictionary in my app.xaml.

I still can't get theming to work. Using implicit styled controls, I can't see any data displayed, but not using implicit styles everything is ok!

1) what am I missing?

2) And where should I apply e.g. : GreenPalette.LoadPreset(GreenPalette.ColorVariation.Dark) - in my bottstrapper?;

Thanks for your help.

Oliver

app.xaml

01.<Application x:Class="Notizen.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Notizen">
02.    <Application.Resources>
03.        <ResourceDictionary>
04.            <ResourceDictionary.MergedDictionaries>
09.                <ResourceDictionary Source="/Telerik.Windows.Themes.Green;component/Themes/Telerik.Windows.Controls.xaml" />
10.                <ResourceDictionary Source="/Telerik.Windows.Themes.Green;component/Themes/Telerik.Windows.Controls.Navigation.xaml" />-->
11.                <ResourceDictionary Source="/Telerik.Windows.Themes.Green;component/Themes/System.Windows.xaml" />
12.                <ResourceDictionary Source="/Telerik.Windows.Themes.Green;component/Themes/Telerik.Windows.Controls.xaml" />
13.                <ResourceDictionary Source="/Telerik.Windows.Themes.Green;component/Themes/Telerik.Windows.Controls.Navigation.xaml" />
14.                <ResourceDictionary>
15.                    <local:AppBootstrapper x:Key="bootstrapper" />
16.                </ResourceDictionary>
17.            </ResourceDictionary.MergedDictionaries>
18.        </ResourceDictionary>
19.    </Application.Resources>
20.</Application>

 

appbootsrapper.cs

01.namespace Notizen {
02.    using System;
03.    using System.Collections.Generic;
04.    using Caliburn.Micro;
05.    using System.ComponentModel.Composition.Hosting;
06.    using System.Linq;
07.    using System.ComponentModel.Composition;
08.    using System.Windows;
09.    using Telerik.Windows.Controls;
10.    public class AppBootstrapper : BootstrapperBase {
11. 
12.        private CompositionContainer container;
13. 
14.        public AppBootstrapper()
15.        {
16.            Initialize();
17.        }
18. 
19.        protected override void Configure()
20.        {
21.            container = new CompositionContainer(
22.                new AggregateCatalog(AssemblySource.Instance.Select(x => new AssemblyCatalog(x)))
23.                );
24. 
25.            var batch = new CompositionBatch();
26. 
27.            batch.AddExportedValue<IWindowManager>(new WindowManager());
28.            //batch.AddExportedValue<IWindowManager>(new TelerikWindowManager());
29.            batch.AddExportedValue<IEventAggregator>(new EventAggregator());
30.            batch.AddExportedValue(container);
31. 
32.            // This is essential to enable Telerik's conventions
33.            TelerikExtensions.TelerikConventions.Install();
34. 
35.            GreenPalette.LoadPreset(GreenPalette.ColorVariation.Dark);
36.            //StyleManager.ApplicationTheme = ThemeManager.FromName("Expression_Dark");
37. 
38.            container.Compose(batch);
39.        }
40. 
41.        protected override object GetInstance(Type serviceType, string key)
42.        {
43.            string contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(serviceType) : key;
44.            var exports = container.GetExportedValues<object>(contract);
45. 
46.            if (exports.Any())
47.                return exports.First();
48. 
49.            throw new Exception(string.Format("Could not locate any instances of contract {0}.", contract));
50.        }
51. 
52.        protected override IEnumerable<object> GetAllInstances(Type serviceType)
53.        {
54.            return container.GetExportedValues<object>(AttributedModelServices.GetContractName(serviceType));
55.        }
56. 
57.        protected override void BuildUp(object instance)
58.        {
59.            container.SatisfyImportsOnce(instance);
60.        }
61. 
62.        protected override void OnStartup(object sender, StartupEventArgs e)
63.        {
64.            DisplayRootViewFor<IShell>();
65.        }
66. 
67.    }
68.}

tagsView.xaml

01.<UserControl x:Class="Notizen.Views.TagsView"
03.             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
04.             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
05.             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
06.             xmlns:local="clr-namespace:Notizen.Views"
07.             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
08.             xmlns:vm="clr-namespace:Notizen.ViewModels"
09.             mc:Ignorable="d"
10.             d:DesignHeight="300" d:DesignWidth="300">
11.    <UserControl.Resources>
12. 
13.            <Style x:Key="radTreeViewItemStyle" TargetType="{x:Type telerik:RadTreeViewItem}">
14.                <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
15.                <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
16.                <Setter Property="FontWeight" Value="Normal" />
17.                <Style.Triggers>
18.                    <Trigger Property="IsSelected" Value="True">
19.                        <Setter Property="FontWeight" Value="Bold" />
20.                    </Trigger>
21.                </Style.Triggers>
22.            </Style>
23. 
24.        <HierarchicalDataTemplate x:Key="hierarchicalDataTemplate"
25.                          DataType="{x:Type vm:TagViewModel}"
26.                          ItemsSource="{Binding Tags}">
27.            <DockPanel LastChildFill="True">
28.                <StackPanel DockPanel.Dock="Right" Orientation="Horizontal" HorizontalAlignment="Right">
29.                    <TextBlock Text="{Binding Count}" Padding="0,0,5,0" Foreground="Gray" />
30.                </StackPanel>
31.                <StackPanel DockPanel.Dock="Left" Orientation="Horizontal">
32. 
33.                    <!--<Image Width="16" Height="16" Margin="3,0" Source="Images\RegionXX.png" />-->
34.                    <TextBlock Text="{Binding Name}" Foreground="#FF1E4BAC" />
35.                </StackPanel>
36. 
37.            </DockPanel>
38.        </HierarchicalDataTemplate>
39.    </UserControl.Resources>
40.    <DockPanel LastChildFill="True">
41.        <TextBlock DockPanel.Dock="Top" Background="CornflowerBlue" x:Name="Message" />
42.        <telerik:RadTreeView x:Name="Tags" DockPanel.Dock="Bottom"
43.                             ItemsSource="{Binding Path=Tags}"
44.                             ItemTemplate="{StaticResource hierarchicalDataTemplate}"
45.                             ItemContainerStyle="{StaticResource radTreeViewItemStyle}"
46.                             
47.                             >
48.            <!--<telerik:RadTreeViewItem Name="Item" Header="Root"></telerik:RadTreeViewItem>-->
49.            <!--Background="#FFE2E8F2"-->
50.            <!--ItemsSource="{Binding Path=Tags}"-->
51.            <!--<telerik:RadTreeView.Items>
52.                <telerik:RadTreeViewItem Name="Item" Header="Root"></telerik:RadTreeViewItem>
53.            </telerik:RadTreeView.Items>-->
54. 
55.        </telerik:RadTreeView>
56.    </DockPanel>
57.</UserControl>

 

tagsViewModel.cs

01.using Caliburn.Micro;
02.using System;
03.using System.Collections.Generic;
04.using System.Collections.ObjectModel;
05.using System.Linq;
06.using System.Text;
07.using System.Threading.Tasks;
08. 
09.namespace Notizen.ViewModels
10.{
11.    public class TagsViewModel : Screen
12.    {
13.        private TagViewModel tag;
14. 
15.        public string Message { get; set; }
16. 
17.        public TagsViewModel()
18.        {
19.            Tags = new ObservableCollection<TagViewModel>();
20.        }
21. 
22.        public TagViewModel RootTag
23.        {
24.            get
25.            {
26.                return this.tag;
27.            }
28. 
29.            set
30.            {
31.                this.tag = value;
32.                Tags.Add(value);
33.                NotifyOfPropertyChange(()=> Tags);
34.            }
35.        }
36. 
37.        public ObservableCollection<TagViewModel> Tags
38.        {
39.            get;
40.            set;
41.        }
42.    }
43.}

 

shellviewModel.cs

 

01.using System;
02.using Notizen.ViewModels;
03.using Notizen.Model;
04.using System.Linq;
05.using Caliburn.Micro;
06.using System.Collections.ObjectModel;
07.using System.ComponentModel.Composition;
08. 
09.namespace Notizen {
10.    [Export(typeof(IShell))]
11.    public class ShellViewModel : Caliburn.Micro.PropertyChangedBase, IShell {
12. 
13.        private TagViewModel _tags;
14.        public TagViewModel TagViewModel
15.        {
16.            get { return _tags; }
17.            set {
18.                if (_tags != value) return;
19.                _tags = value;
20.                NotifyOfPropertyChange(()=> TagViewModel);
21.            }
22.        }
23. 
24.        private string _helloText;
25. 
26.        public string HelloText
27.        {
28.            get { return _helloText; }
29.            set { _helloText = value; }
30.        }
31. 
32.        public TagsViewModel Tags { get; set; }
33. 
34. 
35.        public ShellViewModel()
36.        {
37.            LoadTags();
38.            HelloText = "Hello!";
39.             
40.        }
41. 
42.        private void LoadTags()
43.        {
44.            using (var context = new NotizenDbContext())
45.            {
46.                var roottag = context.Tags.Where(x => x.TagParentId == null).FirstOrDefault();
47. 
48.                //var tagViewModel = new TagViewModel(roottag, null);
49. 
50.                //TagViewModel = tagViewModel;
51. 
52.                Tags = new TagsViewModel() { Message = "Message from TestViewModel" , RootTag = tagViewModel  };
53.            }
54.        }
55.    }
56.}

 

9 Answers, 1 is accepted

Sort by
0
Martin
Telerik team
answered on 25 May 2016, 03:34 PM
Hello Oliver,

I can see that you are not basing custom RadTreeViewItem Style you are using in your application on the predefined style for the control. That would mean that you do not get the ControlTemplate that is set in our default styling. You can include the BasedOn attribute on the custom style and set it to the default RadTreeViewItem style. 
<Style x:Key="radTreeViewItemStyle" TargetType="{x:Type telerik:RadTreeViewItem}" BasedOn="{StaticResource RadTreeViewItemStyle}">
This way your Style would inherit all the properties that we have set up for the control including its template. This should fix the problem you are currently having with theming and you can override anything that you would like to change. If you want to make modifications to anything in the ControlTemplate, you should copy the whole Setter for the ContorlTemplate and then make modifications to it. If you need, you can see more about extracting the whole Style and ControlTemplate in this article.

Note that this information is valid only for a scenario with NoXaml binaries and implicit styles. If you are using Xaml dlls the it is not necessary to base the custom style on the default one.

You could put the GreenPalette.LoadPreset(GreenPalette.ColorVariation.Dark); after the InitializeComponent(); in the bootstrapper, so all the brushes will be loaded as early as possible. However, I should think that it would not be necessary, since the default variation of the Green theme is the Dark one.

Regards,
Martin
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Oliver
Top achievements
Rank 1
answered on 26 May 2016, 08:28 AM

Thanks, the "BasedOn="{StaticResource RadTreeViewItemStyle}" did the trick.

But the rest of the RadTreeView Control except for RadTreeViewItems does not seem to have the green theme dark styling applied, even after adding 

<Style TargetType="{x:Type telerik:RadTreeView}" BasedOn="{StaticResource RadTreeViewStyle}">
    <!--<Setter Property="Background" Value="Black" />-->
</Style>

1.) Still missing anything??

2.) How would I swith from dark to light variation in this mvvm scenario?

3.) How would I change a theme in a mvvm scenario?

 

0
Martin
Telerik team
answered on 31 May 2016, 07:43 AM
Hi,

By design the default background for the RadTreeView in Green theme is transparent, because it is reused in other controls, so I would guess that its Background in your application appears white. You can set it to some of the default brushes for the GreenDark theme (e.g. PrimaryColorBrush or AlternativeColorBrush), or set the Background of any of its parents (e.g. the Window) to a desired dark color.

About changing themes at run-time - you can look at this article in our documentation, which explains the desired behavior with code snippets. And for the Light variation of the Green theme
add GreenPalette.LoadPreset(GreenPalette.ColorVariation.Light).

Regards,
Martin
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Oliver
Top achievements
Rank 1
answered on 02 Jun 2016, 02:18 PM

Hi, thanks for your response.

I finally managed to switch themes from within the viewmodel by using a custom utility class (ThemeManager) :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Telerik.Windows.Controls;
 
namespace XXX.Infrastructure
{
    public enum Theme
    {
        Expression_Dark,
        Green,
        Office2013,
        Office_Black,
        Office_Blue,
        Office__Silver,
        VisualStudio2013,
        Windows7,
        Windows8,
        Windows8Touch,       
         
    }
 
    public enum ColorVariation
    {       
        GreenDark,
        GreenLight,
        Office2013DarkGrey,
        Office2013LightGrey,
        Office2013White,
        VisualStudio2013Blue,
        VisualStudio2013Dark,
        VisualStudio2013Light
    }
 
    public static class ThemeManager
    {
        public static void ChangeTheme(Theme theme)
        {
            ClearResourceDictionary();
 
            switch (theme)
            {
                case Theme.Expression_Dark:
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Expression_Dark;component/Themes/System.Windows.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Expression_Dark;component/Themes/Telerik.Windows.Controls.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Expression_Dark;component/Themes/Telerik.Windows.Controls.Input.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Expression_Dark;component/Themes/Telerik.Windows.Controls.Navigation.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    break;
                case Theme.Office_Black:
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office_Black;component/Themes/System.Windows.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office_Black;component/Themes/Telerik.Windows.Controls.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office_Black;component/Themes/Telerik.Windows.Controls.Input.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office_Black;component/Themes/Telerik.Windows.Controls.Navigation.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    break;
                case Theme.Office_Blue:
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office_Blue;component/Themes/System.Windows.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office_Blue;component/Themes/Telerik.Windows.Controls.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office_Blue;component/Themes/Telerik.Windows.Controls.Input.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office_Blue;component/Themes/Telerik.Windows.Controls.Navigation.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    break;
                case Theme.Office__Silver:
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office__Silver;component/Themes/System.Windows.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office__Silver;component/Themes/Telerik.Windows.Controls.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office__Silver;component/Themes/Telerik.Windows.Controls.Input.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office__Silver;component/Themes/Telerik.Windows.Controls.Navigation.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    break;
                case Theme.Windows8:
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Windows8;component/Themes/System.Windows.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() {  Source = new Uri(uriString: "/Telerik.Windows.Themes.Windows8;component/Themes/Telerik.Windows.Controls.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Windows8;component/Themes/Telerik.Windows.Controls.Input.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Windows8;component/Themes/Telerik.Windows.Controls.Navigation.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    break;
                case Theme.Windows8Touch:
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Windows8Touch;component/Themes/System.Windows.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Windows8Touch;component/Themes/Telerik.Windows.Controls.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Windows8Touch;component/Themes/Telerik.Windows.Controls.Input.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Windows8Touch;component/Themes/Telerik.Windows.Controls.Navigation.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    break;
                case Theme.Windows7:
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Windows7;component/Themes/System.Windows.xaml", uriKind: UriKind.RelativeOrAbsolute)  });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Windows7;component/Themes/Telerik.Windows.Controls.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Windows7;component/Themes/Telerik.Windows.Controls.Input.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Windows7;component/Themes/Telerik.Windows.Controls.Navigation.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    break;
                case Theme.Office2013:
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office2013;component/Themes/System.Windows.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office2013;component/Themes/Telerik.Windows.Controls.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office2013;component/Themes/Telerik.Windows.Controls.Input.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office2013;component/Themes/Telerik.Windows.Controls.Navigation.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    break;
                case Theme.VisualStudio2013:
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.VisualStudio2013;component/Themes/System.Windows.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.VisualStudio2013;component/Themes/Telerik.Windows.Controls.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.VisualStudio2013;component/Themes/Telerik.Windows.Controls.Input.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.VisualStudio2013;component/Themes/Telerik.Windows.Controls.Navigation.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    break;
                case Theme.Green:
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Green;component/Themes/System.Windows.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Green;component/Themes/Telerik.Windows.Controls.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Green;component/Themes/Telerik.Windows.Controls.Input.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Green;component/Themes/Telerik.Windows.Controls.Navigation.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    GreenPalette.LoadPreset(GreenPalette.ColorVariation.Dark);
 
                    break;
                default:
                    break;
            }
        }
 
        public static void ChangeThemeVariation(ColorVariation colorVariation)
        {
            switch (colorVariation)
            {
                case ColorVariation.GreenDark:
                    GreenPalette.LoadPreset(GreenPalette.ColorVariation.Dark);
                    break;
                case ColorVariation.GreenLight:
                    GreenPalette.LoadPreset(GreenPalette.ColorVariation.Light);
                    break;
                case ColorVariation.Office2013DarkGrey:
                    Office2013Palette.LoadPreset(Office2013Palette.ColorVariation.DarkGray);
                    break;
                case ColorVariation.Office2013LightGrey:
                    Office2013Palette.LoadPreset(Office2013Palette.ColorVariation.LightGray);
                    break;
                case ColorVariation.Office2013White:
                    Office2013Palette.LoadPreset(Office2013Palette.ColorVariation.White);
                    break;
                case ColorVariation.VisualStudio2013Blue:
                    VisualStudio2013Palette.LoadPreset(VisualStudio2013Palette.ColorVariation.Blue);
                    break;
                case ColorVariation.VisualStudio2013Dark:
                    VisualStudio2013Palette.LoadPreset(VisualStudio2013Palette.ColorVariation.Dark);
                    break;
                case ColorVariation.VisualStudio2013Light:
                    VisualStudio2013Palette.LoadPreset(VisualStudio2013Palette.ColorVariation.Light);
                    break;
                default:
                    break;
            }
        }
 
        public static void ClearResourceDictionary()
        {
            App.Current.Resources.MergedDictionaries.Clear();
 
        }
    }
}

... which gets called from the following view model

using Notizen.Infrastructure;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace XXXX.ViewModels
{
    [Export(typeof(ThemeManagerViewModel))]
    public class ThemeManagerViewModel
    {
        public void ChangeThemeToGreenTheme()
        {
            ThemeManager.ChangeThemeVariation(ColorVariation.GreenDark);
            ThemeManager.ChangeTheme(Theme.Green);
             
        }
 
        public void ChangeThemeToVisualStudio2013Theme()
        {
            ThemeManager.ChangeThemeVariation(ColorVariation.VisualStudio2013Dark);
            ThemeManager.ChangeTheme(Theme.VisualStudio2013);
             
        }
 
        public void ChangeThemeToExpressionDarkTheme()
        {
            ThemeManager.ChangeTheme(Theme.Expression_Dark);
        }
 
        public void ChangeThemeToOfficeBlackTheme()
        {
            ThemeManager.ChangeTheme(Theme.Office_Black);
        }
    }
}

My XAML view looks like this:

<UserControl x:Class="Notizen.Views.TagsView"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:Notizen.Views"
             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
             xmlns:vm="clr-namespace:Notizen.ViewModels"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
 
        <Style x:Key="radTreeViewItemStyle" TargetType="{x:Type telerik:RadTreeViewItem}" BasedOn="{StaticResource RadTreeViewItemStyle}">
                <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                <Setter Property="FontWeight" Value="Normal" />
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="FontWeight" Value="Bold" />
                    </Trigger>
                </Style.Triggers>
            </Style>
 
        <!--<Style TargetType="{x:Type telerik:RadTreeView}" BasedOn="{StaticResource RadTreeViewStyle}">
            --><!--<Setter Property="Background" Value="Black" />--><!--
        </Style>-->
 
        <HierarchicalDataTemplate x:Key="hierarchicalDataTemplate"
                          DataType="{x:Type vm:TagViewModel}"
                          ItemsSource="{Binding Tags}">
            <DockPanel LastChildFill="True">
                <StackPanel DockPanel.Dock="Right" Orientation="Horizontal" HorizontalAlignment="Right">
                    <TextBlock Text="{Binding Count}" Padding="0,0,5,0" Foreground="Gray" />
                </StackPanel>
                <StackPanel DockPanel.Dock="Left" Orientation="Horizontal">
                    <!--<Image Width="16" Height="16" Margin="3,0" Source="Images\RegionXX.png" />-->
                    <TextBlock Text="{Binding Name}"  /> <!--Foreground="#FF1E4BAC"-->
                </StackPanel>
 
            </DockPanel>
        </HierarchicalDataTemplate>
    </UserControl.Resources>
    <DockPanel LastChildFill="True">
        <!--<TextBlock DockPanel.Dock="Top" Background="CornflowerBlue" x:Name="Message" />-->
        <Button x:Name="AddTag" Content="Add Tag" DockPanel.Dock="Top" />
        <telerik:RadTreeView x:Name="Tags" DockPanel.Dock="Bottom"
                             ItemsSource="{Binding Path=Tags}"
                             ItemTemplate="{StaticResource hierarchicalDataTemplate}"
                             ItemContainerStyle="{StaticResource radTreeViewItemStyle}"
                             SelectionMode="Single"
                             SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
                             >
        </telerik:RadTreeView>
    </DockPanel>
</UserControl>

If I don't use custom styles (which I actually do need!) theme switching works like a charm. But as soon as I am using anything like 

<Style x:Key="radTreeViewItemStyle" TargetType="{x:Type telerik:RadTreeViewItem}" BasedOn="{StaticResource RadTreeViewItemStyle}">

the theme gets not applied !

1) What is wrong?

2) dark themes (expression_dark, office_black) are working correctly , but themes with dark color variations (green theme, visualstudio2013 theme) don't work properly for complex controls but for simple controls like textboxes and buttons. The dark background is not set and the theme is not applied when using custom styles. Any solution for this. Any test project. Note I am using mvvm and mostly don't have access to the underlying view. 

 

Thanks again for your help.

 

XXXXXXXXXXXXXXXXXXXXXXXXXXx

0
Martin
Telerik team
answered on 07 Jun 2016, 12:33 PM
Hello,

You can move the custom Style for the RadTreeViewItem to a ResourceDictionary somewhere in your application and merge it again to your Application.Resources on every theme change in the same manner you handle the merging of the theme resources.

And about the dark variations - could I ask you to specify the controls that the themes are not applied to? I would guess that the backgrounds of some of the controls are simply transparent causing the appearance that the theme is not applied properly in a dark variation. You can use a workaround about not being able to access the underlying view by styling the background of the control or by styling the one of a parent element.

Regards,
Martin
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Oliver
Top achievements
Rank 1
answered on 08 Jun 2016, 11:31 PM

It's the treeviewitem style that does not get applied correctly (please see picture):

<Application x:Class="Notizen.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Notizen">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                 
                <!--<ResourceDictionary Source="/Telerik.Windows.Themes.Green;component/Themes/System.Windows.xaml" />
                <ResourceDictionary Source="/Telerik.Windows.Themes.Green;component/Themes/Telerik.Windows.Controls.xaml" />
                <ResourceDictionary Source="/Telerik.Windows.Themes.Green;component/Themes/Telerik.Windows.Controls.Navigation.xaml" />-->
                <ResourceDictionary Source="/Telerik.Windows.Themes.Expression_Dark;component/Themes/System.Windows.xaml" />
                <ResourceDictionary Source="/Telerik.Windows.Themes.Expression_Dark;component/Themes/Telerik.Windows.Controls.xaml" />
                <ResourceDictionary Source="/Telerik.Windows.Themes.Expression_Dark;component/Themes/Telerik.Windows.Controls.Navigation.xaml" />
                <ResourceDictionary Source="/Telerik.Windows.Themes.Expression_Dark;component/Themes/Telerik.Windows.Controls.Docking.xaml" />
 
                <ResourceDictionary Source="pack://application:,,,/Assets/styles.xaml" />
                 
                <ResourceDictionary>
                    <local:AppBootstrapper x:Key="bootstrapper" />
 
                    <Style x:Key="ContainerBackgroundStyle" TargetType="{x:Type Border}">
                        <Setter Property="Background" Value="#FFFFFFFF" />
                    </Style>
                    <SolidColorBrush x:Key="ContainerBackgroundBrush" Color="#FFFFFFFF" />
                    <!--<Style x:Name="WindowStyle" TargetType="{x:Type Window}">
                        <Setter Property="Background" Value="Black" />
                    </Style>-->
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
         
    </Application.Resources>
</Application>

                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                     
                    xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation"
                    >
 
    <Style x:Key="radTreeViewItemStyle" TargetType="{x:Type telerik:RadTreeViewItem}" BasedOn="{StaticResource RadTreeViewItemStyle}">
        <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        <!--<Setter Property="HorizontalAlignment" Value="Stretch" />-->
        <Setter Property="FontWeight" Value="Normal" />
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="FontWeight" Value="Bold" />
            </Trigger>
        </Style.Triggers>
    </Style>
</ResourceDictionary>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using Telerik.Windows.Controls;
 
namespace Notizen.Infrastructure
{
    public enum Theme
    {
        Expression_Dark,
        Green,
        Office2013,
        Office_Black,
        Office_Blue,
        Office__Silver,
        VisualStudio2013,
        Windows7,
        Windows8,
        Windows8Touch,       
         
    }
 
    public enum ColorVariation
    {       
        GreenDark,
        GreenLight,
        Office2013DarkGrey,
        Office2013LightGrey,
        Office2013White,
        VisualStudio2013Blue,
        VisualStudio2013Dark,
        VisualStudio2013Light
    }
 
    public static class ThemeManager
    {
        public static void ChangeTheme(Theme theme)
        {
            ClearResourceDictionary();
 
            switch (theme)
            {
                case Theme.Expression_Dark:
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Expression_Dark;component/Themes/System.Windows.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Expression_Dark;component/Themes/Telerik.Windows.Controls.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Expression_Dark;component/Themes/Telerik.Windows.Controls.Input.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Expression_Dark;component/Themes/Telerik.Windows.Controls.Navigation.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Expression_Dark;component/Themes/Telerik.Windows.Controls.Docking.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    //ChangeBackgroundStyle(new SolidColorBrush() { Color = Color.FromArgb(255, 28, 28, 28) });
                    ChangeBackGroundColor(Color.FromArgb(255, 28, 28, 28));
                    break;
                case Theme.Office_Black:
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office_Black;component/Themes/System.Windows.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office_Black;component/Themes/Telerik.Windows.Controls.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office_Black;component/Themes/Telerik.Windows.Controls.Input.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office_Black;component/Themes/Telerik.Windows.Controls.Navigation.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office_Black;component/Themes/Telerik.Windows.Controls.Docking.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    ChangeBackGroundColor(Colors.White);
                    break;
                case Theme.Office_Blue:
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office_Blue;component/Themes/System.Windows.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office_Blue;component/Themes/Telerik.Windows.Controls.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office_Blue;component/Themes/Telerik.Windows.Controls.Input.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office_Blue;component/Themes/Telerik.Windows.Controls.Navigation.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office_Blue;component/Themes/Telerik.Windows.Controls.Docking.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    break;
                case Theme.Office__Silver:
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office__Silver;component/Themes/System.Windows.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office__Silver;component/Themes/Telerik.Windows.Controls.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office__Silver;component/Themes/Telerik.Windows.Controls.Input.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office__Silver;component/Themes/Telerik.Windows.Controls.Navigation.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office__Silver;component/Themes/Telerik.Windows.Controls.Docking.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    break;
                case Theme.Windows8:
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Windows8;component/Themes/System.Windows.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() {  Source = new Uri(uriString: "/Telerik.Windows.Themes.Windows8;component/Themes/Telerik.Windows.Controls.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Windows8;component/Themes/Telerik.Windows.Controls.Input.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Windows8;component/Themes/Telerik.Windows.Controls.Navigation.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Windows8;component/Themes/Telerik.Windows.Controls.Docking.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    break;
                case Theme.Windows8Touch:
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Windows8Touch;component/Themes/System.Windows.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Windows8Touch;component/Themes/Telerik.Windows.Controls.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Windows8Touch;component/Themes/Telerik.Windows.Controls.Input.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Windows8Touch;component/Themes/Telerik.Windows.Controls.Navigation.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Windows8Touch;component/Themes/Telerik.Windows.Controls.Docking.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    break;
                case Theme.Windows7:
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Windows7;component/Themes/System.Windows.xaml", uriKind: UriKind.RelativeOrAbsolute)  });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Windows7;component/Themes/Telerik.Windows.Controls.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Windows7;component/Themes/Telerik.Windows.Controls.Input.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Windows7;component/Themes/Telerik.Windows.Controls.Navigation.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Windows7;component/Themes/Telerik.Windows.Controls.Docking.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    break;
                case Theme.Office2013:
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office2013;component/Themes/System.Windows.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office2013;component/Themes/Telerik.Windows.Controls.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office2013;component/Themes/Telerik.Windows.Controls.Input.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office2013;component/Themes/Telerik.Windows.Controls.Navigation.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Office2013;component/Themes/Telerik.Windows.Controls.Docking.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    break;
                case Theme.VisualStudio2013:
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.VisualStudio2013;component/Themes/System.Windows.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.VisualStudio2013;component/Themes/Telerik.Windows.Controls.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.VisualStudio2013;component/Themes/Telerik.Windows.Controls.Input.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.VisualStudio2013;component/Themes/Telerik.Windows.Controls.Navigation.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.VisualStudio2013;component/Themes/Telerik.Windows.Controls.Docking.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    break;
                case Theme.Green:
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Green;component/Themes/System.Windows.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Green;component/Themes/Telerik.Windows.Controls.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Green;component/Themes/Telerik.Windows.Controls.Input.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Green;component/Themes/Telerik.Windows.Controls.Navigation.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "/Telerik.Windows.Themes.Green;component/Themes/Telerik.Windows.Controls.Docking.xaml", uriKind: UriKind.RelativeOrAbsolute) });
                    //ChangeThemeVariation(ColorVariation.GreenDark);
                    break;
                default:
                    break;
            }
 
            App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri(uriString: "pack://application:,,,/Assets/styles.xaml", uriKind: UriKind.RelativeOrAbsolute) });
        }
 
        public static void ChangeThemeVariation(ColorVariation colorVariation)
        {
            switch (colorVariation)
            {
                case ColorVariation.GreenDark:
                    GreenPalette.LoadPreset(GreenPalette.ColorVariation.Dark);
                    ChangeBackGroundColor(Color.FromArgb(255, 45, 45, 48) );
                    break;
                case ColorVariation.GreenLight:
                    GreenPalette.LoadPreset(GreenPalette.ColorVariation.Light);
                    ChangeBackGroundColor(Colors.White);
                    break;
                case ColorVariation.Office2013DarkGrey:
                    Office2013Palette.LoadPreset(Office2013Palette.ColorVariation.DarkGray);
                    ChangeBackGroundColor(Colors.White);
                    break;
                case ColorVariation.Office2013LightGrey:
                    Office2013Palette.LoadPreset(Office2013Palette.ColorVariation.LightGray);
                    ChangeBackGroundColor(Colors.White);
                    break;
                case ColorVariation.Office2013White:
                    Office2013Palette.LoadPreset(Office2013Palette.ColorVariation.White);
                    ChangeBackGroundColor(Colors.White);
                    break;
                case ColorVariation.VisualStudio2013Blue:
                    VisualStudio2013Palette.LoadPreset(VisualStudio2013Palette.ColorVariation.Blue);
                    ChangeBackGroundColor(Colors.White);
                    break;
                case ColorVariation.VisualStudio2013Dark:
                    VisualStudio2013Palette.LoadPreset(VisualStudio2013Palette.ColorVariation.Dark);
                    ChangeBackGroundColor(Color.FromArgb(255, 45, 45, 48) );
                    break;
                case ColorVariation.VisualStudio2013Light:
                    VisualStudio2013Palette.LoadPreset(VisualStudio2013Palette.ColorVariation.Light);
                    ChangeBackGroundColor(Colors.White);
                    break;
                default:
                    break;
            }
        }
 
        public static void ClearResourceDictionary()
        {
            App.Current.Resources.MergedDictionaries.Clear();
 
        }
 
        private static void ChangeBackGroundColor(Color color)
        {
            Application.Current.Resources["ContainerBackgroundBrush"] = new SolidColorBrush() { Color = color };
            //((SolidColorBrush) Application.Current.Resources["ContainerBackgroundBrush"]).Color = color;
        }
 
        private static void ChangeBackgroundStyle(SolidColorBrush color)
        {
             
            Style style = new Style
            {
                TargetType = typeof(System.Windows.Window)
            };
 
            style.Setters.Add(new Setter(Label.BackgroundProperty, color)); // Brushes.Aquamarine
 
            Application.Current.Resources["ContainerBackgroundStyle"] = null;
            Application.Current.Resources["ContainerBackgroundStyle"] = style;
        }
    }
}

<UserControl x:Class="Notizen.Views.TagsView"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:Notizen.Views"
             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
             xmlns:vm="clr-namespace:Notizen.ViewModels"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
 
        <!--<Style x:Key="radTreeViewItemStyle" TargetType="{x:Type telerik:RadTreeViewItem}" BasedOn="{StaticResource RadTreeViewItemStyle}">
                <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                <Setter Property="FontWeight" Value="Normal" />
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="FontWeight" Value="Bold" />
                    </Trigger>
                </Style.Triggers>
            </Style>-->
 
        <!--<Style TargetType="{x:Type telerik:RadTreeView}" BasedOn="{StaticResource RadTreeViewStyle}">
            --><!--<Setter Property="Background" Value="Black" />--><!--
        </Style>-->
 
        <HierarchicalDataTemplate x:Key="hierarchicalDataTemplate"
                          DataType="{x:Type vm:TagViewModel}"
                          ItemsSource="{Binding Tags}">
            <!--<DockPanel LastChildFill="True" HorizontalAlignment="Stretch" >
                <StackPanel DockPanel.Dock="Right" Orientation="Horizontal" HorizontalAlignment="Right"  Width="10">
                    <TextBlock Text="{Binding Count}" Padding="0,0,5,0" Foreground="Gray" />
                </StackPanel>
                <StackPanel DockPanel.Dock="Left" Orientation="Horizontal">
                    --><!--<Image Width="16" Height="16" Margin="3,0" Source="Images\RegionXX.png" />--><!--
                    <TextBlock Text="{Binding Name}"  />
                    --><!--Foreground="#FF1E4BAC"--><!--
                </StackPanel>
            </DockPanel>-->
            <Grid HorizontalAlignment="Stretch">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1*" />
                    <ColumnDefinition Width="20"  />
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding Count}" Grid.Column="1" Padding="0,0,5,0" Foreground="Gray" />
                <TextBlock Text="{Binding Name}" Grid.Column="0"   />
            </Grid>
        </HierarchicalDataTemplate>
    </UserControl.Resources>
    <DockPanel LastChildFill="True">
        <!--<TextBlock DockPanel.Dock="Top" Background="CornflowerBlue" x:Name="Message" />-->
        <Button x:Name="AddTag" Content="Add Tag" DockPanel.Dock="Top" />
        <telerik:RadTreeView x:Name="Tags" DockPanel.Dock="Bottom"
                             ItemsSource="{Binding Path=Tags}"
                             ItemTemplate="{StaticResource hierarchicalDataTemplate}"
                             ItemContainerStyle="{StaticResource radTreeViewItemStyle}"
                             SelectionMode="Single"
                             SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
                             >
        </telerik:RadTreeView>
    </DockPanel>
</UserControl>

When I don't use a custom style for binding, everything works fine.

0
Evgenia
Telerik team
answered on 10 Jun 2016, 09:29 AM
Hi Oliver,

I reviewed the code you sent and everything seems to be set-up correctly. Just one thing to clarify here -- if you change the theme at runtime the RadTreeViewItemStyle changes accordingly to the theme being chosen, right? -- i.e. it gets its new style correctly applied thanks to the inheritance from our theme style (BasedOn="{StaticResource RadTreeViewItemStyle}")? So the onliest thing that doesn't work is the FontWeight property as it never gets Bold even though you set it in Style Trigger. Since it's impossible for me to inspect/debug your code from your code snippets can you please clarify that setting anything other than FontWeight is working in this Style Trigger. For example you might try to change the Foreground property and see whether it applies. If this still doesn't work I'm assuming that the Binding to IsSelecte property is causing you the problem. You might easily check if your binding is correct (there are no binding errors) if you inspect your application with a WPF Inspection tool as Snoop for example. It will let you traverse the visual tree to the RadTreeViewItem element and see what happens on IsSelected property.

Let me know how this goes on your side.

Regards,
Evgenia
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Oliver
Top achievements
Rank 1
answered on 11 Jun 2016, 10:32 AM

H, indeed everything was set-up correctly except for the following thing:

After changing radtreeview itemcontainerstyle from 

ItemContainerStyle="{StaticResource radTreeViewItemStyle}"
to
ItemContainerStyle="{DynamicResource radTreeViewItemStyle}"
everything works as expected.

Thanks for your help nevertheless.

Btw, I would recommend to add an example to your sdk examples how to do theming and theme switching from within an mvvm scenario. This kind of basic stuff took far to much time to realize how to do it properly. I wished I would have had an easy example demonstrating this basic task.

Regards,

Oliver

0
Evgenia
Telerik team
answered on 13 Jun 2016, 09:50 AM
Hello Oliver,

I'm glad you managed to resolve this issue on your own. Indeed setting the Style with DynamicResource instead of StaticResource will look for the resource runtime everytime the resource is required.
Onto your sdk example requirement -- I'll notify the responsible team and they'll further decide whether and when we'll create such example.

Let me know if there is anything else I can assist you with.

Regards,
Evgenia
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Tags
General Discussions
Asked by
Oliver
Top achievements
Rank 1
Answers by
Martin
Telerik team
Oliver
Top achievements
Rank 1
Evgenia
Telerik team
Share this question
or