This repository has been archived by the owner on Oct 16, 2023. It is now read-only.
/
MasterDetailMVVMHelper.vb
161 lines (132 loc) · 5.63 KB
/
MasterDetailMVVMHelper.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Collections.Specialized
Imports System.Windows
Imports System.Windows.Threading
Imports DevExpress.Xpf.Grid
Namespace WPFDataGridApp15
Public Class MasterDetailMVVMHelper
Private ReadOnly Shared instanceDictionary As Dictionary(Of GridControl, MasterDetailMVVMHelper)
Private expandedRowsUpdate As Boolean
Private privateTargetControl As GridControl
Public Property TargetControl() As GridControl
Get
Return privateTargetControl
End Get
Private Set(ByVal value As GridControl)
privateTargetControl = value
End Set
End Property
Private privateExpandedItemsCollection As IList
Public Property ExpandedItemsCollection() As IList
Get
Return privateExpandedItemsCollection
End Get
Private Set(ByVal value As IList)
privateExpandedItemsCollection = value
End Set
End Property
Shared Sub New()
instanceDictionary = New Dictionary(Of GridControl, MasterDetailMVVMHelper)()
End Sub
Private Sub OnTargetControlChanged()
End Sub
Private Shared Function CreateInstance() As MasterDetailMVVMHelper
Return New MasterDetailMVVMHelper()
End Function
Public Shared ReadOnly ExpandedMasterRowsSourceProperty As DependencyProperty = DependencyProperty.RegisterAttached("ExpandedMasterRowsSource", GetType(IList), GetType(MasterDetailMVVMHelper), New UIPropertyMetadata(Nothing, New PropertyChangedCallback(AddressOf OnExpandedMasterRowsSourceChanged)))
Public Shared Function GetExpandedMasterRowsSource(ByVal target As GridControl) As IList
Return CType(target.GetValue(ExpandedMasterRowsSourceProperty), IList)
End Function
Public Shared Sub SetExpandedMasterRowsSource(ByVal target As GridControl, ByVal value As IList)
target.SetValue(ExpandedMasterRowsSourceProperty, value)
End Sub
Private Shared Sub OnExpandedMasterRowsSourceChanged(ByVal o As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
Dim currentInstance As MasterDetailMVVMHelper
Dim targetGridControl As GridControl = CType(o, GridControl)
If (Not instanceDictionary.ContainsKey(targetGridControl)) Then
instanceDictionary(targetGridControl) = CreateInstance()
End If
currentInstance = instanceDictionary(targetGridControl)
currentInstance.SetTargetControl(targetGridControl)
currentInstance.Instance_OnExpandedMasterRowsSourceChanged(TryCast(e.NewValue, IList))
End Sub
Protected Friend Sub Instance_OnExpandedMasterRowsSourceChanged(ByVal newValue As IList)
ExpandedItemsCollection = newValue
Dispatcher.CurrentDispatcher.BeginInvoke(New Action(Function() AnonymousMethod1()), DispatcherPriority.Loaded)
Dim collectionWithNotification As INotifyCollectionChanged = TryCast(newValue, INotifyCollectionChanged)
If collectionWithNotification IsNot Nothing Then
AddHandler collectionWithNotification.CollectionChanged, AddressOf OnExpandedItemCollectionChanged
End If
End Sub
Private Function AnonymousMethod1() As Boolean
ToggleItemRowsExpansion(ExpandedItemsCollection, True)
Return True
End Function
Private Sub OnExpandedItemCollectionChanged(ByVal sender As Object, ByVal e As NotifyCollectionChangedEventArgs)
If e.Action = NotifyCollectionChangedAction.Add Then
ToggleItemRowsExpansion(e.NewItems, True)
End If
If e.Action = NotifyCollectionChangedAction.Remove Then
ToggleItemRowsExpansion(e.OldItems, False)
End If
If e.Action = NotifyCollectionChangedAction.Replace Then
ToggleItemRowsExpansion(e.OldItems, False)
ToggleItemRowsExpansion(e.NewItems, True)
End If
If e.Action = NotifyCollectionChangedAction.Reset Then
For i As Integer = 0 To TargetControl.VisibleRowCount - 1
Dim rowHandle As Integer = TargetControl.GetRowHandleByVisibleIndex(i)
If TargetControl.IsGroupRowHandle(rowHandle) Then
Continue For
End If
If TargetControl.IsMasterRowExpanded(rowHandle) Then
TargetControl.CollapseMasterRow(rowHandle)
End If
Next i
End If
End Sub
Private Sub ToggleItemRowsExpansion(ByVal itemList As IList, ByVal expand As Boolean)
If itemList.Count = 0 Then
Return
End If
expandedRowsUpdate = True
For Each item As Object In itemList
Dim itemIndex As Integer = (CType(TargetControl.ItemsSource, IList)).IndexOf(item)
If itemIndex <> -1 Then
Dim rowHandle As Integer = TargetControl.GetRowHandleByListIndex(itemIndex)
TargetControl.SetMasterRowExpanded(rowHandle, expand)
End If
Next item
expandedRowsUpdate = False
End Sub
Private Sub SetTargetControl(ByVal grid As GridControl)
If TargetControl IsNot Nothing Then
RemoveHandler TargetControl.MasterRowCollapsed, AddressOf TargetControl_MasterRowCollapsed
RemoveHandler TargetControl.MasterRowExpanded, AddressOf TargetControl_MasterRowExpanded
End If
TargetControl = grid
If TargetControl IsNot Nothing Then
AddHandler TargetControl.MasterRowCollapsed, AddressOf TargetControl_MasterRowCollapsed
AddHandler TargetControl.MasterRowExpanded, AddressOf TargetControl_MasterRowExpanded
End If
End Sub
Private Sub TargetControl_MasterRowExpanded(ByVal sender As Object, ByVal e As RowEventArgs)
If (Not InternalExpandedRowsUpdate) Then
ExpandedItemsCollection.Add(e.Row)
End If
End Sub
Private Sub TargetControl_MasterRowCollapsed(ByVal sender As Object, ByVal e As RowEventArgs)
If (Not InternalExpandedRowsUpdate) Then
ExpandedItemsCollection.Remove(e.Row)
End If
End Sub
Protected Friend ReadOnly Property InternalExpandedRowsUpdate() As Boolean
Get
Return expandedRowsUpdate
End Get
End Property
End Class
End Namespace