/
DataBindingViaMVVMContextUserControl.vb
64 lines (59 loc) · 2.47 KB
/
DataBindingViaMVVMContextUserControl.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
Imports DevExpress.MVVM
Imports DevExpress.MVVM.DataAnnotations
Imports DevExpress.MVVM.POCO
Imports DevExpress.Utils.MVVM
Imports DevExpress.XtraEditors
Imports System
Imports System.Windows.Forms
Namespace DxSamplePOCOBindableProperties
Partial Public Class DataBindingViaMVVMContextUserControl
Inherits DevExpress.XtraEditors.XtraUserControl
Public Sub New()
InitializeComponent()
Dim mvvmContext As New MVVMContext()
mvvmContext.ContainerControl = Me
Dim editor As New TextEdit()
editor.Dock = DockStyle.Top
editor.Properties.NullValuePrompt = "Please, enter the Title here..."
editor.Properties.NullValuePromptShowForEmptyValue = True
Dim commandButton As New SimpleButton()
commandButton.Dock = DockStyle.Top
commandButton.Text = "Report the Title property value"
commandButton.Parent = Me
editor.Parent = Me
' Set type of POCO-ViewModel
mvvmContext.ViewModelType = GetType(ViewModel)
' Data binding for the Title property (via MVVMContext API)
mvvmContext.SetBinding(editor, Function(e) e.EditValue, "Title")
' UI binding for the Report command
Dim viewModel As ViewModel = mvvmContext.GetViewModel(Of ViewModel)()
AddHandler commandButton.Click, Sub(s, e) XtraMessageBox.Show(viewModel.GetTitleAsHumanReadableString())
End Sub
Private Sub OnDisposing()
Dim context = MVVMContext.FromControl(Me)
If context IsNot Nothing Then
context.Dispose()
End If
End Sub
''
'' POCO View Model provides out-of-the-box support of the INotifyPropertyChanged.
''
Public Class ViewModel
' Bindable property will be created from this property.
Public Overridable Property Title() As String
' Just a method for readability
Public Function GetTitleAsHumanReadableString() As String
If Title Is Nothing Then
Return "(Null)"
End If
If Title.Length = 0 Then
Return "(Empty)"
End If
If String.IsNullOrWhiteSpace(Title) Then
Return "(WhiteSpace)"
End If
Return Title
End Function
End Class
End Class
End Namespace