New to Telerik Reporting? Download free 30-day trial

WPF Report Viewer Takes Themes from the XAML Files

Environment

Product Version 12.1.18.620+
Product Progress® Telerik® Reporting
Report Viewers WPF

Description

Sometimes it is more convenient to refer the Themes for the WPF Report Viewer from the corresponding XAML files as opposed to Telerik.ReportViewer.Wpf.Themes.dll assembly introduced with Telerik Reporting 12.1.18.620.

Solution

Add a folder (e.g. Themes) in the WPF Viewer project and copy the XAML files with the Themes there. The themes are distributed with Telerik Reporting and can be found in {Telerik Reporting installation folder}\Wpf\Themes, for example C:\Program Files (x86)\Progress\Telerik Reporting 2024 Q1\Wpf\Themes.

The themes can be referred directly in the App.xaml file, or alternatively in the code behind.

  1. In the App.xaml instead of content of the <ResourceDictionary.MergedDictionaries> element from step 4 in the WPF Report Viewer Manual Setup article use the following (the example uses the Material theme):

    <Application.Resources >
    //....
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Themes/Material/System.Windows.xaml" />
        <ResourceDictionary Source="Themes/Material/Telerik.Windows.Controls.xaml" />
        <ResourceDictionary Source="Themes/Material/Telerik.Windows.Controls.Input.xaml" />
        <ResourceDictionary Source="Themes/Material/Telerik.Windows.Controls.Navigation.xaml" />
        <ResourceDictionary Source="Themes/Material/Telerik.ReportViewer.Wpf.xaml" />
        </ResourceDictionary.MergedDictionaries>
    
        // YOU MAY ADD OTHER STYLES IN THE <ResourceDictionary> TAG :
        //....
    </ResourceDictionary>
    //....
    </Application.Resources>
    
  2. In the code behind use the following code that first clears the already merged dictionaries (if any), and then adds the new ones:

    public partial class ReportViewerWindow1 : Window
    {
    static readonly string[] dictionaries = new[]
            {
                "Themes/{0}/System.Windows.xaml",
                "Themes/{0}/Telerik.Windows.Controls.xaml",
                "Themes/{0}/Telerik.Windows.Controls.Input.xaml",
                "Themes/{0}/Telerik.Windows.Controls.Navigation.xaml",
                "Themes/{0}/Telerik.ReportViewer.Wpf.xaml"
            };
    
        public ReportViewerWindow1()
        {
            InitializeComponent();
            MergeResourceDictionaries("Material"); // Set the required theme name here
        }
    
        static void MergeResourceDictionaries(string theme)
        {
            var mergedDictionaries = Application.Current.Resources.MergedDictionaries;
            mergedDictionaries.Clear();
            foreach (var dictionary in dictionaries)
            {
                var uri = string.Format(dictionary, theme);
                mergedDictionaries.Add(new ResourceDictionary()
                {
                    Source = new Uri(uri, UriKind.RelativeOrAbsolute)
                });
            }
        }
    }
    
In this article