Get a Resource from a ResourceDictionary with a key

10,418

Solution 1

According to the MSDN Page on MergedResourceDictionary's it is legal but not common to define a resource in a resource dictionary that is specified in a MergedDictionary. From above page.

It is legal to define resources within a ResourceDictionary that is specified as a merged dictionary, either as an alternative to specifying Source, or in addition to whatever resources are included from the specified source. However, this is not a common scenario; the main scenario for merged dictionaries is to merge resources from external file locations. If you want to specify resources within the markup for a page, you should typically define these in the main ResourceDictionary and not in the merged dictionaries.

So give this a try to see if it works.

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Images/LCCD logo.xaml" />   
            <ResourceDictionary Source="Images/LCCD bottom image.xaml" />
            <ResourceDictionary>
                <Style TargetType="{x:Type tab:FabTabItem}">
                    <Setter Property="Header" Value="{Binding Path=LabelText}"/>
                    <Setter Property="HeaderTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal" Margin="0,0,4,0">
                                    <TextBlock Text="{Binding}" TextTrimming="CharacterEllipsis"/>
                                </StackPanel>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

Solution 2

I ended up moving the <Style> further down into the file, onto the parent element of the elements that use that style.

It's not the best solution, but it works.

Share:
10,418
Moshe Katz
Author by

Moshe Katz

#SOreadytohelp I am currently a Computer Science graduate student (Ph. D. seeking) at the University of Maryland. I do a lot of programming (primarily web application development) for small businesses and nonprofits. In school, I focus mostly on security and context-aware systems, and I have also worked on counter-terrorism applications. You can find me most days in PHP and/or C#, but increasingly in JavaScript with a focus on Rich Internet Application areas (knockout.js, durandal, breeze.js, etc) and WebRTC.

Updated on June 18, 2022

Comments

  • Moshe Katz
    Moshe Katz almost 2 years

    I have a WPF window that is supposed to load two vector images from XAML files. (Each one is in a separate file for easier modification in Expression Design.)

    When I include the XAML files in a MergedDictionary, it works fine. Here is the code I use:

    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Images/LCCD logo.xaml" />
                <ResourceDictionary Source="Images/LCCD bottom image.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    

    and

    <Image Source="{Binding Source={StaticResource LCCDlogo}}" /> <!-- Simplified -->
    <Image Source="{Binding Source={StaticResource LCCDbar}}" /> <!-- Simplified -->
    

    However, I now need to add more to the Window's resources. The new resource belongs to this Window so we want it to be in the same file instead of an included file.

    When I add the following code between <Window.Resources> and <ResourceDictionary>, I get the following error:

    Code

    <Style TargetType="{x:Type tab:FabTabItem}">
            <Setter Property="Header" Value="{Binding Path=LabelText}"/>
            <Setter Property="HeaderTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Margin="0,0,4,0">
                            <TextBlock Text="{Binding}" TextTrimming="CharacterEllipsis"/>
                        </StackPanel>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    

    Warning

    The designer does not support loading dictionaries that mix 'ResourceDictionary' items without a key and other items in the same collection. Please ensure that the 'Resources' property does not contain 'ResourceDictionary' items without a key, or that the 'ResourceDictionary' item is the only element in the collection.

    So I change the <ResourceDictionary> tag to this:

    <ResourceDictionary x:Key="Images">
    

    However, I don't know how to access the resources inside this Dictionary now. How do you get the resources from inside a named ResourceDictionary?


    EDIT

    Never mind. This compiles but doesn't run.

    The error is:

    ''Resources' property has already been set on 'MainWindow'.

    I guess I'll have to do it some other way.