forked from dotnet/winforms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRelatedCurrencyManager.cs
More file actions
265 lines (230 loc) · 9.31 KB
/
Copy pathRelatedCurrencyManager.cs
File metadata and controls
265 lines (230 loc) · 9.31 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections;
using System.ComponentModel;
namespace System.Windows.Forms;
/// <summary>
/// Represents the child version of the System.Windows.Forms.ListManager
/// that is used when a parent/child relationship exists in a System.Windows.Forms.DataSet.
/// </summary>
internal class RelatedCurrencyManager : CurrencyManager, BindingContext.IInterceptedRelatedManager
{
private BindingManagerBase _parentManager;
private PropertyDescriptor _fieldInfo;
private readonly EventHandler? _interceptedParentHandler;
private static List<BindingManagerBase> IgnoreItemChangedTable { get; } = [];
internal RelatedCurrencyManager(BindingManagerBase parentManager, string dataField)
: this(parentManager, dataField, interceptedParentHandlerFactory: null)
{
}
internal RelatedCurrencyManager(
BindingManagerBase parentManager,
string dataField,
Func<RelatedCurrencyManager, EventHandler>? interceptedParentHandlerFactory)
: base(dataSource: null)
{
// The factory receives the fully-constructed child so a handler can close over it, mirroring
// `new ParentCurrentChangedHandler(cm)` on .NET Framework. It is built before Bind() primes.
_interceptedParentHandler = interceptedParentHandlerFactory?.Invoke(this);
Bind(parentManager, dataField);
}
[MemberNotNull(nameof(_parentManager))]
[MemberNotNull(nameof(_fieldInfo))]
internal void Bind(BindingManagerBase parentManager, string dataField)
{
Debug.Assert(parentManager is not null, "How could this be a null parentManager.");
// Unwire previous BindingManagerBase
UnwireParentManager(_parentManager);
_parentManager = parentManager;
_fieldInfo = parentManager.GetItemProperties().Find(dataField, ignoreCase: true)!;
if (_fieldInfo is null || !typeof(IList).IsAssignableFrom(_fieldInfo.PropertyType))
{
throw new ArgumentException(string.Format(SR.RelatedListManagerChild, dataField));
}
finalType = _fieldInfo.PropertyType;
// Wire new BindingManagerBase
WireParentManager(_parentManager);
if (_interceptedParentHandler is null)
{
// DEFAULT PATH - identical to current behaviour.
ParentManager_CurrentItemChanged(parentManager, EventArgs.Empty);
}
else
{
// OPT-IN PATH - prime through the supplied handler (Framework parity); avoids constructor-time AddNew().
_interceptedParentHandler(parentManager, EventArgs.Empty);
}
}
private void UnwireParentManager(BindingManagerBase? bmb)
{
if (bmb is not null)
{
if (_interceptedParentHandler is not null && bmb is CurrencyManager interceptedParent)
{
interceptedParent.CurrentChanged -= _interceptedParentHandler;
}
else
{
bmb.CurrentItemChanged -= ParentManager_CurrentItemChanged;
}
if (bmb is CurrencyManager currencyManager)
{
currencyManager.MetaDataChanged -= ParentManager_MetaDataChanged;
}
}
}
private void WireParentManager(BindingManagerBase bmb)
{
if (bmb is not null)
{
if (_interceptedParentHandler is not null && bmb is CurrencyManager interceptedParent)
{
// Framework parity: drive the child via the parent's CurrentChanged + the supplied handler.
interceptedParent.CurrentChanged += _interceptedParentHandler;
}
else
{
bmb.CurrentItemChanged += ParentManager_CurrentItemChanged;
}
if (bmb is CurrencyManager currencyManager)
{
currencyManager.MetaDataChanged += ParentManager_MetaDataChanged;
}
}
}
internal override PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[]? listAccessors)
{
PropertyDescriptor[] accessors;
if (listAccessors is not null && listAccessors.Length > 0)
{
accessors = new PropertyDescriptor[listAccessors.Length + 1];
listAccessors.CopyTo(accessors, 1);
}
else
{
accessors = new PropertyDescriptor[1];
}
// Set this accessor (add to the beginning)
accessors[0] = _fieldInfo;
// Get props
return _parentManager.GetItemProperties(accessors);
}
/// <summary>
/// Gets the properties of the item.
/// </summary>
public override PropertyDescriptorCollection GetItemProperties()
{
return GetItemProperties(listAccessors: null);
}
/// <summary>
/// Gets the name of the list.
/// </summary>
internal override string GetListName()
{
string name = GetListName([]);
if (name.Length > 0)
{
return name;
}
return base.GetListName();
}
/// <summary>
/// Gets the name of the specified list.
/// </summary>
protected internal override string GetListName(ArrayList? listAccessors)
{
if (listAccessors is null)
{
return string.Empty;
}
listAccessors.Insert(0, _fieldInfo);
return _parentManager.GetListName(listAccessors);
}
private void ParentManager_MetaDataChanged(object? sender, EventArgs e)
{
// Propagate MetaDataChanged events from the parent manager
OnMetaDataChanged(e);
}
private void ParentManager_CurrentItemChanged(object? sender, EventArgs e)
{
if (IgnoreItemChangedTable.Contains(_parentManager))
{
return;
}
int oldlistposition = listposition;
// we only pull the data from the controls into the backEnd. we do not care about keeping the lastGoodKnownRow
// when we are about to change the entire list in this currencymanager.
try
{
PullData();
}
catch (Exception ex)
{
OnDataError(ex);
}
if (_parentManager is CurrencyManager currencyManager)
{
if (currencyManager.Count > 0)
{
// Parent list has a current row, so get the related list from the relevant property on that row.
SetDataSource(_fieldInfo.GetValue(currencyManager.Current));
listposition = (Count > 0 ? 0 : -1);
}
else
{
// APPCOMPAT: bring back the Everett behavior where the currency manager adds an item and
// then it cancels the addition.
//
// really, really hocky.
// will throw if the list in the curManager is not IBindingList
// and this will fail if the IBindingList does not have list change notification. read on....
// when a new item will get added to an empty parent table,
// the table will fire OnCurrentChanged and this method will get executed again
// allowing us to set the data source to an object with the right properties (so we can show
// metadata at design time).
// we then call CancelCurrentEdit to remove the dummy row, but making sure to ignore any
// OnCurrentItemChanged that results from this action (to avoid infinite recursion)
currencyManager.AddNew();
try
{
IgnoreItemChangedTable.Add(currencyManager);
currencyManager.CancelCurrentEdit();
}
finally
{
if (IgnoreItemChangedTable.Contains(currencyManager))
{
IgnoreItemChangedTable.Remove(currencyManager);
}
}
}
}
else
{
// Case where the parent is not a list, but a single object
SetDataSource(_fieldInfo.GetValue(_parentManager.Current));
listposition = (Count > 0 ? 0 : -1);
}
if (oldlistposition != listposition)
{
OnPositionChanged(EventArgs.Empty);
}
OnCurrentChanged(EventArgs.Empty);
OnCurrentItemChanged(EventArgs.Empty);
}
// Explicit implementation of BindingContext.IInterceptedRelatedManager —
// exposes the members a custom handler in a derived BindingContext needs to drive this manager
// without reflection, mirroring the .NET Framework ParentCurrentChangedHandler targets.
bool BindingContext.IInterceptedRelatedManager.ParentHasRows
=> _parentManager is CurrencyManager currencyManager && currencyManager.Count > 0;
void BindingContext.IInterceptedRelatedManager.RaiseParentCurrentItemChanged(object? sender, EventArgs e)
=> ParentManager_CurrentItemChanged(sender, e);
void BindingContext.IInterceptedRelatedManager.BindToEmptyParentPlaceholder(IList placeholder)
{
SetDataSource(placeholder);
listposition = -1;
OnPositionChanged(EventArgs.Empty);
OnCurrentChanged(EventArgs.Empty);
OnCurrentItemChanged(EventArgs.Empty);
}
}