MaterialDesignInXamlToolkit icon indicating copy to clipboard operation
MaterialDesignInXamlToolkit copied to clipboard

Change theme when no app.xaml

Open Pekshev opened this issue 8 years ago • 2 comments

How to change theme if a have't app.xaml? I'm create plugin for anothe application PaletteHelper throw exception "don't find resource dictionary"

Pekshev avatar Aug 08 '18 15:08 Pekshev

I just used this to create an add-in application which is hosted within another application (no app.xaml) so I had the same question. I resolved the issue by adding a resource dictionary item to my project ('MergedResources.xaml') and adding my resources to it like you would the App.xaml.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:DataPAL.Pro">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
        <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
        <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Grey.xaml" />
        <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.LightBlue.xaml" />
        <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.TextBox.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <SolidColorBrush x:Key="PrimaryHueLightBrush" Color="{StaticResource Primary100}"/>
    <SolidColorBrush x:Key="PrimaryHueLightForegroundBrush" Color="{StaticResource Primary100Foreground}"/>

</ResourceDictionary>

Then in order to get my .xaml files to use the resources I had to add the following to each .xaml. I used them within user controls so you would replace <UserControl.Resources> with whatever your control/window is such as <Window.Resources>.

    <UserControl.Resources>
        <ResourceDictionary Source="MergedResources.xaml"></ResourceDictionary>
    </UserControl.Resources>

Essentially since there is no App.xaml there isn't an easy way to create App level resources. So you have to tell each control what resource dictionary to use. There are other methods of doing this but I found this to be the easiest for me since I only had a few .xamls I had to add it to.

z3ke1r avatar Oct 17 '18 15:10 z3ke1r

Closing this for now since @z3ke1r answer should be the solution. If there are still issues please re-open.

Keboo avatar Dec 13 '18 17:12 Keboo