Search code examples
wpfxamlresourcedictionary

ResourceDictionary fails to merge external ResourceDictionaries


I Have a ResourceDictionary Generic.xaml which consists of a set of other resource dictionaries, this is achieved by merging dicionaries. If no resources other than merged dictionaries are defined in my Generic.xaml, during the runtime no styles gets applied. However if I define any styles in the Gerneric.xaml all starts to work properly.

So the example below doesn't work (this is from my Generic.xaml)

<ResourceDictionary.MergedDictionaries>
   <ResourceDictionary Source="pack://application:,,,/Resources;component/ResourceDictionaries/CommonControls/CommonControlsResources.xaml"/>
</ResourceDictionary.MergedDictionaries>

<!--<Style TargetType="{x:Type ToolTip}"/>-->

And here is the working version:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/Resources;component/ResourceDictionaries/CommonControls/CommonControlsResources.xaml"/>
</ResourceDictionary.MergedDictionaries>

<Style TargetType="{x:Type ToolTip}"/>

Why is that happening?

Edit:

To pin point the problem I have created a sample solution. So the structure of the project is following:

ProjectA WPF Windows Application

ProjectC Class Library, containing styles which are share among all project in the solution folder.

Here is the ProjectA:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/ProjectC;component/Themes/ThemeZ.xaml"/>
    </ResourceDictionary.MergedDictionaries>

    <!--<Style TargetType="{x:Type ToolTip}"/>-->
</ResourceDictionary>


    <Application x:Class="MergedResourceDictionaryIssue.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="MainWindow.xaml">
        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="/Themes/Generic.xaml"/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>
    </Application>


    <Window x:Class="MergedResourceDictionaryIssue.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Button/>
        </Grid>
    </Window>

Here is the ProjectC:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <!-- Styles for all our buttons, let make them all red squares -->
    <Style TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Grid Width="50"
                          Height="50"
                          Background="Red" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>

And again if I uncomment the code in the ResourceDictionary project renders as expected, but otherwise the resource dictionary is simply ignored.


Solution

  • According to me, a Dictionary can only contain : - merged dictionaries - OR styles

    Edit : They are discussing about it here It's a "known" issue and the trick that you used is the currently workaround.