使用 1 个数据副本同步多个 VirtualStringTree
我有一个显示在 VirtualStringTree 中的数据层次结构。我在应用程序中多次使用此层次结构,并对树的绘制/显示方式进行了轻微修改。我的方法当前使用 AddChild() 过程来添加节点,因此在应用程序运行时我有多个数据副本。
我现在想合并这些树,并拥有一个指向实际数据的“主”树,但随后让“从”树指向相同的数据。
我有点不确定是否/如何实现这一点。我认为我可以简单地加载主树并用指向我保存的数据的指针填充其 NodeData ,然后对于所有从属树,只需将相同的指针存储在其节点数据中。
不过我运气不太好。
我当前的代码如下所示:
//Initialize the NodeDataSize
procedure TForm1.FormCreate(Sender: TObject);
begin
TreeMasterComponents.NodeDataSize := SizeOf(rMasterComponent);
VST.NodeDataSize := SizeOf(Pointer);
end;
将主树复制到从树的过程
procedure TForm1.LoadSlaveTree(ATree: TVirtualStringTree);
var Node : PVirtualNode;
procedure RecursiveCopy(SrcPNode,SrcTNode : PVirtualNode; ATree : TVirtualStringTree);
var SrcNode, TargetNode : PVirtualNode;
SrcData : PMasterComponent;
begin
SrcNode := TreeMasterComponents.GetFirstChild(SrcPNode);
while Assigned(SrcNode) do
begin
SrcData := TreeMasterComponents.GetNodeData(SrcNode);
TargetNode := ATree.AddChild(SrcTNode,SrcData);
RecursiveCopy(SrcNode,TargetNode,ATree);
SrcNode := SrcNode.NextSibling;
end;
end;
begin
ATree.BeginUpdate;
ATree.Clear;
Node := TreeMasterComponents.GetFirst(true);
while Assigned(Node) do
begin
RecursiveCopy(Node,nil,ATree);
Node := Node.NextSibling;
end;
ATree.EndUpdate;
end;
从属树获取CellText的过程
procedure TForm1.SlaveGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
Column: TColumnIndex; TextType: TVSTTextType; var CellText: WideString);
var Data : PMasterComponent;
begin
Data := Sender.GetNodeData(Node);
Case column of
0:CellText := Data^.ComponentCode;
1:CellText := Data^.FullLocation;
end;
end;
目前,节点已添加到正确的层次结构中,但是从属树没有出现文本。任何帮助将不胜感激。
I have a heirarchy of data that is displayed in a VirtualStringTree. I use this heirarchy multiple times in my application with slight modifications to the way the tree is drawn/displayed. My method currently utilizes the AddChild() procedure for adding nodes an d as such i have multiple copies of the data when the application is run.
I would now like to consolidate these trees and have a 'master' tree which points to the actual data, but then have the 'slave' trees point to the SAME data.
I am a little unsure of if/how this can be acheived. I would think i could simply load the master tree and populate its NodeData with pointers to where the data i being held, and then for all the slave trees, simply store the same pointer in their nodedata.
However im not having much luck.
My current code looks like:
//Initialize the NodeDataSize
procedure TForm1.FormCreate(Sender: TObject);
begin
TreeMasterComponents.NodeDataSize := SizeOf(rMasterComponent);
VST.NodeDataSize := SizeOf(Pointer);
end;
Procedure to copy the master tree to slave trees
procedure TForm1.LoadSlaveTree(ATree: TVirtualStringTree);
var Node : PVirtualNode;
procedure RecursiveCopy(SrcPNode,SrcTNode : PVirtualNode; ATree : TVirtualStringTree);
var SrcNode, TargetNode : PVirtualNode;
SrcData : PMasterComponent;
begin
SrcNode := TreeMasterComponents.GetFirstChild(SrcPNode);
while Assigned(SrcNode) do
begin
SrcData := TreeMasterComponents.GetNodeData(SrcNode);
TargetNode := ATree.AddChild(SrcTNode,SrcData);
RecursiveCopy(SrcNode,TargetNode,ATree);
SrcNode := SrcNode.NextSibling;
end;
end;
begin
ATree.BeginUpdate;
ATree.Clear;
Node := TreeMasterComponents.GetFirst(true);
while Assigned(Node) do
begin
RecursiveCopy(Node,nil,ATree);
Node := Node.NextSibling;
end;
ATree.EndUpdate;
end;
Procedure for slave tree to getCellText
procedure TForm1.SlaveGetText(Sender: TBaseVirtualTree; Node: PVirtualNode;
Column: TColumnIndex; TextType: TVSTTextType; var CellText: WideString);
var Data : PMasterComponent;
begin
Data := Sender.GetNodeData(Node);
Case column of
0:CellText := Data^.ComponentCode;
1:CellText := Data^.FullLocation;
end;
end;
At the moment, the nodes are added in the correct heirarchy, however there is no text appearing for the slave trees. Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道为什么您的奴隶树中没有出现任何文本,但我想提出以下建议。
可能更容易的是使用树和代码创建一个框架,并在表单上重用该框架。树的每个实例只需加载相同的数据(无需复制节点)。
通过为框架/树的特定实例编写新的事件处理程序,可以通过视觉形式继承来实现细微的修改。如果需要,您还可以从框架继承,创建一个新类。
I don't know why no text appears in your slave trees but I'd like to advise the following.
Probably easier would be to create a frame with the tree and code, and reuse the frame on your forms. Each instance of the tree would simply load the same data (no copying of nodes necessary).
The slight modifications can be achieved by visual form inheritance by writing new event handlers for the specific instance of the frame/tree. You can also inherit from the frame, creating a new class, if needed.
好吧,我相信我找到了一个解决方案:
诀窍是创建一个新记录来存储指向原始数据的指针:
现在复制代码如下所示:
和 OnGetText:
现在,如果我更改一棵树中的数据,我必须这样做要做的就是调用 VST.Invalidate ,更改会反映在另一棵树中。
Ok so i beleive i have found a solution:
The trick was to create a new record to store the Pointer to the original data:
Now the Copy code looks like:
and the OnGetText:
Now if i change data in one tree all i have to do is call VST.Invalidate and the changes are reflected in the other tree.