13

I would like to refer to a MergedDictionary together with locally declared resources in my Windows.Resources. However, I'm getting this error:

"All objects added to an IDictionary must have a Key attribute or some other type of key associated with them."

Is it possible to mix local resources together with imported resources in the same Window.Resources?

The XAML is:

 <Window.Resources>
    <CollectionViewSource x:Key="cvsData" Source="{Binding Path=Data}">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="Country"/>
        </CollectionViewSource.GroupDescriptions>           
    </CollectionViewSource>

    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary x:Name="images" Source="pack://application:,,,/CoreWpfControls;component/Images.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

Thanks Jeremy

Jeremy Holt
  • 325
  • 6
  • 16
  • possible duplicate of [How to combine imported and local resources in WPF user control](http://stackoverflow.com/questions/1333786/how-to-combine-imported-and-local-resources-in-wpf-user-control) – Dzyann Jun 03 '15 at 13:57

1 Answers1

34

Yes, it's actually very simple. You just need to move the additional resources inside of the ResourceDictionary element.

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/CoreWpfControls;component/Images.xaml"/>
        </ResourceDictionary.MergedDictionaries>
        <CollectionViewSource x:Key="cvsData" Source="{Binding Path=Data}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="Country"/>
            </CollectionViewSource.GroupDescriptions>           
        </CollectionViewSource>
    </ResourceDictionary>
</Window.Resources>
Josh
  • 68,005
  • 14
  • 144
  • 156
  • Duh - how simple is that?? I looked everywhere in the documentation/Google and couldn't find the answer. Thanks so much!! – Jeremy Holt Apr 23 '10 at 03:04
  • 1
    Although it is 2+ years later, I found this extremely helpful. I search high and low to fix this same build error. – billmiller Aug 24 '12 at 20:15
  • This worked for me as well today, but if everything has to go inside the extra tag, it seems redundant. – Jess Sep 16 '15 at 20:47
  • It's been a while since I've worked with XAML, but I believe it's just a consequence of the way XAML is structured. Basically, XAML usually *implicitly* creates a ResourceDictionary to hold the child elements of Window.Resources as a sort of shortcut. But since you can't set a sub-property of an implied element, you need to explicitly wrap it when you need to access MergedDictionaries property. – Josh Sep 17 '15 at 21:55
  • Although it is 6+ years later, I found this extremely helpful. I search high and low to fix this same build error. – Jan 'splite' K. Feb 25 '17 at 14:25