我想更改现有虚拟(根)节点的单元格文本。我可以更改数据,但我必须刷新整个 TreeView 以便再次触发 GetText。是否有更简单的方法或可能的方法来仅刷新 1 个根节点而不是整个树?
感谢您的帮助。
我想更改现有虚拟(根)节点的单元格文本。我可以更改数据,但我必须刷新整个 TreeView 以便再次触发 GetText。是否有更简单的方法或可能的方法来仅刷新 1 个根节点而不是整个树?
感谢您的帮助。
在以下代码中,我尝试模拟您的节点更新事件。这里由按钮单击Button1Click
事件表示。在这里,如果您单击按钮,数据记录中的值将增加,并且节点将被强制重新绘制(触发OnGetText
事件的原因,您将在该记录中准备好获取这个新增加的值)。在您的真实代码中,您需要执行以下操作:
TopNode
:-)Count
在我的例子中值是递增的)InvalidateNode
(什么会触发一些事件的虚拟事件链,但OnGetText
会在其中)type
PTreeData = ^TTreeData;
TTreeData = record
Title: string;
Count: Integer;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
TreeData: PTreeData;
TreeNode: PVirtualNode;
begin
// this is just for memory leaks report
ReportMemoryLeaksOnShutdown := True;
// initialize data size for our data record
VirtualTree.NodeDataSize := SizeOf(TTreeData);
// add one node and store it to the TreeNode variable
TreeNode := VirtualTree.AddChild(nil);
// get the pointer to just created node data record
TreeData := VirtualTree.GetNodeData(TreeNode);
// and assign some values to that data record
TreeData.Title := 'Count: ';
TreeData.Count := 0;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
TreeData: PTreeData;
TreeNode: PVirtualNode;
begin
// now I take the the top node just as an example
TreeNode := VirtualTree.TopNode;
// get the pointer to its data record
TreeData := VirtualTree.GetNodeData(TreeNode);
// increase the value in data record for this node
TreeData.Count := TreeData.Count + 1;
// and force the node to repaint, what will except
// other events trigger also the OnGetText one
VirtualTree.InvalidateNode(TreeNode);
end;
procedure TForm1.VirtualTreeGetText(Sender: TBaseVirtualTree;
Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
var CellText: string);
var
TreeData: PTreeData;
begin
// here I'll get the pointer to data of the currently refreshed cell's node
TreeData := VirtualTree.GetNodeData(Node);
// and return the caption of the cell
CellText := TreeData.Title + IntToStr(TreeData.Count);
end;
procedure TForm1.VirtualTreeFreeNode(Sender: TBaseVirtualTree;
Node: PVirtualNode);
var
TreeData: PTreeData;
begin
// this is here because of VT memory leaks, so get the pointer to data
TreeData := Sender.GetNodeData(Node);
// and finalize them
Finalize(TreeData^);
end;
此代码是使用编辑框文本更改编辑选定节点文本
procedure Form1.btnEditTextClick(Sender: TObject);
var
Data: PTreeData;
begin
Data := VST.GetNodeData(VST.FocusedNode);
Data.Title := Edit2.Text;
VST.InvalidateNode(VST.FocusedNode);
end;