...">

WPF style is not applied at runtime (but works in designer)

I have this code in a WPF window:

<Window.Resources>
    <Style x:Key="MahappsStyle">
        <Style.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
                    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
                    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
                    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
                    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Style.Resources>
    </Style>
</Window.Resources>

      

The idea is to include external styles in the dictionary for individual elements in my application. For example, it should work by applying the "MahappsStyle" style to an element called "HamburgerMenu":

<mahapps:HamburgerMenu x:Name="hamburgerMenu" Style="{StaticResource MahappsStyle}"
                    DisplayMode="CompactOverlay">
</mahapps:HamburgerMenu>

      

But this approach only seems to work in the designer, not at runtime. What am I missing? Is there any other way to set MergedDictionaries to a single item?

UPDATE. ... Found a way to do it. First you need to create a Mahapps.xaml application with the following content:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:mahapps="http://metro.mahapps.com/winfx/xaml/controls"
                    xmlns:local="clr-namespace:Promt.Desktop">

    <ResourceDictionary.MergedDictionaries >
        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml"/>
        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

      

And then it can be applied to one element:

<mahapps:HamburgerMenu>
            <mahapps:HamburgerMenu.Resources>
                <ResourceDictionary Source="pack://application:,,,/Promt.Desktop;component/Styles/Mahapps.xaml"/>
            </mahapps:HamburgerMenu.Resources>
</mahapps:HamburgerMenu>

      

I am very disappointed that the ResourceDictionary cannot contain an x: key property. If someone knows a different approach - write it.

UPDATE2. An even better solution from Evk (based on Light's answer).

+3


source to share


2 answers


Laith's answer is close but not completely, you need to do it like this:

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary x:Key="MahappsResources">
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </ResourceDictionary>
</Window.Resources>

      

And then you can actually reference the key:



<mahapps:HamburgerMenu Resources="{StaticResource MahappsResources}" />

      

You need to add one more definition ResourceDictionary

because otherwise it treats yours MahappsResources

like Window.Resources

(so analog Window.Resources = new ResourceDictionary() ...

) and setting a key on it really doesn't make sense. When you add another one ResourceDictionary

- now you are actually adding the dictionary MahappsResources

to Window.Resources

with the given key and therefore you can refer to it using this key.

+1


source


You can check if this works:

<Window.Resources>
    <ResourceDictionary x:Key="MahappsResources">
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

      



and your controls can refer to it using a key:

<mahapps:HamburgerMenu Resources="{StaticResource MahappsResources}" />

      

+1


source







All Articles