问题描述
我正在使用虚拟树状视图.有可靠的方法来知道节点是否为根节点吗?
I am using Virtual Tree View. Is there a reliable way to know if a node is root or not?
我尝试使用
if not Assigned(Node.Parent) then
Output('This is root')
else
Output('This is not root')
但是不起作用.
我尝试使用
if Node = tvItems.RootNode then
Output('This is root')
else
Output('This is not root')
但也不起作用.
推荐答案
VTV
(或 VST
)中的最终根节点是一个特殊的不可见节点,它充当所有用户创建的根节点(使用 parent = nil
创建的节点)的父级.通过设计,此特殊的不可见节点的 NextSibling
和 PrevSibling
属性设置为指向自身.
The ultimate root node in VTV
(or VST
) is a special invisible node, which acts as parent for all user created root nodes (nodes created with parent = nil
). This special invisible node has by design its NextSibling
and PrevSibling
properties set to point to itself.
要检测某个节点是否为根节点(从用户创建的根的角度来看),您可以例如做:
To detect whether a node is root node (in the sense of user created root) you can e.g. do:
procedure TForm16.tvItemsNodeClick(Sender: TBaseVirtualTree;
const HitInfo: THitInfo);
begin
if HitInfo.HitNode.Parent.NextSibling = HitInfo.HitNode.Parent then
Caption := 'Root node'
else
Caption := 'Not root node';
end;
或者,如OP所述,并且不使用内部实现细节:
Alternatively, as OP commented, and without using internal implementation details:
procedure TForm16.tvItemsNodeClick(Sender: TBaseVirtualTree;
const HitInfo: THitInfo);
begin
if HitInfo.HitNode.Parent = Sender.RootNode then
Caption := 'Root node'
else
Caption := 'Not root node';
end;
引用:TBaseVirtualTree.RootNode属性(在帮助中)
Ref: TBaseVirtualTree.RootNode Property (in Help)
这篇关于如何知道节点在Virtual TreeView中是根?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!