-
Notifications
You must be signed in to change notification settings - Fork 6
/
MainViewModel.vb
78 lines (74 loc) · 2.8 KB
/
MainViewModel.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
Imports DevExpress.Mvvm
Imports EFCoreIssues.Issues
Imports DevExpress.Mvvm.DataAnnotations
Imports DevExpress.Data.Linq
Imports Microsoft.EntityFrameworkCore
Imports System.Linq
Imports System.Collections
Imports DevExpress.Mvvm.Xpf
Imports System
Public Class MainViewModel
Inherits ViewModelBase
Private _ItemsSource As EntityInstantFeedbackSource
Public ReadOnly Property ItemsSource As EntityInstantFeedbackSource
Get
If _ItemsSource Is Nothing Then
_ItemsSource = New EntityInstantFeedbackSource With {
.KeyExpression = NameOf(Issue.Id)
}
AddHandler _ItemsSource.GetQueryable, Sub(sender, e)
Dim context = New IssuesContext()
e.QueryableSource = context.Issues.AsNoTracking()
End Sub
End If
Return _ItemsSource
End Get
End Property
Private _Users As IList
Public ReadOnly Property Users As IList
Get
If _Users Is Nothing AndAlso Not DevExpress.Mvvm.ViewModelBase.IsInDesignMode Then
Dim context = New IssuesContext()
_Users = context.Users.[Select](Function(user) New With {
.Id = user.Id,
.Name = user.FirstName & " " + user.LastName
}).ToArray()
End If
Return _Users
End Get
End Property
<Command>
Public Sub DataSourceRefresh(ByVal args As DataSourceRefreshArgs)
_Users = Nothing
RaisePropertyChanged(Nameof(Users))
End Sub
<Command>
Public Sub CreateEditEntityViewModel(ByVal args As CreateEditItemViewModelArgs)
Dim context = New IssuesContext()
Dim item As Issue
If args.IsNewItem Then
item = New Issue() With {
.Created = Date.Now
}
context.Entry(item).State = EntityState.Added
Else
item = context.Issues.Find(args.Key)
End If
args.ViewModel = New EditItemViewModel(item, New EditIssueInfo(context, Users), title:=If(args.IsNewItem, "New ", "Edit ") & NameOf(Issue))
End Sub
<Command>
Public Sub ValidateRow(ByVal args As EditFormRowValidationArgs)
Dim context = CType(args.EditOperationContext, EditIssueInfo).DbContext
context.SaveChanges()
End Sub
<Command>
Public Sub ValidateRowDeletion(ByVal args As EditFormValidateRowDeletionArgs)
Dim key = CInt(args.Keys.[Single]())
Dim item = New Issue() With {
.Id = key
}
Dim context = New IssuesContext()
context.Entry(item).State = EntityState.Deleted
context.SaveChanges()
End Sub
End Class