Search code examples
c#wpfxamldatatemplateresourcedictionary

Use a different DataTemplate with the same key if called from different exe


In a wpf.dll, I define a DataTemplate with a key MorphControl in ResourceDictionary XAML:

<ResourceDictionary>
       <DataTemplate x:Key="MorphControl">
        <n1:m1 />
    </DataTemplate>
</ResourceDictionary>

If this wpf.dll is called by an exe ( say, A.exe), then I would like the above FindResource("MorphControl") to return m1 related resrouce.

But, there is a possibility that wpf.dll is being called by another exe ( say, B.exe), in B.exe, the same key is being redefined to use another value, ie, inside B.exe, this definition exists:

<ResourceDictionary>
       <DataTemplate x:Key="MorphControl">
        <n2:m2 />
    </DataTemplate>
</ResourceDictionary>

So when B.exe is calling, I want FindResource["MorphControl"] to return m2 related resource.

I want to use the same wpf.dll for both A.exe and B.exe. And I don't want to move the MorphControl definition from wpf.dll to A.exe-- it has to remain at wpf.dll.

Also, this particular Resource key must remain the same throughout because inside wpf.dll, I have a DataTemplateSelector logic that I don't want to touch

How to do this, or is this possible?


Solution

  • I found my answer ( same one as this answer): just define the ResourceDictionary.MergeDictionaries with wpf.dll DataTemplate first, follow by B.exe DataTemplate. In other words:

    <Application.Resources>
       <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                      <ResourceDictionary Source="wpf.dll.xaml"/>
                      <ResourceDictionary Source="B.exe.xaml"/>
                </ResourceDictionary.MergedDictionaries>
       </ResourceDictionary>
    </Application.Resources>