0

I have the following content control:

<ContentControl Content="{Binding Content}">
  <ContentControl.Resources>
    <DataTemplate DataType="{x:Type viewModels1:A}">
      <views1:A />
    </DataTemplate>
    <DataTemplate DataType="{x:Type viewModels2:B}">
      <views2:B />
    </DataTemplate>
  </ContentControl.Resources>
</ContentControl>

Now I want to move viewModels1:A and views1:A into a separate project P1 and viewModels2:B and views2:B into another project P2. P1 may contain a resource dictionary with

<DataTemplate DataType="{x:Type viewModels1:A}">
  <views1:A />
</DataTemplate>

and P2 a similar dictionary with

<DataTemplate DataType="{x:Type viewModels2:B}">
  <views2:B />
</DataTemplate>

How can I make the ContentControl of the main project aware of these mapping between view model and view? Is it possible not to state the types viewModels:A, views1:A, viewModels2:B and views2:B explicitly in the main project but just obtain the mapping somehow from P1 and P2?

MarkusParker
  • 1,264
  • 2
  • 17
  • 35
  • http://stackoverflow.com/questions/338056/resourcedictionary-in-a-separate-assembly maybe that helps. – grek40 Jan 06 '17 at 13:05
  • Ok, I could merge the dictionaries of the projects P1 and P2 in the resources of ContentControl. But I would like to avoid accessing the dictionaries from the main project directly, but rather obtain the mapping by some explicit interfaces of P1 and P2. – MarkusParker Jan 06 '17 at 13:19
  • Well you don't have to add them to the `ContentControl` resources, but at some point you have to say *"MainProject, take this assembly and load its content"*. For example, you can create a resource file in the assemblies, which references all local resources. Then load this single resource file into app.xaml. This would be a single line of import xaml to be maintained in the main project for each referenced assembly. I suppose it would also be possible to import somehow via code, but I don't know how and I don't see why. – grek40 Jan 06 '17 at 13:36
  • A reason for doing it via code is considering P1 or P2 as optional plug-ins which may be used but need not be used in the program. But if a dependency to these projects is hard coded in a xaml file, this is not possible anymore. – MarkusParker Jan 06 '17 at 13:49

1 Answers1

0

How can I make the ContentControl of the main project aware of these mapping between view model and view?

Merge the resource dictionaries in the external assemblies where your DataTemplates are defined into your application, for example in the OnStartup method of your App.xaml.cs or in the window or UserControl where the ContentControl resides depending on the scope in which you want the templates to be applied:

public MainWindow()
{
    InitializeComponent();
    Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("pack://application:,,,/WpfControlLibrary1;component/ResourceDictionaryWithDataTemplate.xaml")});
    theContentControl.Content = new WpfControlLibrary1.A()
}

The templates should then be applied as expected.

mm8
  • 163,881
  • 10
  • 57
  • 88