0

我有 2 个商务舱。这些类由数据库加载。“产品”类有一个属性“类别”,它是类别的一个实例。

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.

在此处输入图像描述

在此处输入图像描述

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

ListLink.FieldName            := 'Category.ID';

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

4

0 回答 0