Search code examples
c#wpfxamlresourcedictionary

What kind of "Key" do I need to supply when loading Resource Dictionaries?


In trying to set a default ResourceDictionary I receive the following warning:

The designer does not support loading dictionaries that mix 'ResourceDictionary' items without a key and other items in the same collection. Please ensure that the 'Resources' property does not contain 'ResourceDictionary' items without a key, or that the 'ResourceDictionary' item is the only element in the collection.

This is the code that I am using in my App.xaml file, that received the above warning:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Lang.en-US.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

This is the exact same code that I've used to set a ResourceDictionary in Visual Studio 2008. I am now using VS 2010. What Key do I need to provide this ResourceDictionary for it to work correctly?

This is the line in my MainWindow.xaml that I am currently testing along with this code:

<MenuItem Header="{DynamicResource new_test}" />

Solution

  • Since you haven't posted your complete XAML file, i suspect there are other resources apart from merged dictionary in your resources section.

    As per MSDN -

    It is legal to define resources within a ResourceDictionary that is specified as a merged dictionary, either as an alternative to specifying Source, or in addition to whatever resources are included from the specified source. However, this is not a common scenario; the main scenario for merged dictionaries is to merge resources from external file locations. If you want to specify resources within the markup for a page, you should typically define these in the main ResourceDictionary and not in the merged dictionaries.

    Try moving other resources in separate resource dictionary and make sure all other resources have x:Key set on them -

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Lang.en-US.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <ResourceDictionary>
                 <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
                 <ContextMenu x:Key="MyContextMenu"/>
            </ResourceDictionary>
        </ResourceDictionary>
    </Application.Resources>