-
Notifications
You must be signed in to change notification settings - Fork 1
/
ContactDetailViewBlazorController.cs
102 lines (99 loc) · 4.86 KB
/
ContactDetailViewBlazorController.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
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Blazor.Components.Models;
using DevExpress.ExpressApp.Blazor.Layout;
using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp.Model;
using MySolution.Module.BusinessObjects;
using System;
using System.ComponentModel;
using TabCount.Module;
namespace DetailViewTabCount.Module.Blazor.Controllers {
public class ContactDetailViewBlazorController : ObjectViewController<DetailView, Contact> {
protected override void OnActivated() {
base.OnActivated();
View.DelayedItemsInitialization = false;
((BlazorLayoutManager)View.LayoutManager).ItemCreated += DxFormLayoutItemController_ItemCreated;
}
private void DxFormLayoutItemController_ItemCreated(object sender, BlazorLayoutManager.ItemCreatedEventArgs e) {
if (e.LayoutControlItem is DxFormLayoutTabPageModel layoutGroup && e.ModelLayoutElement.Parent is IModelTabbedGroup) {
foreach (var item in (IModelLayoutGroup)e.ModelLayoutElement) {
if (item is IModelLayoutViewItem layoutViewItem && View.FindItem(layoutViewItem.ViewItem.Id) is ListPropertyEditor propertyEditor) {
propertyEditor.Frame.GetController<NestedListViewTabCountController>().Initialize(layoutGroup);
propertyEditor.ValueRead += (s, e) => { propertyEditor.Frame.GetController<NestedListViewTabCountController>().SubscribeToListChanged(); };
}
}
}
}
protected override void OnDeactivated() {
((BlazorLayoutManager)View.LayoutManager).ItemCreated -= DxFormLayoutItemController_ItemCreated;
base.OnDeactivated();
}
}
public class NestedListViewTabCountController : ViewController<ListView> {
private DxFormLayoutTabPageModel layoutGroup;
public NestedListViewTabCountController() {
TargetViewNesting = Nesting.Nested;
}
protected override void OnActivated() {
base.OnActivated();
UpdateLayoutGroupText();
View.CollectionSource.CollectionChanging += CollectionSource_CollectionChanging;
View.CollectionSource.CollectionChanged += CollectionSource_CollectionChanged;
View.CollectionSource.CollectionReloaded += CollectionSource_CollectionReloaded;
SubscribeToListChanged();
}
internal void SubscribeToListChanged() {
if (GetBindingList(View.CollectionSource.Collection) is IBindingList bindingList) {
bindingList.ListChanged += CollectionSourceBindingList_ListChanged;
}
}
public void Initialize(DxFormLayoutTabPageModel layoutGroup) {
this.layoutGroup = layoutGroup;
UpdateLayoutGroupText();
}
private void CollectionSource_CollectionChanging(object sender, EventArgs e) {
UnsubscribeFromListChanged();
}
private void CollectionSource_CollectionChanged(object sender, EventArgs e) {
UpdateLayoutGroupText();
SubscribeToListChanged();
}
private void CollectionSource_CollectionReloaded(object sender, EventArgs e) {
UpdateLayoutGroupText();
}
private void CollectionSourceBindingList_ListChanged(object sender, ListChangedEventArgs e) {
UpdateLayoutGroupText();
}
private void UpdateLayoutGroupText() {
if (layoutGroup != null) {
int count = View.CollectionSource.GetCount();
layoutGroup.Caption = DetailViewControllerHelper.ClearItemCountInTabCaption(layoutGroup.Caption);
if (count > 0) {
layoutGroup.Caption = DetailViewControllerHelper.AddItemCountToTabCaption(layoutGroup.Caption, count);
}
}
}
protected override void OnDeactivated() {
View.CollectionSource.CollectionChanging -= CollectionSource_CollectionChanging;
View.CollectionSource.CollectionChanged -= CollectionSource_CollectionChanged;
View.CollectionSource.CollectionReloaded -= CollectionSource_CollectionReloaded;
UnsubscribeFromListChanged();
base.OnDeactivated();
}
private void UnsubscribeFromListChanged() {
if (GetBindingList(View.CollectionSource.Collection) is IBindingList bindingList) {
bindingList.ListChanged -= CollectionSourceBindingList_ListChanged;
}
}
private static IBindingList GetBindingList(object collection) {
switch (collection) {
case IBindingList bindingList:
return bindingList;
case IListSource listSource:
return listSource.GetList() as IBindingList;
default:
return null;
}
}
}
}