【问题标题】:LiveBindings - TList<TMyObject> bound to TStringGridLiveBindings - TList<TMyObject> 绑定到 TStringGrid
【发布时间】:2011-11-22 09:33:02
【问题描述】:

我有以下示例代码集,如何使用 LiveBindings 将 Data 列表元素绑定到 TStringGrid。我需要双向更新,以便在更改网格中的列时可以更新底层TPerson

我已经看到了如何使用基于TDataset 的绑定来执行此操作的示例,但我需要在没有TDataset 的情况下执行此操作。

unit Unit15;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, System.Generics.Collections;

type
  TPerson = class(TObject)
  private
    FLastName: String;
    FFirstName: string;
  published
    property firstname : string read FFirstName write FFirstName;
    property Lastname : String read FLastName write FLastName;
  end;

  TForm15 = class(TForm)
    StringGrid1: TStringGrid;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    Data : TList<TPerson>;
  end;


var
  Form15: TForm15;



implementation

{$R *.dfm}

procedure TForm15.FormCreate(Sender: TObject);
var
 P : TPerson;
begin
  Data := TList<TPerson>.Create;
  P := TPerson.Create;
  P.firstname := 'John';
  P.Lastname := 'Doe';
  Data.Add(P);
  P := TPerson.Create;
  P.firstname := 'Jane';
  P.Lastname := 'Doe';
  Data.Add(P);
  // What can I add here or in the designer to link this to the TStringGrid.
end;

end.

【问题讨论】:

  • 不...菲尔(谁问/回答了这个问题)和我是试图弄清楚这一切的同事。但一直无法弄清楚使网格工作所需的表达式。
  • 好吧,我猜 FM 框架目前缺少一些文档。作为旁注,在代码中或隐藏在设计器中进行此链接的首选方式是什么?就我个人而言,我不想隐藏代码中的逻辑。
  • 我已经尝试了一个小时,但无法弄清楚。似乎应该很容易做到的事情,但不知何故并非如此。我希望有人提出解决方案!
  • @Jim,不,我们没有弄清楚。我们完成了绑定系统的编写,这更容易了。

标签: delphi delphi-xe2 livebindings


【解决方案1】:

部分解决方案:从 TList 到 TStringGrid 是:

procedure TForm15.FormCreate(Sender: TObject); 
var 
 P : TPerson; 
 bgl: TBindGridList;
 bs: TBindScope;
 colexpr: TColumnFormatExpressionItem;
 cellexpr: TExpressionItem;
begin 
  Data := TList<TPerson>.Create; 
  P := TPerson.Create; 
  P.firstname := 'John'; 
  P.Lastname := 'Doe'; 
  Data.Add(P); 
  P := TPerson.Create; 
  P.firstname := 'Jane'; 
  P.Lastname := 'Doe'; 
  Data.Add(P); 
  // What can I add here or in the designer to link this to the TStringGrid. 

  while StringGrid1.ColumnCount<2 do
    StringGrid1.AddObject(TStringColumn.Create(self));

  bs := TBindScope.Create(self);

  bgl := TBindGridList.Create(self);
  bgl.ControlComponent := StringGrid1;
  bgl.SourceComponent := bs;

  colexpr := bgl.ColumnExpressions.AddExpression;
  cellexpr := colexpr.FormatCellExpressions.AddExpression;
  cellexpr.ControlExpression := 'cells[0]';
  cellexpr.SourceExpression := 'current.firstname';

  colexpr := bgl.ColumnExpressions.AddExpression;
  cellexpr := colexpr.FormatCellExpressions.AddExpression;
  cellexpr.ControlExpression := 'cells[1]';
  cellexpr.SourceExpression := 'current.lastname';

  bs.DataObject := Data;
end; 

【讨论】:

    猜你喜欢
    • 2011-03-11
    • 2014-04-23
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    相关资源
    最近更新 更多