Delphi XE 6 FMX TreeListVew文本margin.left导致运行时错误

时间:2015-02-09 23:05:56

标签: delphi firemonkey delphi-xe6

我需要在左边移动TreeViewItem.Text。我的代码导致运行时错误。

constructor TVppTreeViewItem.Create(AOwner: TComponent);
 var
 c:TTextControl;
begin
  inherited;
  self.Text:='test';
  self.TextObject.Align:=TAlignLayout.Left;
  self.TextObject.Margins.Left:=50;
end;

如何正确编码?

1 个答案:

答案 0 :(得分:0)

创建时TextObject TTreeItem字段{和您的TVppTreeItemnil并且访问它会导致AV错误。您应该将修改TextObject的代码移动到ApplyStyle方法,其中TextObject将从Style初始化。由于无法保证即使在应用样式后TextObject也有效,因此在尝试对其进行任何操作之前,应检查nil

  TVppTreeViewItem = class(TTreeViewItem)
  protected
    procedure ApplyStyle; override;
  public
    constructor Create(AOwner: TComponent); override;
  end;

constructor TVppTreeViewItem.Create(AOwner: TComponent);
begin
  inherited;
  self.Text := 'test';
end;

procedure TVppTreeViewItem.ApplyStyle;
begin
  inherited;
  if Assigned(TextObject) then
    begin
      TextObject.Align := TAlignLayout.Left;
      TextObject.Margins.Left := 50;
    end;
end;