Search code examples
wpfxamlstylesresourcedictionary

XAML - MergedDictionaries throwing XmlParseException "item has already been added". Why?


I have the following, very easy to reproduce problem: I'm creating a xaml application which uses resources from another file. The way to go is to create a MergedDictionaries-tag to merge the local and global resources, like this:

<Window>
<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="path.to.xaml.file"/>
            <ResourceDictionary>
                <Style TargetType="{x:Type Border}" x:Key="TypeBlock">

                </Style>
                <Style TargetType="{x:Type Border}" x:Key="SetBlock">

                </Style>
            </ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>
....
</Window>

This little piece of code will crash if you run it:

Item has already been added. Key in dictionary: 'System.Windows.Controls.Border'  Key being added: 'System.Windows.Controls.Border'

If we remove the MergedDictionaries-tag, the code will run as expected:

<Window>
<Window.Resources>
    <Style TargetType="{x:Type Border}" x:Key="TypeBlock">

    </Style>
    <Style TargetType="{x:Type Border}" x:Key="SetBlock">

    </Style>
</Window.Resources>
</Window>

I don't understand why it throws the exception when we use Merged Resources. Off course, the fix is easy enough for now (move the resources to a lower level). It would be nice to know if this is 'normal' behavior...


Solution

  • If your resources are not located in a separate file, then they shouldn't be part of the merged dictionaries. Move them outside like this:

    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="path.to.xaml.file"/>
            </ResourceDictionary.MergedDictionaries>
    
            <Style TargetType="{x:Type Border}" x:Key="TypeBlock">
    
            </Style>
            <Style TargetType="{x:Type Border}" x:Key="SetBlock">
    
            </Style>
        </ResourceDictionary>
    </Window.Resources>
    

    That said, the error message is a little misleading and may be a result of a bug in the XAML compiler.