0
votes

I am rather new in WPF and I am developing a WPF application in VB.net. I have several controls in my Main Window. As I follow the MVVM pattern, I have a MainViewModel for my Main Window. In this ViewModel, I instatiate my user control view models.

Public Class MainViewModel

    Public Property myUserControlViewModel As UserControlViewModel

    Public Sub New()
        Dim myModel As Model

        myModel = New Model(parameter)
        myUserControlViewModel = New myUserControlViewModel (myModel)

    End Sub
End Class

Question1: Is this pattern following MVVM Correctly?

Question2: How can I force my user control to use myUserControlViewModel? I mean specificly this instance which is already created in my MainViewModel. How should I even introduce myUserControlViewModel to my User Control?

1

1 Answers

2
votes

Is this pattern following MVVM Correctly?

Yes.

How can I force my user control to use myUserControlViewModel?

Create a ContentControl in the view that binds to the myUserControlViewModel property and and includes a DataTemplate for the UserControlViewModel type:

<ContentControl Content="{Binding myUserControlViewModel}">
    <ContentControl.Resources>
        <DataTemplate DataType="{x:Type local:UserControlViewModel}">
            <local:UserControl1 />
        </DataTemplate>
    </ContentControl.Resources>
</ContentControl>

This will display the UserControlViewModel as a UserControl1.