This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CategoryView.cs
187 lines (161 loc) · 7.32 KB
/
CategoryView.cs
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
namespace DxExample
{
public partial class CategoryView : UserControl
{
public CategoryView()
{
InitializeComponent();
mvvmContext1.ViewModelType = typeof(CategoryViewModel);
var fluentAPI = mvvmContext1.OfType<CategoryViewModel>();
ExpandedMasterRowsManager expandedMasterRowsManager = new ExpandedMasterRowsManager(categoryGridView);
fluentAPI.SetBinding(gridControl1, grid => grid.DataSource, x => x.Categories);
fluentAPI.SetBinding(expandedMasterRowsManager, x => x.ExpandedMasterRowsCollection, y => y.ExpandedEntitiesCollection);
fluentAPI.SetBinding(countLabelControl, x => x.Text, y => y.ExpandedEntitiesCount);
fluentAPI.WithEvent<CustomMasterRowEventArgs>(categoryGridView, "MasterRowExpanded").EventToCommand(x => x.Update(null), ea => { return expandedMasterRowsManager.GetCategoryID(ea.RowHandle); });
fluentAPI.WithEvent<CustomMasterRowEventArgs>(categoryGridView, "MasterRowCollapsed").EventToCommand(x => x.Update(null), ea => { return expandedMasterRowsManager.GetCategoryID(ea.RowHandle); });
fluentAPI.WithEvent(categoryGridView, "DataSourceChanged").EventToCommand(x => x.UpdateDataBindings());
fluentAPI.WithEvent(categoryIdEdit, "EditValueChanged").EventToCommand(x => x.UpdateCommands(GetCategoryIDFromEditor()));
fluentAPI.BindCommand(buttonExpand, x => x.ExpandEntity(null), x => GetCategoryIDFromEditor());
fluentAPI.BindCommand(buttonCollapse, x => x.CollapseEntity(null), x => GetCategoryIDFromEditor());
fluentAPI.BindCommand(buttonDelete, x => x.DeleteEntity(null), x => GetCategoryIDFromEditor());
}
private int GetCategoryIDFromEditor()
{
return Convert.ToInt32(categoryIdEdit.EditValue);
}
class ExpandedMasterRowsManager : IDisposable
{
public ExpandedMasterRowsManager(GridView _view)
{
view = _view;
SubscribeViewEvents(true);
}
GridView view;
ObservableCollection<object> expandedMasterRowsCollection;
bool viewIsNull { get { return view == null; } }
bool lockExpandedMasterRowsCollectionChanged { get; set; }
bool lockMasterRowExpanded = false;
public ObservableCollection<object> ExpandedMasterRowsCollection
{
get
{
return expandedMasterRowsCollection;
}
set
{
if (expandedMasterRowsCollection == value) return;
SubscribeCollectionChangedEvent(false);
expandedMasterRowsCollection = value;
SubscribeCollectionChangedEvent(true);
}
}
private void SubscribeCollectionChangedEvent(bool subscribe)
{
if (expandedMasterRowsCollection == null) return;
if (subscribe)
expandedMasterRowsCollection.CollectionChanged += ExpandedMasterRowsCollectionChanged;
else
expandedMasterRowsCollection.CollectionChanged -= ExpandedMasterRowsCollectionChanged;
}
private void SubscribeViewEvents(bool subscribe)
{
if (viewIsNull) return;
if(subscribe)
{
view.Disposed += view_Disposed;
view.MasterRowExpanded += view_MasterRowExpanded;
view.MasterRowCollapsed += view_MasterRowCollapsed;
view.DataSourceChanged += view_DataSourceChanged;
}
else
{
view.Disposed -= view_Disposed;
view.MasterRowExpanded -= view_MasterRowExpanded;
view.MasterRowCollapsed -= view_MasterRowCollapsed;
view.DataSourceChanged -= view_DataSourceChanged;
view = null;
}
}
private void view_DataSourceChanged(object sender, EventArgs e)
{
lockExpandedMasterRowsCollectionChanged = true;
expandedMasterRowsCollection.Clear();
lockExpandedMasterRowsCollectionChanged = false;
}
private void view_Disposed(object sender, EventArgs e)
{
SubscribeViewEvents(false);
}
private void view_MasterRowCollapsed(object sender, CustomMasterRowEventArgs e)
{
MasterRowExpanded(false, GetCategoryID(e.RowHandle));
}
private void view_MasterRowExpanded(object sender, CustomMasterRowEventArgs e)
{
MasterRowExpanded(true, GetCategoryID(e.RowHandle));
}
public int GetCategoryID(int rowHandle)
{
return (view.GetRow(rowHandle) as Category).ID;
}
private void MasterRowExpanded(bool expanded, int categoryID)
{
if (lockMasterRowExpanded) return;
lockExpandedMasterRowsCollectionChanged = true;
if (expanded)
expandedMasterRowsCollection.Add(categoryID);
else
expandedMasterRowsCollection.Remove(categoryID);
lockExpandedMasterRowsCollectionChanged = false;
}
private void ExpandedMasterRowsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (lockExpandedMasterRowsCollectionChanged || viewIsNull) return;
lockMasterRowExpanded = true;
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
foreach (int categoryID in e.NewItems)
ExpandMasterRow(categoryID);
break;
case NotifyCollectionChangedAction.Remove:
foreach (int categoryID in e.OldItems)
CollapseMasterRow(categoryID);
break;
case NotifyCollectionChangedAction.Reset:
CollapseAllMasterRows();
break;
}
lockMasterRowExpanded = false;
}
private void ExpandMasterRow(int categoryID)
{
view.ExpandMasterRow(GetRowHandle(categoryID));
}
private void CollapseMasterRow(int categoryID)
{
view.CollapseMasterRow(GetRowHandle(categoryID));
}
private void CollapseAllMasterRows()
{
view.CollapseAllDetails();
}
private int GetRowHandle(int categoryID)
{
return view.LocateByValue("ID", categoryID, null);
}
public void Dispose()
{
SubscribeViewEvents(false);
SubscribeCollectionChangedEvent(false);
}
}
}
}