WPF XAML Data Binding
Introduction#
This example shows how to create a ViewModel and a View within the MVVM pattern and WPF, and how to bind the two together, so that each is updated whenever the other is changed.
Binding a String in the ViewModel to a TextBox in the View
SampleViewModel.vb
'Import classes related to WPF for simplicity
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Public Class SampleViewModel
Inherits DependencyObject
'A class acting as a ViewModel must inherit from DependencyObject
'A simple string property
Public Property SampleString as String
Get
Return CType(GetValue(SampleStringProperty), String)
End Get
Set(ByVal value as String)
SetValue(SampleStringProperty, value)
End Set
End Property
'The DependencyProperty that makes databinding actually work
'for the string above
Public Shared ReadOnly SampleStringProperty As DependencyProperty = _
DependencyProperty.Register("SampleString", _
GetType(String), GetType(SampleViewModel), _
New PropertyMetadata(Nothing))
End Class
A DependencyProperty can be easily added by using the wpfdp
code snippet (type wpfdp
, then press the TAB
key twice), however, the code snippet is not type safe, and will not compile under Option Strict On
.
SampleWindow.xaml
<Window x:Class="SampleWindow"
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
xmlns:des="https://schemas.microsoft.com/expression/blend/2008"
DataContext="{Binding}"
Loaded="Window_Loaded">
<Grid>
<TextBox>
<TextBox.Text>
<Binding Path="SampleString" />
</TextBox.Text>
</TextBox>
</Grid>
</Window>
SampleWindow.xaml.vb
Class SampleWindow
Private WithEvents myViewModel As New SampleViewModel()
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
Me.DataContext = myViewModel
End Sub
End Class
Note that this is a very rudimentary way to implement MVVM and databinding. A more robust practice would be to use a platform like Unity to “inject” the ViewModel into the View.