2

使用 Delphi Seattle,我试图在我们当前的项目中掌握实时绑定的窍门,并创建了一个带有外部填充 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>

但结果相同..空列表框

4

1 回答 1

2

更新:

希望如果您遵循这个示例,这是我第二次尝试使用 LiveBindings 填充 TClientDataSet 的 ListBox,您也应该能够使其正常工作。您可能想注意填充我在下面这个答案的原始版本中提到的 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 一起使用,我自己在新的西雅图 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 保持为空。

于 2016-05-20T12:58:55.393 回答