question

JoergDebus avatar image
0 Votes"
JoergDebus asked JoergDebus commented

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

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


dotnet-wpf-xaml
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@JoergDebus
Did you check my below answer? Did it work for your question? Please let me know if there is anything that I can help here.

0 Votes 0 ·

1 Answer

DaisyTian-1203 avatar image
0 Votes"
DaisyTian-1203 answered JoergDebus commented

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.


capture.png (8.7 KiB)
capture2.png (11.4 KiB)
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@DaisyTian-MSFT

Hi Daisy,
many thanx for your detailed advice.

Your example assured my actual experience, that you can reference objects in the XAML files from primary resources. My problem is referencing primary XAML from a XAML file. Just the opposite of your example, I'm afraid. I would like to structure my XAML code in chunks associated with my view models. To implement this structure, objects from the XAML files must reference primary XAML as the top view framing the application GUI. So objects from the ResourceDictionary in line 22 of my original Post has to refer to the StaticResource <m:ExecuteTheJob x:Key="ExecuteTheJob"/> coded in line 10.

The docs are not very helpful here. The statement there say, that "MergedDictionaries" are placed in the search hierarchy just above the primary XAML. From this definition, referencing from XAML files to primary XAML would be no forward reference and should work. But it does not work.

Works as designed?

TIA
JD

0 Votes 0 ·

@JoergDebus
I put my ViewModel, Converter and ResourceDictionary in to the App.xaml like below, my project also runs well.

   <Application.Resources>
         <ResourceDictionary>
             <ResourceDictionary.MergedDictionaries>
                 <ResourceDictionary Source="pack://application:,,,/MVVMDemo;component/Styles/MyMVVMStyles.xaml"/>
             </ResourceDictionary.MergedDictionaries>
             <mvvm:PersonViewModel x:Key="VM"></mvvm:PersonViewModel>
             <mvvm:Converter x:Key="conv"></mvvm:Converter>
         </ResourceDictionary>
     </Application.Resources>

0 Votes 0 ·

Thanx for your prompt answer.

But again your test does not reflect my problem: You have no references from the App.Xaml to any object in Window.Resources. Pls. add one to your code and we will see what happens. Consider to add a Style that references a StaticResource in Window.Resources.

Best regards
JD

0 Votes 0 ·
Show more comments

@daisytian-msft
Hi Tian,
sorry for answering so late! I had some priority issues to handle.

The best way to reproduce my problem may be the following:

  1. Put the complete definition of the property Text in line 19 of your original answer as a Style in a separate XAML resource file and code a x:Key name like "TextSpecs".

  2. Add this as a new ResourceDictionary to MergedDictionaries in your ResourceDictionary.xaml.

  3. Replace the Text definiton with Text="{StaticResource TextSpecs}"

  4. Leave the rest in in Window as coded in your original post.

  5. Try to run. You have solved my problem when the Binding does find the Converter "conv".

I hope you will succeed.

Best regards
JD

0 Votes 0 ·