如何在运行时将节点添加到FireMonkey的TreeView

问题描述:

我找不到在线文档或Delphi XE2随附的演示中用于向 FMX.TreeView.TTreeView 控件。那么,如何在运行时添加,删除和遍历FireMonkey TreeView的节点?

I can't found any sample in the online documentation, or in the demos included with Delphi XE2, for adding nodes to a FMX.TreeView.TTreeView control at runtime. So, how can I add, remove, and traverse nodes of a FireMonkey TreeView at runtime?

我认为我们都在学习在这一点上...

I think we are all learning at this point...

但是从我看到的TTreeView使用的原理来看,任何控件都可以成为另一个控件的父对象。

But from what I have seen the TTreeView use the principle that any control can parent another control.

您需要做的就是设置 Parent 属性,以使该项目显示为儿童。 / p>

All you need to do is set the Parent Property to get the item to show up as a child.

var
  Item1 : TTreeViewItem;
  Item2 : TTreeViewItem;
begin
  Item1 := TTreeViewItem.Create(Self);
  Item1.Text := 'My First Node';
  Item1.Parent := TreeView1;

  Item2 := TTreeViewItem.Create(Self);
  Item2.Text := 'My Child Node';
  Item2.Parent := Item1;
end;

因此,您可以做以前从未做过的事情,例如在TreeView中放置任何控件。例如,此代码将向Item2使用的区域添加一个按钮,直到该Item2可见,该按钮才可见。

Because of this you can do things never possible before, such as placing any control in the TreeView. For example this code will add a button to the area used by Item2, and the button won't be visible until the Item2 is visible.

  Button := TButton.Create(self);
  Button.Text := 'A Button';
  Button.Position.X := 100;
  Button.Parent := Item2;