我如何获得TTreeViewItem的第二个后代以显示在TreeView上

时间:2019-02-17 01:50:33

标签: firemonkey delphi-10.3-rio ttreeview

使用Delphi 10.3和Fmx,如何获取TTreeviewItem的第二个后代以显示其文本?第一个后代工作正常。第二个(T3)始终显示空白。在此示例中,“父项”和“文本”已正确分配给项目。但是在我的项目中,父级不会分配,但文本会分配。

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes,
  System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics,
  FMX.Dialogs, FMX.Layouts, FMX.TreeView;

type
  TForm1 = class(TForm)
    vue: TTreeView;
    procedure FormCreate(Sender: TObject);
  private
  public
  end;

var
  Form1: TForm1;

type
  T1 = class (TTreeViewItem)
    constructor Create (AOwner : TTreeViewItem); reintroduce; virtual;
  end;

  T2 = class (TTreeViewItem)
    constructor Create (AOwner : TTreeViewItem); reintroduce; virtual;
  end;

  T3 = class (T1)
    constructor Create (AOwner : TTreeViewItem); override;
  end;


implementation

{$R *.fmx}

{ T1 }

constructor T1.Create(AOwner: TTreeViewItem);
begin
  inherited Create (AOwner);
  Parent := AOwner;
  Text := 'T1';
end;

{ T2 }

constructor T2.Create(AOwner: TTreeViewItem);
begin
  inherited Create (Aowner);
  Parent := AOwner;
  Text := 'T2';
end;

{ T3 }

constructor T3.Create(AOwner: TTreeViewItem);
begin
  inherited;
//  Parent := AOwner;
  Text := 'T3';
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  R,
  I1, I2 : TTreeViewItem;
  T1a : T1;
  T2a : T2;
  T3a : T3;
begin
  R := TTreeViewItem.Create (vue);
    R.Parent := vue;  R.Text := 'Root';
  { Doesnt Work }
  I1 := TTreeViewItem.Create (R);
    I1.Parent := R;  I1.Text := 'First';
  { Works }
  I2 := TTreeViewItem.Create (R);
    I2.Parent := R;  I2.Text := 'Second';

  { This works }
  T1a := T1.Create (I1);
  { This works }
  T2a := T2.Create (I1);
  { This doesnt work }
  T3a := T3.Create (I1);
end;

end.

这是dfm

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 480
  ClientWidth = 640
  FormFactor.Width = 320
  FormFactor.Height = 480
  FormFactor.Devices = [Desktop]
  OnCreate = FormCreate
  DesignerMasterStyle = 0
  object vue: TTreeView
    Align = Left
    Size.Width = 217.000000000000000000
    Size.Height = 480.000000000000000000
    Size.PlatformDefault = False
    TabOrder = 0
    Viewport.Width = 213.000000000000000000
    Viewport.Height = 476.000000000000000000
  end
end

0 个答案:

没有答案