1
votes

I'm currently building a PRISM 5 application in VB.NET by using the MVVM pattern, using the PRISM ViewModelLocator. The PRISM ViewmodelLocator requires all Views to implement the Microsoft.Practises.Prism.Mvvm.IView Interface. This interface contains only one property: DataContext.

As a View (which is a FrameworkElement) already has a property DataContext, in c# it is sufficient to add ": IView" behind the View class name without providing a new implementation for the DataContext property (implicit interface implementation). By magic, the ViewModel is then automatically wired to the View through the Datacontext property.

In VB.NET this is not possible, as you cannot do implicit interface implementations. When adding "Implements IView" at the top of the class, VB instructs to provide an implementation for the DataContext property. As the FrameworkElement already has a Datacontext property, VB automatically adds an implementation for a Datacontext1 property. Of course, wiring up ViewModel to View doesn't work. Below is my code:

View

Imports Microsoft.Practices.Prism.Mvvm

Namespace Views
    Class MyWindow
        Inherits Window
        Implements IView

        Public Property DataContext1 As Object Implements IView.DataContext
    End Class
End Namespace

ViewModel

Imports Microsoft.Practices.Prism.Mvvm

Namespace ViewModels
    Public Class MyWindowViewModel
        Inherits BindableBase

        Private mTheText As String = "This is the text."

        Public Property TheText() As String
            Get
                Return Me.mTheText
            End Get
            Set(ByVal value As String)
                SetProperty(Of String)(Me.mTheText, value)
            End Set
        End Property
    End Class
End Namespace

Is there a best practise around this?

Until now, all I found that seems to work is add this line to the constructor of my View:

Datacontext = Datacontext1

But I find this a very dirty solution.

1

1 Answers

0
votes

Try with:

Public Shadows Property DataContext As Object Implements IView.DataContext

So your View would be:

Imports Microsoft.Practices.Prism.Mvvm

Namespace Views
    Class MyWindow
        Inherits Window
        Implements IView

        Public Shadows Property DataContext As Object Implements IView.DataContext
    End Class
End Namespace

However, if you wish to retain the dependency property behavior, you may want to update the actual base class' DataContext property in your new property's getter and setter.

You can find more information on this SO question.