/
SafeBindingSource.cs
72 lines (66 loc) · 2.44 KB
/
SafeBindingSource.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
namespace ACT_Plugin
{
class SafeBindingSource : BindingSource
{
public Control UIThreadMarshal { get; set; }
private delegate void OnListChangedDelegate(ListChangedEventArgs e);
private Dictionary<string, SafeBindingSource> relatedBindingSources = new Dictionary<string, SafeBindingSource>();
public SafeBindingSource()
: base()
{
}
public SafeBindingSource(IContainer components)
: base(components)
{
}
public SafeBindingSource(object dataSource, string dataMember)
: base(dataSource, dataMember)
{
}
public override CurrencyManager GetRelatedCurrencyManager(string dataMember)
{
CurrencyManager result = null;
if (string.IsNullOrEmpty(dataMember))
{
//If you call the CurrencyManager property you end up in a recursive loop
Type t = this.GetType().BaseType;
result = t.InvokeMember("currencyManager", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField, null, this, null) as CurrencyManager;
}
else if (dataMember.IndexOf(".") != -1)
{
//dot notation is not supported by the BindingSource
result = null;
}
else if (relatedBindingSources.ContainsKey(dataMember))
{
result = relatedBindingSources[dataMember].CurrencyManager;
}
else
{
SafeBindingSource bindingSource = new SafeBindingSource(this, dataMember);
this.relatedBindingSources.Add(dataMember, bindingSource);
result = bindingSource.CurrencyManager;
}
return result;
}
protected override void OnListChanged(System.ComponentModel.ListChangedEventArgs e)
{
if (UIThreadMarshal != null && UIThreadMarshal.InvokeRequired)
{
OnListChangedDelegate changeDelegate = new OnListChangedDelegate(base.OnListChanged);
UIThreadMarshal.Invoke(new MethodInvoker(delegate { OnListChanged(e); }));
}
else
{
base.OnListChanged(e);
}
}
}
}