Search code examples
c#wpfxamlmvvmpixelsense

Multiple views binding to multiple ViewModels


I want to create multiple instances of a SearchTagView and bind each of those to its own SearchTagViewModel.

I'm using the WAF framework for WPF which follows the MVVM-concept and furthermore Dependency injection (IoC). The application is developed for the SUR40 using the SurfaceSDK.

This means that the views are instantiated like this:

TagVisualizationDefinition tagDefinition = new TagVisualizationDefinition();
tagDefinition.Source = new Uri("Views/SearchTagView.xaml", UriKind.Relative);
tagVisualizer.Definitions.Add(tagDefinition);

tagVisualizer is a control element of type TagVisualizer in SearchView. So multiple SearchTagViews are placed in one SearchView. This works.

The problem is that because of dependency injection all SearchTagViews use the same SearchTagViewModel:

xmlns:vm="clr-namespace:Applications.ViewModels;assembly=Applications"

How can I use a different ViewModel for each View following the MVVM?


Solution

  • Alan's hint concerning the non-shared attribute was good, but I couldn't use it to solve my problem. The problem was that the MEF is working before I init my TagVisualizationDefinitions.

    The only solution was to set the binding in code-behind of the parent user control in the method for the event TagVisualization_Loaded