Data binding without INotifyPropertyChanged
Update Controls does not require that you implement INotifyPropertyChanged or declare a DependencyProperty. It connects controls directly to CLR properties. It discovers dependencies upon data through layers of intermediate code. This makes it perfect for the Model/View/ViewModel pattern; no extra code is needed in the ViewModel, which sits between the Model and the View.
Wrap the DataContext of your Window. The wrapper not only implements INotifyPropertyChanged for all of your object's properties, it also automatically detects their dependencies on other properties. There is no base class or interface to implement.
Partial Public Class Window1 Public Sub New() ' This call is required by the Windows Form Designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. DataContext = ForView.Wrap(New PersonPresentation(New Person())) End Sub End Class
Public Class PersonPresentation Private _person As Person Public Sub New(ByVal person As Person) _person = person End Sub Public Property Person() As Person Get Return _person End Get End Property Public ReadOnly Property FirstLast() As String Get Return _person.FirstName + " " + _person.LastName End Get End Property Public ReadOnly Property LastFirst() As String Get Return _person.LastName + ", " + _person.FirstName End Get End Property Public ReadOnly Property Title() As String Get If _person.DisplayStrategy = 0 Then Return "Person - " + FirstLast Else Return "Person - " + LastFirst End If End Get End Property End Class
When you add a field to a data object, select the field and press Ctrl+D, G. The Update Controls Visual Studio add-in creates a property to wrap the field. It adds the code required to keep track of changes to the data field.
Public Class Person Private _firstName As String Private _lastName As String Private _displayStrategy As Integer #Region "Independent properties" ' Generated by Update Controls -------------------------------- Private _indFirstName As New Independent() Private _indLastName As New Independent() Private _indDisplayStrategy As New Independent() Public Property FirstName() As String Get _indFirstName.OnGet() Return _firstName End Get Set(ByVal value As String) _indFirstName.OnSet() _firstName = value End Set End Property Public Property LastName() As String Get _indLastName.OnGet() Return _lastName End Get Set(ByVal value As String) _indLastName.OnSet() _lastName = value End Set End Property Public Property DisplayStrategy() As Integer Get _indDisplayStrategy.OnGet() Return _displayStrategy End Get Set(ByVal value As Integer) _indDisplayStrategy.OnSet() _displayStrategy = value End Set End Property ' End generated code -------------------------------- #End Region End Class
The rest is taken care of. As the user changes the independent properties in the underlying data object, Update Controls refreshes the dependent properties in the presentation object.
