1

I'm having problems of using two different kinds of resources definitions in the resources section of a XAML file:

This throws an error:

<Window.Resources> <XmlDataProvider x:Key="Maschinen"> <x:XData xmlns=""> <machines> <machine name="alte Maschine"/> <machine name="neue Maschine"/> </machines> </x:XData> </XmlDataProvider> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="ShinyRed.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Window.Resources> 

and this also:

<Window.Resources> <XmlDataProvider x:Key="Maschinen"> <x:XData xmlns=""> <machines> <machine name="alte Maschine"/> <machine name="neue Maschine"/> </machines> </x:XData> </XmlDataProvider> <ResourceDictionary Source="ShinyRed.xaml"/> </Window.Resources> 

Does anyone know what I have to do to avoid error messages? Thank you!

3
  • Sorry. The error message is as follows: All objects added to an IDictionary must have a Key attribute or some other type of key associated with them. Commented Dec 8, 2011 at 13:15
  • the upper one should work, no? At least I have siomilar constructs in my xaml and never got problem. Commented Dec 8, 2011 at 13:21
  • both are running into the same error. None of them works. If I'm adding a key to the dictionary then it works: <ResourceDictionary x:Key="ResDict01" Source="ShinyRed.xaml"/> But then I don't know how to access the ResourceDictionary the styles in the ResourceDictionary do not work any longer. Commented Dec 8, 2011 at 16:20

1 Answer 1

1

Any object placed into a ResourceDictionary must be assigned a key. This key is used by WPF to retrieve the object, either implicitly (e.g. via default style) or explicitly (e.g. via StaticResource, DynamicResource).

In your example above, you have created a ResourceDictionary without a key, hence the error. The syntax you are looking for is:

<Window.Resources> <ResourceDictionary> <!-- Create a resource dictionary here --> <XmlDataProvider x:Key="Maschinen"> <!-- Put this into the dictionary --> <x:XData xmlns=""> <machines> <machine name="alte Maschine"/> <machine name="neue Maschine"/> </machines> </x:XData> </XmlDataProvider> <ResourceDictionary.MergedDictionaries> <!-- Merge into this dictionary another dictionary ... --> <ResourceDictionary Source="ShinyRed.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Window.Resources> 

Keep in mind the first ResourceDictionary tag in the snippet above is implicit if you leave it out. As a result, in your original examples, the tag <ResourceDictionary> on its own defines a new second dictionary (without a key) to be added to the outer dictionary.

Hope this helps!

Sign up to request clarification or add additional context in comments.

1 Comment

@manton, please mark as answer by checking the V on the top-left corner of the post (see example: i.imgur.com/imbXp.png)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.