用OO绑定组合框

时间:2017-03-07 07:53:46

标签: oop delphi combobox delphi-10.1-berlin livebindings

我有2个商务课程。此类由数据库加载。 “product”类具有属性“category”,它是类别的实例。

unit Unit5;

interface

uses
    System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
    FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, Generics.Collections,
    FMX.Edit, FMX.ListBox, FMX.Controls.Presentation, FMX.StdCtrls,
    Data.Bind.Components, Data.Bind.ObjectScope, Data.Bind.GenData,
    FMX.Bind.Editors, System.Rtti, System.Bindings.Outputs,
    Data.Bind.EngExt, FMX.Bind.DBEngExt;

type
    TCategory = class
    private
        FName     : String;
        FShortName: String;
        FID       : integer;
    public
        constructor Create(IDFromDataBase: integer; NameFromDataBase: string);
        property ID: integer read FID write FID;
        property Name: String read FName write FName;
    end;

    TProduct = class
    private
        FID      : integer;
        FName    : string;
        FCategory: TCategory;
    public
        constructor Create(IDFromDataBase: integer; NameFromDataBase: string; Cat: TCategory);
        property ID: integer read FID write FID;
        property Name: string read FName write FName;
        property Category: TCategory read FCategory write FCategory;
    end;

    TForm5 = class(TForm)
        Label1: TLabel;
        Category: TLabel;
        ComboProductCategory: TComboBox;
        EditProductName: TEdit;
        PrototypeBindSourceCategory: TPrototypeBindSource;
        PrototypeBindSourceProduct: TPrototypeBindSource;
        procedure FormCreate(Sender: TObject);
        procedure PrototypeBindSourceProductCreateAdapter(Sender: TObject; var ABindSourceAdapter: TBindSourceAdapter);
        procedure PrototypeBindSourceCategoryCreateAdapter(Sender: TObject; var ABindSourceAdapter: TBindSourceAdapter);
    private
        { Déclarations privées }
        FListCategory: TObjectList<TCategory>;
        FProduct     : TProduct;
    public
        { Déclarations publiques }
        constructor Create(AOwner: TComponent); override;

    end;

var
    Form5: TForm5;

implementation

{$R *.fmx}
{ TCategory }

constructor TCategory.Create(IDFromDataBase: integer; NameFromDataBase: string);
begin
    inherited Create;
    FID   := IDFromDataBase;
    FName := NameFromDataBase;
end;

{ TProduct }

constructor TProduct.Create(IDFromDataBase: integer; NameFromDataBase: string; Cat: TCategory);
begin
    inherited Create;
    FID       := IDFromDataBase;
    FName     := NameFromDataBase;
    FCategory := Cat;
end;

constructor TForm5.Create(AOwner: TComponent);
begin
    FListCategory := TObjectList<TCategory>.Create();
    // Normaly load by query on database
    FListCategory.Add(TCategory.Create(1, 'Clothe'));
    FListCategory.Add(TCategory.Create(2, 'Luxury'));

    FProduct := TProduct.Create(1, 'Clock', FListCategory.Items[1]);

    inherited Create(AOwner);
end;

procedure TForm5.FormCreate(Sender: TObject);
var
    // Bind sur un TEdit
    LinkControl: TLinkControlToField;
    ListLink   : TLinkFillControlToField;
begin
    PrototypeBindSourceProduct.Active  := false;
    PrototypeBindSourceCategory.Active := false;

    try
        // Bind product name on edit.
        LinkControl            := TLinkControlToField.Create(self);
        LinkControl.DataSource := PrototypeBindSourceProduct;
        LinkControl.FieldName  := 'Name';
        LinkControl.Control    := EditProductName;
        LinkControl.Track      := true;

        // Bind combo.
        ListLink                      := TLinkFillControlToField.Create(self);
        ListLink.DataSource           := PrototypeBindSourceProduct;
        ListLink.FieldName            := 'Category.ID';
        ListLink.Control              := ComboProductCategory;
        ListLink.FillDataSource       := PrototypeBindSourceCategory;
        ListLink.FillValueFieldName   := 'ID';
        ListLink.FillDisplayFieldName := 'Name';
        ListLink.AutoFill             := true;
        ListLink.Track                := true;
    finally
        PrototypeBindSourceCategory.Active := true;
        PrototypeBindSourceProduct.Active  := true;
    end;
end;

procedure TForm5.PrototypeBindSourceCategoryCreateAdapter(Sender: TObject; var ABindSourceAdapter: TBindSourceAdapter);
begin
    ABindSourceAdapter := TListBindSourceAdapter<TCategory>.Create(self, FListCategory);
end;

procedure TForm5.PrototypeBindSourceProductCreateAdapter(Sender: TObject; var ABindSourceAdapter: TBindSourceAdapter);
begin
    ABindSourceAdapter := TObjectBindSourceAdapter<TProduct>.Create(self, FProduct);
end;

end.

enter image description here

enter image description here

但是当我启动程序时,组合框就加载了类别列表。但我没有选中的项目。 我认为这里的问题是:

ListLink.FieldName            := 'Category.ID';

我不想在我的班级“产品”上创建“CategoryID”。 在OO类上绑定组合框的好方法是什么?

0 个答案:

没有答案