Search code examples
wpfmvvmunity-containerprismviewmodel

MVVM Multiple Views same ViewModel in Prism


I have a question regarding multiple user controls views with the same view model type. I can't seems to find specific answers for my confusion but this is quite speculative.

I have.

    <StackPanel Orientation="Vertical">

        <TextBlock Text="Signature Summary" FontSize="14" FontWeight="Bold" TextAlignment="Center" Height="30"/>

        <my:ParameterFileSummaryView DataContext="{Binding ParamterFile1ViewModel}"/>

        <my:ParameterFileSummaryView DataContext="{Binding ParamterFile2ViewModel}"/>

        <my:ParameterFileSummaryView DataContext="{Binding ParamterFile3ViewModel}"/>

        <my:ParameterFileSummaryView DataContext="{Binding ParamterFile4ViewModel}"/>

        <my:ParameterFileSummaryView DataContext="{Binding ParamterFile5ViewModel}"/>

    </StackPanel>

the stack panel is a container inside a main view which has a dependancy property view model datacontext used for a Prism/Unity IoC architecture.

These are therefore binding the datacontect for these individual views to properties of the interface of the main view via another interface.

It all seems to work ok and the binding of the elements in the ParameterFileSummaryView bind nicely to the values set on the, say for the first one, ParamterFile1ViewModel.

Which is exactly what I want. But of cource these ViewModels are built within the ViewModel of the main window and not out of the Unity container.... It all feels a little bit hacky. Is there a cleaner way to implement what I am attempting.

Apologies if it is really a moot question... but I can't see the wood for the trees. If the question confuses I will add edits, please be patient I am not an expert :) .


Solution

  • To pull the right ViewModel for the main view, use the container. Either as a service locator and have each model resolve to the same type and pull IEnumerable<TViewModel> then bind that to the view.