Search code examples
c#wpfxamlresourcedictionary

How is the order of ResourceDictionary keys determined?


I've got the following ResourceDictionary (shortened):

<ResourceDictionary>
  <Fluent:RibbonContextualTabGroup Header="Dokument" x:Key="dokumentRibbonGroup" x:Name="dokumentRibbonGroup" />
  <Fluent:RibbonTabItem x:Key="dokumentRibbonTab" Header="Start" />
  <Fluent:RibbonTabItem x:Key="ueberpruefenRibbonTab" Header="Überprüfen" />
  <Fluent:RibbonTabItem x:Key="austauschRibbonTab" Header="Austausch" />
</ResourceDictionary>

When I access the Keys property of the dictionary, the keys are in the following order:

dokumentRibbonTab
austauschRibbonTab
ueberpruefenRibbonTab
dokumentRibbonGroup

I can't make any sense of that order. It's not alphabetical, not revserse and in particular not the original order. Does anyone have a hint? Or even better, a suggestion on how to specify the order of items in a ResourceDictionary?


Solution

  • It may well be based on hash code somehow. (Not necessarily just "in ascending order of hash code" though.) In particular, the docs state:

    The ResourceDictionary class is not derived from DictionaryBase. Instead, the ResourceDictionary class implements IDictionary but relies on a Hashtable internally.

    Given that it uses a hash table, it's likely that the order depends on the hash.

    Typically dictionaries aren't ordered - or rather, the order is an implementation detail which shouldn't be relied on.

    Given that I can't see any documentation specifying the ordering, I think you should treat it as unordered. If you want to present the keys in a particular order, you should do that yourself.