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

Jörg Debus 66 Reputation points

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


 <!-- 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 Source="pack://application:,,,/10 Classes/10 Views/10 Styles/GeneralStyles.xaml" />
 <ResourceDictionary Source="pack://application:,,,/10 Classes/10 Views/ExecuteTheJobStyles.xaml" />


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?


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

1 answer

Sort by: Most helpful
  1. DaisyTian-1203 11,506 Reputation points

    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:

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

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

    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:

                <mvvm:PersonViewModel x:Key="VM"></mvvm:PersonViewModel>  
                <mvvm:Converter x:Key="conv"></mvvm:Converter>  
                <StackPanel Background="Azure">  
                    <Label Content="Use UserControl from MVVMDemo" />  
                    <mvvm:UserControl1 Width="300" Height="200" DataContext="{StaticResource VM}"></mvvm:UserControl1>  
                <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 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"/>  

    Result picture is:

    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.