Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merged dictionaries and local resources

I have a Styles.xaml that groups many ResourceDictionarys inside a MergedDictionary.

I imported Styles.xaml in my UserControl.Resources

<UserControl.Resources>
    <ResourceDictionary Source="Dictionaries\Styles.xaml" />
</UserControl.Resources>

but when I try to add a converter

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Dictionaries\Styles.xaml" /> <--! Exception -->
        </ResourceDictionary.MergedDictionaries>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
    </ResourceDictionary>
</UserControl.Resources>

it raises

ArgumentNullException: Value cannot be null.
Parameter name: item

Wrapping the converter inside another MergedDictionary has no effect.
How can I solve this?
Thank you all!


SOLVED

I eventually figured it out: the Exception was raised inside one the .xaml files, but Visual Studio does not provide enough info to locate the faulty line.
Following code does work.

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Dictionaries\Styles.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
    </ResourceDictionary>
</UserControl.Resources>
like image 693
Sergio Avatar asked Sep 05 '13 00:09

Sergio


People also ask

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.

What is a resource dictionary?

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.

What all we can store in a resource dictionary?

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.


Video Answer


1 Answers

Try this

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/PROJECTNAMESPACE (TestProject.Something);component/Dictionaries/Styles.xaml" /> 
        </ResourceDictionary.MergedDictionaries>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
    </ResourceDictionary>
</UserControl.Resources>
like image 103
123 456 789 0 Avatar answered Nov 03 '22 00:11

123 456 789 0