Question

I've been programming in WPF for a while now coming from Windows Forms. Because of this I've always been linking my View and ViewModel directly together by giving my controls in the View a name and use that name in the ViewModel to update my View.

Now I did some searching and found out that there is something called MVVM. I've been studying it for a week now and tried to make my application 'better'.

First, I started using binding with Dependency Properties like this:

Public Shared SelKlantProperty As DependencyProperty = DependencyProperty.Register("SelKlant", GetType(String), GetType(MainWindow))
Public Property SelKlant As String
    Get
        Return DirectCast(GetValue(SelKlantProperty), String)
    End Get
    Set(value As String)
        SetValue(SelKlantProperty, value)
    End Set
End Property

I now have about 50 Properties made like that and my code is getting big pretty fast of those. Also my ClassDiagram is filled with the Fields of the Public Shared DependencyProperty and Properties of the Public Property.

I'm wondering, is there an easier or much better way to bind View and ViewModel? Most of the Properties I use are to put Text in TextBoxes.

Was it helpful?

Solution

You don't need to create dependency properties for your view model properties - regular properties will suffice. You will however have to use dependency properties if you're going to bind to that property (for example, TextBox.Text is a dependency property - you wouldn't be able to bind to it if it wasn't). You can keep your properties as they were - just make sure you implement INotifyPropertyChanged.

Dependency properties have other uses other than just for data binding, but I won't elaborate as it's a very large subject and you can find in other resources. Other than that, you can read more on how to implement the MVVM pattern here. This page also includes examples where you can see that the viewmodel does not need to have any dependency properties defined.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top