使用Delphi Seattle,我正在尝试在当前项目中获取livebinding,并创建了一个外部填充TFDMemTable的表单。 memTable连接到TGrid和TListbox。网格显示所有类似的信息,但列表框保持为空。
我做错了什么?
代码(从实际情况简化,但仍显示空列表框):
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, System.Rtti, FireDAC.Stan.Intf,
FireDAC.Stan.Option, FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf, FireDAC.DApt.Intf,
FireDAC.Stan.StorageBin, Data.Bind.EngExt, Fmx.Bind.DBEngExt, Fmx.Bind.Grid, System.Bindings.Outputs,
Fmx.Bind.Editors, Data.Bind.Components, Data.Bind.Grid, FMX.ListBox, Data.Bind.DBScope, Data.DB, FireDAC.Comp.DataSet,
FireDAC.Comp.Client, FMX.Layouts, FMX.Grid, FMX.Types, FMX.Controls, FMX.Controls.Presentation, FMX.StdCtrls,
FMX.Forms;
type
TForm1 = class(TForm)
fdmAccounts: TFDMemTable;
fdmAccountscode: TStringField;
fdmAccountsdesc: TStringField;
bsAccounts: TBindSourceDB;
Grid1: TGrid;
BindingsList1: TBindingsList;
ListBox1: TListBox;
LinkGridToDataSourcebsAccounts: TLinkGridToDataSource;
LinkFillControlToField1: TLinkFillControlToField;
procedure FormCreate(Sender: TObject);
private
FItemlist: TStringlist;
procedure Refreshlist(Sender: TObject);
procedure UpdateAccounts(afilter: string);
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
procedure TForm1.UpdateAccounts(aFilter: string);
var
s: string;
begin
with fdmAccounts do
begin
EmptyDataSet;
for s in FItemList do
begin
if aFilter.IsEmpty or s.Contains(aFilter) then
InsertRecord([s, '']);
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FItemlist := TStringList.Create;
FItemlist.Delimiter := ',';
Fitemlist.DelimitedText := 'item1, item2, ander item3, laatste item';
fdmAccounts.Open;
Refreshlist(nil);
end;
procedure TForm1.Refreshlist(Sender: TObject);
begin
UpdateAccounts('');
end;
end.
生活绑定定义:
object BindingsList1: TBindingsList
Methods = <>
OutputConverters = <>
Left = 164
Top = 237
object LinkGridToDataSourcebsAccounts: TLinkGridToDataSource
Category = 'Quick Bindings'
DataSource = bsAccounts
GridControl = Grid1
Columns = <>
end
object LinkFillControlToField1: TLinkFillControlToField
Category = 'Quick Bindings'
Control = ListBox1
Track = True
FillDataSource = bsAccounts
FillDisplayFieldName = 'desc'
AutoFill = True
FillExpressions = <>
FillHeaderExpressions = <>
FillBreakGroups = <>
end
端
我也尝试将LinkFillControlToField1的FillExpression设置为:
FillExpressions = <
item
SourceMemberName = 'desc'
ControlMemberName = 'Text'
end>
但结果相同..空列表框
答案 0 :(得分:2)
<强>更新强>
希望你能按照这个例子,这是我第二次尝试使用 LiveBindings用来填充一个TBox的DataClSet,你应该可以得到 它也有效。你可能想要注意填充我在下面这个答案的原始版本中提到的TStringGrid的问题。
DFM提取
object ListBox1: TListBox
Left = 8
Top = 320
Width = 121
Height = 97
ItemHeight = 13
TabOrder = 6
end
object DataSource1: TDataSource
DataSet = CDS1
Left = 128
Top = 24
end
object CDS1: TClientDataSet
Aggregates = <>
Params = <>
OnNewRecord = CDS1NewRecord
Left = 72
Top = 24
object CDS1ID: TIntegerField
FieldName = 'ID'
end
object CDS1Name: TStringField
FieldName = 'Name'
Size = 40
end
object CDS1Value: TStringField
FieldName = 'Value'
Size = 80
end
end
object BindSourceDB1: TBindSourceDB
DataSource = DataSource1
ScopeMappings = <>
Left = 216
Top = 32
end
object BindingsList1: TBindingsList
Methods = <>
OutputConverters = <>
Left = 72
Top = 96
object LinkListControlToField1: TLinkListControlToField
Category = 'Quick Bindings'
DataSource = BindSourceDB1
FieldName = 'Name'
Control = ListBox1
FillExpressions = <>
FillHeaderExpressions = <>
FillBreakGroups = <>
end
end
代码提取
procedure TForm1.FormCreate(Sender: TObject);
var
i : Integer;
begin
CDS1.IndexFieldNames := 'ID';
CDS1.CreateDataSet;
for i := 1 to 6 do begin
CDS1.Insert;
CDS1.FieldByName('Name').AsString := 'Name ' + IntToStr(i);;
CDs1.FieldByName('Value').AsString := 'Value ' + IntToStr(i);
CDS1.Post;
end;
CDS1.First;
StringGrid1.Invalidate;
end;
procedure TForm1.CDS1NewRecord(DataSet: TDataSet);
begin
Inc(NextID);
DataSet.FieldByName('ID').AsInteger := NextID;
end;
我之前尝试的唯一不同之处是
LinkListControlToField1: TLinkListControlToField
我假设(错误地,事实证明)TLinkListControlToField用于 TListViews,但它显然也适用于TListViews
最初发布的回答
我不确定你做错了什么,LiveBindings对我来说似乎很麻烦 - 请看我对这个问题的回答:Delphi TEdit to filter Tstringgrid with Access。 stringrid显示ID = 6的行重复,但不是ID = 5的那个事实让我觉得不是特别有希望,因为这是一个非常明显的问题。
我无法让LiveBindings与ListBox一起工作,在新的Seattle VCL项目中自己动手做或者按照本文http://edn.embarcadero.com/article/41707进行操作。该文章的几个问题之一是它引用了一个“TBindScopeDB”组件,据我所见,它不存在。即使考虑到它可能是“TBindSourceDB”的拼写错误这一事实,当我尝试按照文章中的步骤操作时,我也没有得到描述的结果,并且ListBox当然不会被填充。
您可能需要查看它提到的SourceForge VCL项目https://radstudiodemos.svn.sourceforge.net/svnroot/radstudiodemos/branches/RadStudio_XE2/LiveBindings/bindlist。我自己没有尝试过,但它看起来比我想象的要复杂得多,不过那是因为它是因为XE2和LiveBindings从那以后一直在继续,我不知道。我确实看过DFM,我永远不会猜到Expression属性会被赋予它们的值,无论是从第一原理还是在文章中所说的。我尝试在我的项目中使用它们,但ListBox保持空白。