wpfthemesapp.xaml

Have a App.xaml ResourceDictionary.MergedDictionaries Theme affect only certain controls WPF


In my App.xaml I have the following code:

<Application.Resources>

    <ResourceDictionary>

        <ResourceDictionary.MergedDictionaries>

            <!-- primary color -->
            <ResourceDictionary>
                <!-- include your primary palette -->
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="Styles/Scroll.xaml" />
                    <ResourceDictionary Source="Styles/ComboBox.xaml" />
                    <ResourceDictionary Source="Styles/ComboBoxCanType.xaml" />
                    <ResourceDictionary Source="Styles/ComboBoxCanType1.xaml" />
                    <ResourceDictionary Source="Styles/ComboBox1.xaml" />
                    <ResourceDictionary Source="Styles/TextBox.xaml" />
                    <ResourceDictionary Source="Styles/TaskContextMenu.xaml" />
                    <ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/SkinDefault.xaml"/>
                    <ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/Theme.xaml"/>
                    <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/MaterialDesignColor.red.xaml" />
                    <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Dark.xaml" />
                    <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
                </ResourceDictionary.MergedDictionaries>

                <SolidColorBrush x:Key="PrimaryHueLightBrush" Color="#E42F34"/>
                <SolidColorBrush x:Key="PrimaryHueLightForegroundBrush" Color="#FFFFFF"/>
                <!--<LinearGradientBrush x:Key="PrimaryHueMidBrush" EndPoint="1.5,1.5" StartPoint="0.5,0">
                    <GradientStop Color="#8E2E2F" Offset="0"/>
                    <GradientStop Color="#E43D47" Offset="1"/>
                </LinearGradientBrush>-->
                <!--#F14450-->
                <SolidColorBrush x:Key="PrimaryHueMidBrush" Color="#F14450"/>
                <SolidColorBrush x:Key="PrimaryHueMidForegroundBrush" Color="#FFFFFF"/>
                <SolidColorBrush x:Key="PrimaryHueDarkBrush" Color="#4D1DCF"/>
                <SolidColorBrush x:Key="PrimaryHueDarkForegroundBrush" Color="#FFFFFF"/>

            </ResourceDictionary>

        </ResourceDictionary.MergedDictionaries>

    </ResourceDictionary>
</Application.Resources>

I want to have a specific control from the HandyControl UI (specifically the WaveProgressBar) and therefore I need these lines:

<ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/SkinDefault.xaml"/>
                <ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/Theme.xaml"/>

For it to work and load the control. However, if I do do this, it messes with my MaterialDesign controls and other default controls I have, so how can I set the theme for ONLY the WaveProgressBar? Any help would be appreciated!


Solution

  • Resources have scope. You can merge those just for your control:

    <WaveProgressBar ......>
        <WaveProgressBar.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/SkinDefault.xaml"/>
                     <ResourceDictionary Source="pack://application:,,,/HandyControl;component/Themes/Theme.xaml"/>
    

    Or as resources of a grid containing several of these controls. Or as resources of a usercontrol containing a number of these controls. And so on.

    Having said that.

    Styles and templates can be applied targetting a specific control and you seem to have a unique control there. So you could grab the bits you need and put them in a style which sets template, property values etc just to WaveProgressBar. This would also likely be more efficient.