Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does styles don't work at runtime in wpf, when using multiple MergedDictionaries?

Tags:

wpf

xaml

If i use MergedDictionaries multiple times to define styles, it doesn't work at runtime, but in WPF Designer of VS2010 it works. It also work if load MergedDictionaries using code at runtime.

Why this is happening ? Is it my problem only or ? And how to solve it ?

I am using WPF4 and loading themes/styles from an assembly at application level.

Not working

<!--Application.xaml-->
 <Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/Lib;component/Themes/Theme.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>


<!--Theme.xaml-->
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Theme/Shared.xaml" />
    <ResourceDictionary Source="Theme/Button.xaml" />
</ResourceDictionary.MergedDictionaries>

Working

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/Lib;component/Themes/Theme/Shared.xaml" />
                <ResourceDictionary Source="pack://application:,,,/Lib;component/Themes/Theme/Button.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
like image 522
Code0987 Avatar asked Jan 21 '23 10:01

Code0987


1 Answers

See the answer to this question. I have a feeling you're having the same problem

This is an optimization bug, see this link

On the creation of every object in XAML, if a default style is present (i.e. style w/ a key of Type) that style should be applied. As you can imagine there are several performance optimizations to make that (implied) lookup a light weight as possible. One of them is that we don’t look inside Resource Dictionaries unless they are flagged as “containing default Styles”. There is a bug: if all your default styles are nested in merged dictionaries three levels deep (or deeper) the top dictionary does not get flagged so the search skips it. The work around is to put a default Style to something, anything, in the root Dictionary.

So adding a dummy style to the root dictionary fixes this. Example

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/Lib;component/Themes/Theme.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <!-- Dummy Style, anything you won't use goes -->
        <Style TargetType="{x:Type Rectangle}" />
    </ResourceDictionary>
</Application.Resources>
like image 92
Fredrik Hedblad Avatar answered Jan 30 '23 13:01

Fredrik Hedblad