Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you define a ResourceDictionary.MergedDictionary together with other (local) resources in <Windows.Resources>?

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

like image 574
Jeremy Holt Avatar asked Apr 23 '10 02:04

Jeremy Holt


People also ask

What can we store in a ResourceDictionary?

In Extensible Application Markup Language (XAML), the ResourceDictionary class is typically an implicit collection element that is the object element value of several Resources properties, when given in property element syntax. For details on implicit collections in XAML, see XAML Syntax Terminology.

What is ResourceDictionary?

A resource dictionary is a repository for XAML resources, such as styles, that your app uses. You define the resources in XAML and can then retrieve them in XAML using the {StaticResource} markup extension and {ThemeResource} markup extension s. You can also access resources with code, but that is less common.

How do I merge a resource dictionary in WPF?

To add a merged dictionary through code, you obtain a reference to the desired primary ResourceDictionary , get its MergedDictionaries property value, and call Add on the generic Collection that's contained in MergedDictionaries . The object you add must be a new ResourceDictionary .

How do you add a resource to a dictionary?

Tip You can create a resource dictionary file in Microsoft Visual Studio by using the Add > New Item… > Resource Dictionary option from the Project menu. Here, you define a resource dictionary in a separate XAML file called Dictionary1.


1 Answers

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>
like image 134
Josh Avatar answered Sep 21 '22 09:09

Josh