Get resource from ResourceDictionary using key

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

When I include XAML files in MergedDictionary , it works fine. Here is the code I'm using:

 <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, now I need to add more Window resources. The new resource belongs to this window, so we want it to be in the same file, and not in the included file.

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

the 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> 

Attention

The designer does not support loading dictionaries that mix ResourceDictionary elements without a key and other elements in the same collection. Verify that the Resource property does not contain keyless ResourceDictionary elements or that the 'ResourceDictionary' element is the only element in the collection.

So, I am changing the <ResourceDictionary> to this:

 <ResourceDictionary x:Key="Images"> 

However, I do not know how to access the resources inside this dictionary now. How do you get resources from the name ResourceDictionary ?


EDIT

Nothing. It compiles, but does not start.

Error:

'' The resource property is already set to 'MainWindow'.

I think I have to do it differently.

+6
source share
2 answers

According to the MSDN page on MergedResourceDictionary's, it is legal, but not common, to define a resource in the resource dictionary that is specified in MergedDictionary. To the top of the page.

The ResourceDictionary document allows you to define resources that are specified as a unified dictionary, either as an alternative to specifying a source, or in addition to any resources included from the specified source. However, this is not a general scenario; The main scenario for merged dictionaries is combining resources from external file locations. If you want to specify resources in the markup for the page, you should usually define them in the main ResourceDictionary, and not in combined dictionaries.

So, 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> 
+8
source

In the end, I moved <Style> further into the file to the parent element of the elements that use this style.

This is not the best solution, but it works.

+1
source

Source: https://habr.com/ru/post/917495/


All Articles