-
Notifications
You must be signed in to change notification settings - Fork 1
/
ControlBinder_ToolStrip.cs
41 lines (36 loc) · 1.16 KB
/
ControlBinder_ToolStrip.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
using System;
using System.Linq.Expressions;
using System.Reflection;
using System.Windows.Forms;
namespace WinForms.Binding
{
public partial class ControlBinder<T>
{
public void Add(ToolStripTextBox control, Func<T, string> setProperty, Action<T> setControl)
{
_setControls.Add(setControl);
control.TextChanged += (sender, args) =>
{
if (_suspend) return;
var propertyName = setProperty.Invoke(Object);
IsDirty = true;
PropertyUpdated?.Invoke(this, Object, propertyName);
};
}
public void Add(ToolStripTextBox control, Expression<Func<T, object>> property)
{
PropertyInfo pi = GetProperty(property);
Func<T, string> setProperty = (doc) =>
{
pi.SetValue(doc, control.Text);
return pi.Name;
};
var func = property.Compile();
Action<T> setControl = (doc) =>
{
control.Text = func.Invoke(doc)?.ToString();
};
Add(control, setProperty, setControl);
}
}
}