unit View.MainView;
interface
uses
ViewModel.MainViewModel,
Winapi.Windows,
Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs, Data.Bind.Components,
Data.Bind.ObjectScope, Data.Bind.GenData, System.Rtti,
System.Bindings.Outputs,
Vcl.Bind.Editors, Data.Bind.EngExt,
Vcl.Bind.DBEngExt,
Vcl.StdCtrls, System.Actions,
Vcl.ActnList;
type
TMainView =
class( TForm )
ViewModelSource: TAdapterBindSource;
ViewModelGenerator: TDataGeneratorAdapter;
Edit1: TEdit;
Edit2: TEdit;
BindingsList1: TBindingsList;
LinkControlToField1: TLinkControlToField;
LinkControlToField2: TLinkControlToField;
procedure ViewModelSourceCreateAdapter( Sender: TObject;
var ABindSourceAdapter: TBindSourceAdapter );
private
FViewModelAdapter: TObjectBindSourceAdapter<TMainViewModel>;
function GetViewModel: TMainViewModel;
procedure SetViewModel(
const Value: TMainViewModel );
protected
procedure Notification( AComponent: TComponent; Operation: TOperation );
override;
public
property ViewModel: TMainViewModel
read GetViewModel
write SetViewModel;
end;
var
MainView: TMainView;
implementation
{$R *.dfm}
function TMainView.GetViewModel: TMainViewModel;
begin
Result := FViewModelAdapter.DataObject;
end;
procedure TMainView.Notification( AComponent: TComponent; Operation: TOperation );
begin
inherited;
if Operation = opRemove
then
begin
if ViewModel = AComponent
then
ViewModel :=
nil;
end;
end;
procedure TMainView.SetViewModel(
const Value: TMainViewModel );
var
LOldViewModel: TComponent;
begin
if FViewModelAdapter.DataObject <> Value
then
begin
LOldViewModel := FViewModelAdapter.DataObject;
if Assigned( LOldViewModel )
then
LOldViewModel.RemoveFreeNotification( Self );
FViewModelAdapter.SetDataObject( Value, False );
if Assigned( Value )
then
Value.FreeNotification( Self );
FViewModelAdapter.Active := True;
end;
end;
procedure TMainView.ViewModelSourceCreateAdapter( Sender: TObject;
var ABindSourceAdapter: TBindSourceAdapter );
begin
FViewModelAdapter := TObjectBindSourceAdapter<TMainViewModel>.Create( Self );
ABindSourceAdapter := FViewModelAdapter;
ABindSourceAdapter.AutoEdit := True;
ABindSourceAdapter.AutoPost := True;
end;
end.