-
Notifications
You must be signed in to change notification settings - Fork 2
/
MainView.pas
109 lines (88 loc) · 2.38 KB
/
MainView.pas
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
unit MainView;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Knockoff.Binding, Vcl.Samples.Spin;
type
TMainViewForm = class(TForm)
GroupBox1: TGroupBox;
[Bind('Value', 'FirstName')]
edtFirstName: TEdit;
[Bind('Value', 'LastName')]
edtLastName: TEdit;
[Bind('Text', 'FullName')]
lblFullName: TLabel;
GroupBox2: TGroupBox;
[Bind('Click', 'RegisterClick')]
[Bind('Disabled', 'HasClickedTooManyTimes')]
btnRegisterClick: TButton;
[Bind('Text', 'NumberOfClicks')]
lblClickCount: TLabel;
[Bind('Click', 'ResetClicks')]
[Bind('Visible', 'HasClickedTooManyTimes')]
btnResetClicks: TButton;
[Bind('Visible', 'HasClickedTooManyTimes')]
lblClickedTooManyTimes: TLabel;
GroupBox3: TGroupBox;
[Bind('Value', 'ChosenTicket')]
[BindOptions('Tickets')]
[BindOptionsCaption('Choose...')]
[BindOptionsText('Name')]
cbTickets: TComboBox;
[Bind('Text', 'ChosenTicket.Price')]
lblPrice: TLabel;
[Bind('Click', 'ResetTicket')]
[Bind('Enabled', 'ChosenTicket')]
btnClear: TButton;
GroupBox4: TGroupBox;
[Bind('Value', 'Country')]
[BindOptions('AvailableCountries')]
[BindOptionsCaption('Choose...')]
cbAvailableCountries: TComboBox;
SpinEdit1: TSpinEdit;
procedure FormCreate(Sender: TObject);
end;
var
MainViewForm: TMainViewForm;
implementation
{$R *.dfm}
uses
Rtti,
Knockoff.Observable,
Knockoff.Binding.Components,
MainViewModel;
var
vm: TViewModel;
type
TSpinEditBinding = class(TBinding<TSpinEdit>)
protected
procedure HandleChange(Sender: TObject);
function InitGetValue(const observable: IObservable): TFunc<TValue>; override;
procedure InitTarget; override;
end;
{ TMainForm }
procedure TMainViewForm.FormCreate(Sender: TObject);
begin
vm := TViewModel.Create('John', 'Doe');
ApplyBindings(Self, vm);
TSpinEditBinding.Create(SpinEdit1, vm.Number as IObservable);
end;
{ TSpinEditBinding }
procedure TSpinEditBinding.HandleChange(Sender: TObject);
begin
Source.Value := Target.Value;
end;
function TSpinEditBinding.InitGetValue(
const observable: IObservable): TFunc<TValue>;
begin
Result :=
function: TValue
begin
Target.Value := observable.Value.AsInteger;
end;
end;
procedure TSpinEditBinding.InitTarget;
begin
Target.OnChange := HandleChange;
end;
end.