How to Reference objects from another XAML file to objects in other resource dictionaries?

Jörg Debus 66 Reputation points
2021-04-21T16:47:19.603+00:00

Hi all,
I would like to structure my XAML code into different files related to different Views. So I coded the following:

 <Window.Resources>
 <ResourceDictionary>

 <!-- Create instance(s) of the ViewModel(s) and 
 related objects.
 -->  
 <m:ViewModelBase x:Key="ViewModelBase"/>
 <m:GatherFileSet x:Key="GatherFileSet"/>
 <m:LoadFileSet x:Key="LoadFileSet"/>
 <m:ExecuteTheJob x:Key="ExecuteTheJob"/>
 <m:SaveFileSet x:Key="SaveFileSet"/>
 <m:ChangeSettings x:Key="ChangeSettings"/>

 <m:StringBuilderConverter x:Key="StringToBuilder"/>
 <m:EnumHasFlagConverter x:Key="HasFlag"/>
 <m:EnumEqualityConverter x:Key="EnumEqualTrue"/>
 <m:EnumEqualityConverter x:Key="EnumEqualFalse" Negation="True" />

 <ResourceDictionary.MergedDictionaries>
 <ResourceDictionary 
 <ResourceDictionary Source="pack://application:,,,/10 Classes/10 Views/10 Styles/GeneralStyles.xaml" />
 <ResourceDictionary Source="pack://application:,,,/10 Classes/10 Views/ExecuteTheJobStyles.xaml" />
 </ResourceDictionary.MergedDictionaries>

 </ResourceDictionary>
 </Window.Resources>

Unfortunately all references with StaticResource or DynamicResource coded in the RD "ExecuteTheJobStyles.xaml" against objects outside of the file don't work against the Window.Resources nor against the GeneralStyles.xaml-file. References from Window.Resources to objects within the "external" xaml-files do work.

Works as designed or is my code wrong?

TIA.
JD

XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
808 questions
{count} votes

1 answer

Sort by: Most helpful
  1. DaisyTian-1203 11,621 Reputation points
    2021-04-22T07:12:50.65+00:00

    I made a sample to use UserControl.Xaml, ResourceDictionary.xaml and Converter.cs from my WPF project which named MVVMDemo in UseXamlFromOthers.
    The project directory of MVVMDemois as below:
    90262-capture.png

    Right click UseXamlFromOthers's references and add MVVMDemo , then add below code in UseXamlFromOthers's App.xaml

      <ResourceDictionary>  
                <ResourceDictionary.MergedDictionaries>  
                    <ResourceDictionary Source="pack://application:,,,/MVVMDemo;component/Styles/MyMVVMStyles.xaml"/>  
                </ResourceDictionary.MergedDictionaries>  
            </ResourceDictionary>  
    

    Add xmlns:mvvm="clr-namespace:MVVMDemo;assembly=MVVMDemo" and xmlns:sys="clr-namespace:System;assembly=mscorlib" in UseXamlFromOthers's Xaml, then use code from MVVMDemo in UseXamlFromOthers like below:

    <Window.Resources>  
                <mvvm:PersonViewModel x:Key="VM"></mvvm:PersonViewModel>  
                <mvvm:Converter x:Key="conv"></mvvm:Converter>  
            </Window.Resources>  
          
            <StackPanel>  
                <StackPanel Background="Azure">  
                    <Label Content="Use UserControl from MVVMDemo" />  
                    <mvvm:UserControl1 Width="300" Height="200" DataContext="{StaticResource VM}"></mvvm:UserControl1>  
                </StackPanel>  
          
                <StackPanel Background="LightGreen">  
                    <Label Content="Use DictionaryStyle from MVVMDemo" />  
                    <Button Name="btn" Content="Use Dictionary MVVMDemo " Style="{StaticResource StyleA}" Width="200" Height="30"></Button>  
                </StackPanel>  
          
                <StackPanel Background="LightPink">  
                    <Label Content="Use Converter from MVVMDemo" />  
                    <TextBox Margin="4"  Text="{Binding Source={x:Static sys:DateTime.Now}, Converter={StaticResource conv},Mode=OneWay}" Width="200"/>  
                </StackPanel>  
          
            </StackPanel>  
    

    Result picture is:
    90252-capture2.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.