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.
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With