Skip to content

Instantly share code, notes, and snippets.

@ritalin
Created June 12, 2014 05:07
Show Gist options
  • Save ritalin/eb1f3110a4112fa9af9f to your computer and use it in GitHub Desktop.
Save ritalin/eb1f3110a4112fa9af9f to your computer and use it in GitHub Desktop.
Class for binding to all child control property in a control (eg: TLayout,...)
procedure MyForm.AfterConstruction;
var
expr: TExpressionItem;
begin
inherited;
BindList2 := TBindControlList.Create(Self);
BindList2.SourceComponent := AdapterBindSource1;
BindList2.SourceMemberName := 'ColorsName1';
BindList2.ControlComponent := GridLayout1;
expr := BindList2.FormatExpressions.AddExpression;
expr.SourceExpression := 'Value';
expr.ControlExpression := 'Text';
BindList2.Active := true;
end;
unit Sample;
interface
uses
SysUtils, Classes,
Data.Bind.Components, Data.Bind.Consts, Data.Bind.Editors, FMX.Controls, FMX.Bind.Editors,
System.Bindings.EvalProtocol
;
type
TBindControlList = class(TCustomBindList)
protected
function TryGetBindListEditor(out AEditor: IBindListEditorCommon): Boolean; override;
end;
TBindControlListEditor = class(TBindListEditor)
private
FControl: TControl;
FEditorObj: TControlEditorObject;
FIndex: integer;
protected
function AddItem(Select: Boolean = False): IScope; override;
function CanInsertItem: Boolean; override;
function InsertItem(Select: Boolean = False): IScope; override;
function MoveNext: Boolean; override;
function CurrentItem: IScope; override;
procedure DeleteToEnd; override;
procedure SetSelectedText(const AValue: string); override;
function GetSelectedText: string; override;
function GetRowCount: Integer; override;
procedure BeginUpdate; override;
procedure EndUpdate; override;
procedure ClearList; override;
public
constructor Create(AControl: TControl);
destructor Destroy; override;
end;
TBindControlListEditorObject = class(TControlEditorObject)
protected
function CurrentControl: TControl; override;
function CheckRange: Boolean; override;
end;
procedure Register;
implementation
uses
System.Bindings.ObjEval, FMX.Types
;
procedure Register;
begin
RegisterBindComponents(SDataBindingsCategory_Lists, [TBindControlList]);
end;
{ TBindControlList }
function TBindControlList.TryGetBindListEditor(
out AEditor: IBindListEditorCommon): Boolean;
begin
AEditor := nil;
if (Self.ControlComponent <> nil) and
(not (csDestroying in Self.ControlComponent.ComponentState)) then
begin
if Self.ControlComponent is TControl then begin
AEditor := TBindControlListEditor.Create(TControl(Self.ControlComponent));
end;
end;
Result := Assigned(AEditor);
end;
{ TBindControlListEditor }
constructor TBindControlListEditor.Create(AControl: TControl);
begin
FControl := AControl;
FEditorObj := TBindControlListEditorObject.Create;
end;
destructor TBindControlListEditor.Destroy;
begin
FEditorObj.Free;
inherited;
end;
procedure TBindControlListEditor.BeginUpdate;
begin
FControl.BeginUpdate;
end;
procedure TBindControlListEditor.EndUpdate;
begin
FControl.EndUpdate;
end;
procedure TBindControlListEditor.ClearList;
begin
FIndex := 0;
end;
function TBindControlListEditor.AddItem(Select: Boolean): IScope;
begin
if FIndex >= FControl.ControlsCount then Exit(nil);
Result := WrapObject(FControl.Controls[FIndex]);
Inc(FIndex);
end;
function TBindControlListEditor.CanInsertItem: Boolean;
begin
Result := false;
end;
function TBindControlListEditor.CurrentItem: IScope;
begin
Abort;
end;
procedure TBindControlListEditor.DeleteToEnd;
begin
inherited;
Abort;
end;
function TBindControlListEditor.GetRowCount: Integer;
begin
Result := FControl.ControlsCount;
end;
function TBindControlListEditor.GetSelectedText: string;
begin
Abort;
end;
function TBindControlListEditor.InsertItem(Select: Boolean): IScope;
begin
Abort;
end;
function TBindControlListEditor.MoveNext: Boolean;
begin
Result := false;
end;
procedure TBindControlListEditor.SetSelectedText(const AValue: string);
begin
inherited;
Abort;
end;
{ TBindControlListEditorObject }
function TBindControlListEditorObject.CheckRange: Boolean;
begin
Abort;
Result := false;
end;
function TBindControlListEditorObject.CurrentControl: TControl;
begin
Abort;
Result := nil;
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment